Python - Variables

9.
What is the difference between == and is when comparing variables?

When comparing variables in Python, == and is serve different purposes:

== checks for equality of values, while is checks for identity, i.e., if the variables refer to the same object in memory.

# Using == for value equality
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print("Using ==:")
print(a == b)  # True, as the values are the same
print(a == c)  # True, as the values are the same

# Using is for identity
print("\nUsing is:")
print(a is b)  # False, as they are different objects in memory
print(a is c)  # True, as both refer to the same object in memory

In the example, == compares the values of the lists, and it returns True if the values are the same. On the other hand, is checks if two variables refer to the same object in memory.


10.
What is the purpose of the del statement in Python with respect to variables?

In Python, the del statement is used to delete variables or items from a collection (such as lists or dictionaries). It serves several purposes related to memory management and variable manipulation.

Here's an example program illustrating the use of del:

# Using del to delete variables
x = 10
y = [1, 2, 3]

print("Before deletion:")
print("x =", x)
print("y =", y)

# Deleting variables
del x
del y[0]

print("\nAfter deletion:")
# Uncommenting the next line would result in an error since 'x' is deleted
# print("x =", x)
print("y =", y)

In this example, the del statement is used to delete the variable x and the first element of the list y. After deletion, attempting to access x would result in an error, while the list y reflects the removal of its first element.


11.
Explain the concept of dynamic typing in Python.

In Python, dynamic typing is a concept where the type of a variable is determined at runtime, rather than at compile-time. This means that you can assign values of different types to the same variable during the execution of the program.

Here's an example program illustrating dynamic typing in Python:

# Dynamic typing example
variable = 10  # variable is an integer
print("Variable is an integer:", variable)

variable = "Hello"  # variable is now a string
print("Variable is a string:", variable)

variable = [1, 2, 3]  # variable is now a list
print("Variable is a list:", variable)

In this example, the variable variable is assigned different types of values - first an integer, then a string, and finally a list. The type of the variable is determined at runtime based on the assigned value.


12.
How can you concatenate two strings in Python using variables?

In Python, you can concatenate two strings using the + operator or by using the += shorthand.

Here's an example program demonstrating string concatenation in Python:

# String concatenation example
first_name = "John"
last_name = "Doe"

# Using the + operator
full_name = first_name + " " + last_name
print("Full name using + operator:", full_name)

# Using the += shorthand
greeting = "Hello, "
name = "Alice"
greeting += name
print("Greeting using += shorthand:", greeting)

In this example, two strings, first_name and last_name, are concatenated using the + operator to form the full_name. Additionally, a greeting string is concatenated with a name using the += shorthand.