Asosiy matnga o'tish
ITIT Interview
Python

Turlar va sintaksis

O'rnatilgan turlar, mutable va immutable, is va == farqi, truthy va falsy, f-string, slicing, shartlar, sikllar, match va walrus operatori.

O'rganildi: 0 / 5
Progress

O'rnatilgan turlar

TurMisolO'zgaruvchanmi
int42, 1_000_000Yo'q
float3.14Yo'q
boolTrue, FalseYo'q
str"salom"Yo'q
tuple(1, 2)Yo'q
frozensetfrozenset({1, 2})Yo'q
NoneTypeNoneYo'q
list[1, 2]Ha
dict{"a": 1}Ha
set{1, 2}Ha

int cheksiz katta bo'lishi mumkin: 2 ** 1000 ishlaydi.

O'zgaruvchilar — bu havolalar

Python'da o'zgaruvchi obyektga havola (nom). Tayinlash nusxa yaratmaydi:

a = [1, 2, 3]
b = a          # bir xil ro'yxat
b.append(4)
print(a)       # [1, 2, 3, 4]

x = 10
y = x
y += 1         # int immutable: yangi obyekt yaratiladi
print(x)       # 10

is va ==

a = [1, 2]
b = [1, 2]
a == b      # True  — qiymatlar teng
a is b      # False — turli obyektlar

x = None
x is None   # None, True, False bilan doim `is` ishlatiladi

Truthy va falsy

Falsy qiymatlar: False, None, 0, 0.0, "", [], {}, set(), (). Qolgan hammasi truthy.

if not items:
    print("Ro'yxat bo'sh")

Satrlar

name, age = "Ali", 25
f"{name} {age} yoshda"           # f-string
f"{3.14159:.2f}"                 # '3.14'
f"{age=}"                        # 'age=25' — debug uchun

s = "Python"
s[0], s[-1]                      # 'P', 'n'
s[1:4]                           # 'yth'
s[::-1]                          # 'nohtyP' — teskari
", ".join(["a", "b", "c"])       # 'a, b, c'
"a,b,c".split(",")               # ['a', 'b', 'c']
"  salom ".strip()               # 'salom'

Slicing [start:stop:step] ro'yxat va tuple'larda ham ishlaydi; stop kirmaydi.

Shartlar va sikllar

if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "C"

status = "kattalar" if age >= 18 else "bolalar"   # ternary

for i, name in enumerate(["Ali", "Vali"]):        # indeks bilan
    print(i, name)

for name, score in zip(names, scores):            # parallel
    print(name, score)

for i in range(0, 10, 2):                         # 0, 2, 4, 6, 8
    ...

while queue:
    item = queue.pop()

for ... else: else bloki sikl break siz tugaganda bajariladi.

match (Python 3.10+)

match command:
    case {"action": "move", "x": x, "y": y}:
        move(x, y)
    case {"action": "quit"}:
        quit()
    case _:
        print("Noma'lum buyruq")

Walrus operatori

if (n := len(data)) > 100:
    print(f"Juda katta: {n}")

Intervyu savollari

Ushbu sahifada