Num 1 = float (input("brinchi sonni kriting:")) num 2 = float (input


Download 49.39 Kb.
Sana01.06.2020
Hajmi49.39 Kb.
#112667
Bog'liq
Python


num_1 = float (input("brinchi sonni kriting:"))
num_2 = float (input("ikkinchi sonni kriting:"))
res = num_1 + num_2
print ("natija:",res)

JAVOBI


brinchi sonni kriting:>? 23

ikkinchi sonni kriting:>? 23

natija: 46.0

Res = input ("bror narsa kriting")


Res *= 5
print (Res)
JAVOBI

bror narsa kriting>? ab

ababababab
input () ------ kritish

print () ------ chiqarish

float ------ butunli son [ 23,1 ]

int ------ istalgan son manfiy musbat [ -2 -1 0 1 2 ]

or ------ yoki uni yoki buni

and ------ ikkalasiyam ishlash kerak

not ------ yoq manosini beradi

num = input ("raqam kriting")


if int (num) > 0:
print("noldan katta raqam kiritdingiz")
elif int (num) < -10:
print("siz -10 dan kam sonni kirittingiz")
else:
print("0 dan kichik va -10 dan katta raqamni kiritdingiz")
print("hammasi yaxshi")
raqam kriting>? -3

0 dan kichik va -10 dan katta raqamni kiritdingiz

hammasi yaxshi

Bunda “if” operatorida shart beramiz va “elif” operatori bilan shartlarni davom ettirishimiz mumkin

“else” operatori tugatish manosini bildiradi.

num = input ("raqam kriting")


if int (num) > 0:
if int (num) > 10:
print("10 dan katta raqam kiritdingiz")
if int (num) >= 50:
print("50 dan katta raqam kiritdingiz")
else:
print("siz -10 dan katta sonni kirittingiz")
elif int (num) < -10:
print("siz -10 dan kichkina sonni kirittingiz")
else:
print("siz -10 dan katta sonni kirittingiz")
print("hammasi yaxshi")
raqam kriting>? 59

10 dan katta raqam kiritdingiz

50 dan katta raqam kiritdingiz

hammasi yaxshi

Bu kod ichma ich yozlgan “if” lar

name = input ()


A = 'Yes' if name != "Test" else 'No'
print(A)
soz kriting:>? Otabek

Yes


Bu yerda kritgan sozimiz “ Test ” ga teng bosa ‘Yes’ aks xolda ‘No’ chqadi

for j in 'salomalekum':


if j == 's':
continue
print(j*3,end = '')
aaalllooommmaaallleeekkkuuummm

for j in 'salomalekum':


if j == 's':
break
print(j*3,end = '')
for j in 'salomalekum':
if j == 's':
break
else:
print("harflar va sozda yoq")

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)

Orange Banana Cherry

x = "awesome"
print("Python is " + x)
Python is awesome

+ x orqali biz belgilab olgan qiymatimizni qowib olamiz

x = "Python is "
y = "awesome"
z = x + y
print(z)
Python is awesome

a = float(input('a ni kiriting = '))


b = float(input('b ni kiriting = '))
c = float(input('c ni kiriting = '))
D = b*b-4*a*c
if D>0:
x1=(-b+pow(D,1/2))/2*a
x2 = (-b - pow(D, 1 / 2)) / 2 * a
print('X1 = ', x1)
print('X2 = ', x2)
elif D==0:
x12=-b/2*a
print('X12 = ', x12)
else:
print('Diskriminant 0 dan kichkina, tenglama yechimga ega emas')
a ni kiriting = 3

b ni kiriting = 5

c ni kiriting = 2

X1 = -6.0

X2 = -9.0

lis = [a + b for a in 'bek' if a != 'b' for b in 'mun' if b != 'm']


print(lis)

lis.append(58)


print(lis)

m = [25, 63, 'fa']


lis.extend(m)
print(lis)

lis.insert(5, 'hz')


lis.append(58)
print(lis)

lis.remove(58)


print(lis)

lis.pop(5)


print(lis)

print(lis.index('hz'))


print(lis.count(58))

