Satr (string) turi va uning funksiyalari. Dictionary ( lug’at) turi va uning funksiyalari. Funksiya yaratish va unga misollar
Download 54.87 Kb.
|
2-yakuniy
- Bu sahifa navigatsiya:
- Natija
- Natija: Ford Mustang 1964 Lug’at uzunligi: print(len(thisdict)) Natija
- Natija: {} 3.
Lug’at yaratish:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) Natija: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} Lug’atda elemnetga murojat: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict["model"] print(x) Natija: Mustang Get()-funksiyasi: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict.get("model") print(x) Natija: Mustang Lug’atda qiymatni uzgartisrish: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["year"] = 2018 print(thisdict) Natija: {'brand': 'Ford', 'model': 'Mustang', 'year': 2018} Lug’at yaratish: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) Natija: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} Lug’atda qiymatlarni chop etish: for x in thisdict.values(): print(x) Natija: Ford Mustang 1964 Lug’at uzunligi: print(len(thisdict)) Natija: 3 Lug’atga element qushish: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"] = "red" print(thisdict) Natija: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'} Lug’atda nusxalash: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = thisdict.copy() print(mydict) Natija: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} Lug’atda pop va popitem uchiish funksiyalari: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.pop("model") print(thisdict) thisdict.popitem() print(thisdict) Natija: {'brand': 'Ford', 'year': 1964} {'brand': 'Ford'} Lug’atda clear car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car.clear() print(car) Natija: {} 3. Funksiya – bu ko'p marta ishlatiladigan dastur bo'lagi. Funksiyalar ma'lum buyruqlar blokini ko'rsatilgan nom bilan saqlash va shu blokni dasturning istalgan joyida, istalgan miqdorda bajarish imkonini beradi. Biz oldingi darslarimizda len va range Python funksiyalari bilan tanishgan edik. Funksiyalar def zahira so'zi orqali aniqlanadi. Bu so'zdan so'ng funksiya nomi, undan so'ng qavs va shu qavs ichida bir necha o'zgaruvchilarni ko'rsatish mumkin bo'ladi va oxirida ikki nuqta (:) yoziladi. Shulardan so'ng funksiyani tashkil qiluvchi buyruqlar bloki yoziladi Funksiyalarga misollar keltiraylik: #mergesort def kamsort(ziyo): if len(ziyo)>1: mid=len(ziyo)//2 l=ziyo[:mid] r=ziyo[mid:] kamsort(l) kamsort(r) i=j=k=0
while i ziyo[k]=l[i] i+=1
else: ziyo[k]=r[j] j+=1 k+=1
while i ziyo[k]=l[i] i+=1 k+=1
while j ziyo[k]=r[j] j+=1 k+=1
def men(ziyo): for i in range(len(ziyo)): print(ziyo[i],end=" ") print()
ziyo=[16,49,85,24,13,56,7,44] print("Kiritilgan massiv !!!",end="\n") men(ziyo) kamsort(ziyo) print("Tartiblangan massiv !!!",end="\n") men(ziyo) Kiritilgan massiv !!! 16 49 85 24 13 56 7 44 Tartiblangan massiv !!! 7 13 16 24 44 49 56 85 # Quicksort def partition(ziyo,low,high): i = ( low-1 ) pivot =ziyo[high] for j in range(low , high): if ziyo[j] < pivot: i = i+1 ziyo[i],ziyo[j] = ziyo[j],ziyo[i] ziyo[i+1],ziyo[high] = ziyo[high],ziyo[i+1] return ( i+1 ) def quickSort(ziyo,low,high): if low < high: pi = partition(ziyo,low,high) quickSort(ziyo, low, pi-1) quickSort(ziyo, pi+1, high) ziyo= [12,56,48,7,5,19,45,32,59,14] n = len(ziyo) for i in range(n): print (ziyo[i],end=" ") quickSort(ziyo,0,n-1) print(" ") print ("Berilgan massivni o'sish tartibida !!!") for i in range(n): print (ziyo[i],end=" ") 12 56 48 7 5 19 45 32 59 14 berilgan massivni o'sish tartibida !!! 5 7 12 14 19 32 45 48 56 59 #BINARY SEARCH print("Qidirgan elementizni kiriting !!!") def bsearch(ziyo, l, r, x): if r >= l: mid = l + (r - l) // 2 if ziyo[mid] == x: return mid elif ziyo[mid] > x: return bsearch(ziyo, l, mid - 1, x) else: return bsearch(ziyo, mid + 1, r, x) else: return -1 ziyoda= [2, 3, 4, 10, 15, 22, 30, 35, 40] x = int(input("n=")) m= bsearch(ziyoda, 0, len(massiv) - 1, x) if m!= -1: print("Kiritgan elementiz nomeri !!!") print(m+1) else: print("Bunday element yuq !!!") qidirgan elementizni kiriting !!! n=2
kiritgan elementiz nomeri !!! 1 Download 54.87 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling