welcome = "Hello world"
greeting = welcome
year = 2026
this = year
year = 2026
this = year
year = 2027
names = ["Andrey", "Carlos", "Nikita"]
names.append("Olesia")
names = ["Andrey", "Carlos"]
teachers = names
names = ["Andrey", "Carlos"]
teachers = names
names.append("Olesia")
print(teachers)
value = 23
another = value
value += 50
print(another)
value += 50 is value = value + 50, which is value = value.__add__(50)
sys.getrefcount, __del__, circular references, weakrefnames[1] is also a referencenames = ["Olesia", "Carlos"]
names = ["Olesia", "Carlos"]
names[1] += " Manuel"
In all these cases, X becomes a reference to a value in memory
X = ...
for X in ...
[... for X in ...]
(... for X in ...)
{... for X in ...}
class X(...):
def X(...):
def fn(X): ... ; fn(12)
with ... as X:
try ... except ... as X:
import X
from ... import X
import ... as X
from ... import ... as X
vat = 0.2
def calculate_cost(quantity: int, price: float) -> float:
return quantity * price * (1 + vat)
print(calculate_cost(5, 1.5))
quantity and price created?vat is a global variable, available both inside and outside the functiondef append_twice(where: list, what: int):
where.append(what)
where.append(what)
return where
print(append_twice([10, 15], 4))
def append_twice(where: list, what: int):
where.append(what)
where.append(what)
return where
items = [10, 15]
print(append_twice(items, 4))
print(items)
def append_twice(where: list, what: int):
where = where + [what, what]
return where
items = [10, 15]
print(append_twice(items, 4))
print(items)
value = 123
value = "Hello world"
nums = [1, 2, 3]
del nums
If it walks like a duck, and it quacks like a duck, then it must be a duck
def sum(a, b):
return a + b
def sum(a: int, b: int) -> int:
return a + b
print(sum("One ", "Two"))
All lists in Python are one-dimensional, but you can create a one-dimensional list of one-dimensional lists
a = [[1, 2, 3], [5, 6, 7]]
print(a[1][2])
a = [[1, 2, "Hello"], ["world", 10], 15]
print(a)
a[1] = [20, "updated"]
values = [0] * 10
print(values)
values = [[0] * 5] * 10
print(values)
values[1][2] = "X"
print(values)
values = [[0] * 5 for _ in range(10)]
values[1][2] = "X"
print(values)
cart = {
"fruits": ["apple", "banana", "orange"],
"vegetables": ["carrot", "potato", "onion"],
"drinks": ["water", "juice"]
}
id(variable)!a = [100, 500]
b = a
print(id(a), id(b))
a = [100, 500]
b = [100, 500]
print(id(a), id(b))
a = 100500
b = 100500
print(id(a), id(b))
number1 = int("100500")
number2 = int("100500")
print(id(number1) == id(number2)) # same as "number1 is number2"
number1 = int("100")
number2 = int("100")
print(id(number1) == id(number2))
Task: identify the range of pre-cached numbers [answer]