t = [25, 45, 12, 158, 75, 1]


t.sort()
print(t)

t.reverse()


print(t)

t.clear()


print(t)

['eu', 'en', 'ku', 'kn'] === a va b dagi qiymatlarni faqat 2 tasini qoshadi

['eu', 'en', 'ku', 'kn', 58] === lis.append(58) ----lis da berlgan qiymat oxriga qoshad

['eu', 'en', 'ku', 'kn', 58, 25, 63, 'fa'] === lis.extend(m)---lis ni oxriga belglab toda qlb qoshadi

['eu', 'en', 'ku', 'kn', 58, 'hz', 25, 63, 'fa', 58] ===lis.insert(5,’hz’)---5-joyga ‘hz’ ni qoshadi

['eu', 'en', 'ku', 'kn', 'hz', 25, 63, 'fa', 58] ===lis.remove(58)--shu belglaganimizni 1-kegani ochradi

['eu', 'en', 'ku', 'kn', 'hz', 63, 'fa', 58] ===lis.pop(5)-shu belglaganimizni boshidan sanab ochrad

4 ===print(lis.index(‘hz’))---nechinchi qatordaligin korsatad

1 ===print(lis.count(58))---shuni nechtaligini korsatadi

[1, 12, 25, 45, 75, 158] ===t.sort() --- kichkinadan kattaga qarab saralaydi

[158, 75, 45, 25, 12, 1] ===t.reverse() ---- kattadan kichkinaga qarab saralaydi

[] ===t.clear() --- xammasini ochrib tozalaydi

l = [34, 'sd', 56, 34.34]
i=0
while i < 4 :
print (l[i])
i += 1
item [START:STOP:STEP]
34

sd

56



34.34

item [START:STOP:STEP] === biz buyerda shuni organamiz pasda misollar blan

l = [34, 'sd', 56, 34.34]
i=0
while i < 4 :
print (l[i])
i += 1
print(l[:3])

#item [START:STOP:STEP]

34

[34, 'sd', 56]



sd

[34, 'sd', 56]

56

[34, 'sd', 56]



34.34

[34, 'sd', 56]

item [START:STOP:STEP] ==== bu formulani ishlatb kordik

STOP ==== bunga nechi bersak shuncha natija cqaradi

‘ item ’---‘ l ‘ , ‘ START ’---‘ bosh ‘ , ‘ STOP ’ --- ‘ 3 ‘ , ‘ STEP ‘ --- ‘ bosh’ ==== shular orniga qoyiladi

l = [34, 'sd', 56, 34.34]


i=0
while i < 4 :
print (l[i])
i += 1
print(l[::2])
#item [START:STOP:STEP]
34

[34, 56]


sd

[34, 56]


56

[34, 56]


34.34

[34, 56]


item [START:STOP:STEP] ==== bu formulani ishlatb kordik

STEP ==== bunga misol ‘ 2 ‘ bersak xar ikkinchida turganni olb takrorlanb boraveradi

‘ item ’---‘ l ‘ , ‘ START ’---‘ bosh ‘ , ‘ STOP ’ --- ‘ bosh ‘ , ‘ STEP ‘ --- ‘ 2’ ==== shular orniga qoyiladi

a = (43, 56, 45.23, 'd')


b = [43, 56, 45.23, 'd']
c = {43, 56, 45.23, 'd'}
print (a.__sizeof__())
print (b.__sizeof__())
print (c.__sizeof__())

28 ------ a = () ------ kortej ----index orqali hisoblaydi xech narsa kritib bomaydi

36 ------ b = [] ------ royxat ---- index orqali kritiladi kortej blan br xil joyidan farq qladi

100 ------ c = {} ------slovar ---- bunda kalit soz blan kritiladi va chiqariladi

__sizeof__() ==== bu ‘ a ‘ va ‘ b ‘ da turganlarni razmerini korsatadi

d = dict (short='dict', longer='dictionary')


d['short']=234
print(d)

{'short': 234, 'longer': 'dictionary'}

d = dict.fromkeys (['a', 'b', 'c'], 1)
print(d)

{'a': 1, 'b': 1, 'c': 1}

