Python Programming - Variables
- Variables - General Questions
x = "Hello"
y = "IndiaBIX"
print(y + x)
In Python, the +
operator can be used to concatenate strings.
In this case, the variables x
and y
hold the strings "Hello"
and "IndiaBIX"
, respectively.
When they are concatenated with the +
operator, the result y+x
is "IndiaBIXHello"
.
y
after executing the following code:
x = [1, 2, 3]
y = x
x = [4, 5, 6]
print(y)
In this code, the variable x
is initially assigned a list containing the values 1, 2, and 3.
The variable y
is then assigned the same list that x
holds a reference to.
However, when x
is reassigned a new list containing the values 4, 5, and 6, y
still holds a reference to the original list [1, 2, 3]
.
So when y
is printed to the console, it prints [1, 2, 3]
.
my_list
?in
and not in
operators can be used to check if a value is or is not in a list, respectively.
my_list = [1, 2, 3]
my_list[1] = 4
print(my_list)
In this code, the variable my_list
is initially assigned a list containing the values 1, 2, and 3.
The second element of the list (which has an index of 1) is then changed to 4 using the assignment operator =
.
When my_list
is printed to the console, it shows the modified list [1, 4, 3]
.
my_string = "hello"
my_string[1] = "a"
print(my_string)
In Python, strings are immutable, which means their individual characters cannot be modified once they're created.
Attempting to change a character of a string using the index operator [ ] results in a TypeError.