Kirish.

Python dasturlash tilini o'rganish vaqtida ko'plab masalalarni dasturini tuzib, bajarib ko'ramiz. Ayrim masalalarni dasturini tuzayotganimizda qandaydir amalni bir necha marta takrorlashimizga to'g'ri keladi. Bunday masalalarni takrorlash ya'ni sikl operatorlari yordamida oson yechishimiz mumkin. Quyida takrorlanuvchi algaritmlarga oid masalalar berilgan bo'lib, ular yordamida python dasturlash tilining sikl operatorlarini o'rganamiz.

Takrorlanuvchi algaritm.
Takrorlanuvchi algaritm.

Python tilida ikkita takrorlash operatori For va While dan foydalanamiz. For operatori takrorlanishlar soni aniq bo'lganda foydalaniladi. Quyidagicha ko'rinishda yozishimiz mumkin:

for i in range(a, b, d):
         takrorlanuvchi qism

Bu yerda i takrorlanishlar soni, a takrorlanishning birinchi qiymati, b takrorlanishning oxirgi qiymati, d qadam qiymati. For operatorining parametrlarini boshqacha ko'rinishda yozishimiz ham mumkin.

Takrorlanishlar soni oldindan aniq bo'lmaganda va qandaydir shartga bog'liq bo'lganda While operatoridan foydalanamiz. Quyidagicha ko'rinishda yozishimiz mumkin:

while <shart>:
       takrorlanuvchi qism

Bu yerda shart rost bo'lganda takrorlanuvchi qism bajariladi. 

For va While operatori yordamida quyidagi masalarni dasturini tuzamiz.

1-masala. "Hello World" so'zini 10 marta ekranga chiqaruvchi dastur tuzing.

for i in range(10):print("Hello World")

While yordamida

i=1
while i<=10:
	print("Hello World")
	i+=1

2-masala. 1 dan 100 gacha bo'lgab sonlarni ekranga chiqaruvchi dastur tuzing.

for i in range(1,101,1):
	print(i)

While yordamida

i=1
while i<=100:
	print(i)
	i+=1

3-masala. 1 dan 100 gacha bo'lgan toq sonlarni chiqaruvchi dastur tuzing.

for i in range(1,100,2):
	print(i)

While yordamida

i=1
while i<=100:
	if i%2!=0:print(i)
	i+=1

4-masala. 1 dan 10 gacha bo'lgan sonlarning kvadratini chiqaruvchi dastur tuzing.

for i in range(1,11,1):
	print(i*i)

While yordamida

i=1
while i<=10:
	print(i*i)
	i+=1

5-masala. n dan m gacha bo'lgan juft sonlarni chiqaruvchi dastur tuzing.

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
for i in range(n,m+1,1):
	if i%2==0:print(i)

While yordamida

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
while n<=m:
	if n%2==0:print(n)
	n+=1

6-masala. 1 dan n gacha bo'lgan sonlarning yig'indisini hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
s=0
for i in range(1,n+1,1):
	s=s+i
print(s)

While yordamida

n=int(input("n sonini kiriting: "))
s=0
i=1
while i<=n:
	s=s+i
	i+=1
print(s)

7-masala. 1 dan n gacha bo'lgan sonlarning ko'paytmasi(faktorial)ni hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
p=1
for i in range(1,n+1,1):
	p=p*i
print(p)

While yordamida

n=int(input("n sonini kiriting: "))
p=1
i=1
while i<=n:
	p=p*i
	i+=1
print(p)

8-masala. For operatori yordamida hafta kuni nomlarini chiqaruvchi dastur tuzing.

for i in ["Dushanba","Seshanba","Chorshanba","Payshanba","Juma","Shanba","Yakshanba"]:
	print(i)

9-masala. n dan m gacha 5 ga qoldiqsiz bo'linadigan sonlarni chiqaruvchi dastur tuzing.

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
for i in range(n,m+1,1):
	if i%5==0:print(i)

While yordamida

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
while n<=m:
	if n%5==0:print(n)
	n+=1

10-masala. n sonini barcha qoldiqsiz bo'luvchilarini topuvchi dastur tuzing.

n=int(input("n sonini kiriting: "))
for i in range(1,n+1,1):
	if n%i==0:print(i)

While yordamida

n=int(input("n sonini kiriting: "))
i=1
while i<=n:
	if n%i==0:print(i)
	i+=1

11-masala. Kiritilgan so'zni teskari chiqaruvchi dastur tuzing.

s=input("So`z kiriting: ")
s2=""
for i in range(len(s)-1,-1,-1):
    s2=s2+s[i]
print(s2)

12-masala. Quyidagi ifodani hisoblovchi dastur tuzing. 

n=int(input("n sonini kiriting: "))
s=0
for i in range(0,n,1):
	s=s+(i+2)
print(s)

While yordamida

n=int(input("n sonini kiriting: "))
s=0
i=0
while i<=n:
	s=s+(i+2)
	i+=1
print(s)

13-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
p=1
for k in range(0,n,1):
	p=p*(k+2)
print(p)

While yordamida

n=int(input("n sonini kiriting: "))
p=1
k=1
while i<=n:
	p=p*(k+2)
	k+=1
print(p)

14-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
s=0
s2=0
for i in range(0,n,1):
	for k in range(0,m,1):
		s=s+(i+k)
	s2=s2+s
print(s2)

15-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
m=int(input("m sonini kiriting: "))
s=0
p=1
for i in range(1,n,1):
	for k in range(1,m,1):
		p=p*(i+k)
	s=s+p
print(s)

16-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

import math
n=int(input("n sonini kiriting: "))
s=0
for i in range(1,n,1):
	s=math.sqrt(s+n)
print(s)

17-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

n=int(input("n sonini kiriting: "))
s=0
for i in range(1,n,1):
	s=s+(i/i+1)
print(s)

18-masala. Quyidagi ifodani hisoblovchi dastur tuzing.

import math
n=int(input("n sonini kiriting: "))
s=0
for i in range(1,n,1):
	s=s+i**2/(i+2)+math.sqrt(i)
print(s)

Xulosa.

Dasturlash bo’yicha ko’nikmalarni oshirish va mustahkamlashning eng yaxshi usullaridan biri dasturlashga oid masalalarni ishlashdir. Lekin, dasturlashni endi boshlagan dasturchilar yaxshi manbalarni topishga qiynalishadi. Men turli manbalardan olingan masalalardan foydalanib chiziqli, tarmoqlanuvchi va takrorlanuvchi jarayonlarning  dasturini python tilida yozib chiqdim. Bu masalalardan o'quvchilarni fan olimpiadalariga tayyorlash va to'garaklarni olib borishda foydalanaman. O'quvchilarim tuman, viloyat bosqichlarida faxrli o'rinlarni olishgan. Maqolada berilgan masalalardan python tilini o'rganishda yoki o'quvchilarga o'rgatishda foydalanishingiz mumkin.

 

Qolgan qismlari:

chiziqli masalalar;

shartli masalalar.