d = {'test' : 1}
print(d['test'])

1

d = {a : a ** 2 for a in range(7)}


print(d)

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}

person = {'name' : {'last_name' : 'Qobilov', 'first_name' : 'Jasur', 'midddle_name' : 'Ivanovich'},
'adress' : ['Toshkent', 'Mirobod'], 'phone' : {'home_phone' : '0072272'}}
print(person['name']['first_name'])
Jasur

person = {'name' : {'last_name' : 'Qobilov', 'first_name' : 'Jasur', 'midddle_name' : 'Ivanovich'},


'adress' : ['Toshkent', 'Mirobod','Yunusabot'], 'phone' : {'home_phone' : '0072272'}}
print(person['adress'][2])
Yunusabot

person = {'name' : {'last_name' : 'Qobilov', 'first_name' : 'Jasur', 'midddle_name' : 'Ivanovich'},


'adress' : ['Toshkent', 'Mirobod','Yunusabot'], 'phone' : {'home_phone' : '0072272'}}
print(person.keys())
dict_keys(['name', 'adress', 'phone'])

a = {23 : 34}


print(type(a))
print(a)
b = {23, 34}
print(type(b))
print(b)
c = {i ** 2 for i in range(10)}
print(type(c))
print(c)

{23: 34}


{34, 23}


{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}

a = { 32, 45, 43.23, 76}
x = 45
print (x in a)
True

Bu yerda ‘ a ‘ ni ichida ‘ x ‘ bor yoki yoqligini qdryabdi bor bolsa ‘ True’ aks xolda false qiymat qaytadi

a = { 32, 45, 43.23, 76 }
x = {67, 12, 90}
print (a.isdisjoint(x))
True

Bu yerda ‘ a ‘ ni ichida ‘ x ‘ bor yoki yoqligini qdryabdi“yoq”bolsa ‘True’ aks xolda False qiymat qaytadi

a = {32, 45, 43.23, 76}
print(len(a))

4

Buyerda ‘ a ‘ ni ichida nechta qiymat borligini sanaydi



a = { 3, 5, 3.2, 7 }
x = { 16, 12, 19,5 }
a.intersection_update(x)
print (a)

{ 5 }


Ikkalasidayam borini chiqaradi

a = { 3, 5, 8, 7, 15 }


x = { 17, 12, 19, 15 }
a.difference_update(x)
print (a)

{3, 5, 7, 8}

Bunda ‘ x ‘ da yogini chiqaradi ‘ a ‘

a = { 3, 5, 8, 7, 15 }


x = { 17, 12, 19, 15 }
a.symmetric_difference_update(x)
print (a)

{3, 5, 7, 8, 12, 17, 19}

Bunda ikkalasidayam bor sonlar chiqmaydi

a = { 3, 5, 8, 7, 15 }


a.remove(15)
print (a)

{3, 5, 7, 8}

Bunda belglagan sonimiz chiqmaydi

a = { 3, 5, 8, 7, 15 }


a.discard(3)
print (a)

{5, 7, 8, 15}

Bunda belglagan sonimiz chiqmaydi

a = { 3, 5, 8, 7, 15 }


a.clear()
print (a)

set()


Yani xammasini tozalab ochirvoradi

a = { 3, 5, 15 }


x = { 12, 19, 15 }
a.update(x)
print (a)

{3, 19, 5, 12, 15}

Bunda ikkalasidan xammasini oladi faqat ikkalasida br xil son bolsa bittasini oladi

def func ( x, a ):


return x + a
print (func('hello','world'))
helloworld

Buyerda “ def ” funksiya xisoblanadi ‘ func ‘ ni orniga xoxlagan nom bershimiz mumkin

def func ( x ):
def add ( a ):
return x + a
return add
test = func(100)
print (test(200))

300


Bu ichma ich ochilgan funksiya

def func ( r, w, y = 2 ):


res = r + w
res *= y
return res
print (func(2, 4))

12

def func ( r, w, y = 2 ):


res = r + w
res *= y
return res
print (func(2, 4, 3))

18

def func ( **args):


