for takrorlash operatori

  • oldingi darslarimizda while operatori bilan ishladik

  • for eng ko’p ishlatiladi

  • asosan ketma-ketlikdan qiymatlarni birma-bir olib berish orqali ishlaydi

  • bir necha xil sintaksisga ega

Sodda namunalar

[4]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
apple
banana
cherry
[5]:
for x in "banana":
  print(x)
b
a
n
a
n
a
[17]:
# Iterating over dictionary
print("Dictionary Iteration")

d = dict()

d['xyz'] = 123
d['abc'] = 345
for i in d:
    print("% s % d" % (i, d[i]))
Dictionary Iteration
xyz  123
abc  345
[6]:
t = ((1, 2), (3, 4), (5, 6))
for a, b in t:
    print(a, b)
1 2
3 4
5 6

range() funksiyasi

  • 3 ta parameterdan iborat:

  • start - qiymatning boshlanishi

  • stop - qiymatning tugashi

  • step - qadam

Namunalar

[7]:
for x in range(6):
  print(x)
0
1
2
3
4
5
[8]:
for x in range(2, 6):
  print(x)
2
3
4
5
[9]:
for x in range(2, 30, 3):
  print(x)
2
5
8
11
14
17
20
23
26
29

for-else operatori

[6]:
for x in range(6):
  print(x)
else:
  print("Finally finished!")
0
1
2
3
4
5
Finally finished!

Ichma-ich for operatori

[8]:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

break operatori

[9]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break
apple
banana
[10]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    break
  print(x)
apple
[10]:
for x in range(6):
  if x == 3: break
  print(x)
else:
  print("Finally finished!")
0
1
2

continue operatori

[12]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)
apple
cherry

pass operatori

[16]:
for x in range(10):
  pass

print(x)
9

enumerate() funksiyasi

  • iterable - ketma-ketlik

  • start - boshlanish indeksi

[17]:
l1 = ["eat", "sleep", "repeat"]
s1 = "geek"

# creating enumerate objects
obj1 = enumerate(l1)
obj2 = enumerate(s1)

print ("Return type:", type(obj1))
print (list(enumerate(l1)))

# changing start index to 2 from 0
print (list(enumerate(s1, 2)))
Return type: <class 'enumerate'>
[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]
[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]
[18]:
l1 = ["eat", "sleep", "repeat"]

# printing the tuples in object directly
for ele in enumerate(l1):
    print (ele)

# changing index and printing separately
for count, ele in enumerate(l1, 100):
    print (count, ele)

# getting desired output from tuple
for count, ele in enumerate(l1):
    print(count)
    print(ele)
(0, 'eat')
(1, 'sleep')
(2, 'repeat')
100 eat
101 sleep
102 repeat
0
eat
1
sleep
2
repeat
[24]:
fruits = ['apple', 'banana', 'cherry']
enum_fruits = enumerate(fruits)

next_element = next(enum_fruits)
print(f"Next Element: {next_element}")
Next Element: (0, 'apple')

zip() funksiyasi

[25]:
fruits = ["apple", "banana", "cherry"]
colors = ["red", "yellow", "green"]
for fruit, color in zip(fruits, colors):
    print(fruit, "is", color)
apple is red
banana is yellow
cherry is green