return args
print (func(short = 'dict', longer = 'dictionary'))
add = lambda x, y: x*y
print (add(1, 5))
{'short': 'dict', 'longer': 'dictionary'}

5

def func ( **args):


return args
print (func(short = 'dict', longer = 'dictionary'))
add = lambda x, y: x*y
print (add(2, 5))
print (add('q', 5))
print ((lambda x, y: x*y)(2,6))
{'short': 'dict', 'longer': 'dictionary'}

10

qqqqq



12

def func ( *args):


return args
print (func('sd', 45.45, 3))
('sd', 45.45, 3)

x = int(input('brinchi sonni kriting = '))


y = int(input('ikkinchi sonni kriting = '))
try:
res = x/y
except ZeroDivisionError:
print("siz 0 raqamini kritdingiz")
res = 0
print(res)

brinchi sonni kriting = 1

ikkinchi sonni kriting = 0

siz 0 raqamini kritdingiz

0

try:
x = int(input('raqam kriting = '))


except ValueError:
print("siz raqam kritmadingiz")
x = 0
print(x)

raqam kriting =

siz raqam kritmadingiz

0

try:


x = int(input('raqam kriting = '))
except ValueError:
print("siz raqam kritmadingiz")
x = 0
try:
y = int(input('raqam kriting = '))
except ValueError:
print("siz raqam kritmadingiz")
y = 0
try:
res = x/y
except ZeroDivisionError:
print("siz 0 raqamini kritdingiz")
res = 0
print(res)

raqam kriting =

siz raqam kritmadingiz

raqam kriting = 1

0.0

try:
x = int(input('raqam kriting = '))


except ValueError:
print("siz raqam kritmadingiz")
x = 0
try:
y = int(input('raqam kriting = '))
except ValueError:
print("siz raqam kritmadingiz")
y = 0
else:
print("hammasi togri")
finally:
print("100% tugadi")
try:
res = x/y
except ZeroDivisionError:
print("siz 0 raqamini kritdingiz")
res = 0
print(res)

raqam kriting = 3

raqam kriting = 5

hammasi togri

100% tugadi

0.6


dict.fromkeys([seq], value)

dict.items()

dict.keys()

dict.popitem()

dict.setdefault(key , default)

dict.update([other])

dict.values()

dict.clear()

dict.copy()

a = int(input("brinchi sonni kriting = "))


b = int(input("ikkinchi sonni kriting = "))
N = []
for i in range(a,b):
N.append(i**2)
print(sum(N))

brinchi sonni kriting = 1

ikkinchi sonni kriting = 5

30

A dan B gacha bolgan barcha butun son kvadratlarning yig’indisini chiqaruvchi pragramma.



range(a,b): -------------- a dan b gacha butun sonlarni olib beradi

N.append(i**2) -------------- qoshib keturadi

Print(sum(N)) -------------- yig’indi

x = 5
print(type(x))


Bu turini aniqlaw , va pasdagilar turlari



Matn turi:

Str

Turlari soni:

intfloatcomplex

Navbati turlari:

listtuplerange

Xaritalash turi:

Dict

Sozlash turlari:

setfrozenset

Boolean turi:

Bool

Ikkilik turlari:

bytesbytearraymemoryview

x = str("Hello World str

x = int(20) int



x = float(20.5) float

x = complex(1j) complex



x = list(("apple", "banana", "cherry")) list

x = tuple(("apple", "banana", "cherry")) tuple



x = range(6) range

x = dict(name="John", age=36) dict

x = set(("apple", "banana", "cherry)) set

x = frozenset(("apple", "banana", "cherry")) frozenset



x = True bool

x = bool(5) bool

x = b"Hello" bytes

x = bytes(5) bytes

x = bytearray(5) bytearray

x = memoryview(bytes(5)) memoryview

Python raqamlari 3 xil kornishda boladi

bir xil boshqasiga aylantirish:

x = 1 # int


y = 2.8 # float
z = 1j # complex

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)

print(a)
print(b)


print(c)

print(type(a))


print(type(b))
print(type(c))

1.0


2

(1+0j)







Download 49.39 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling