Ikkilik operatorlar
Pythonda quyidagi ikkilik (bitli) operatorlar ishlatiladi:
~ ikkilik inversiya. Har bir bit qarama qarshisiga o’zgaradi:
>>> х = 100 # 011 001 00
>>> х= ~х # 100 110 11
>>> x = 100 # 01100100
>>> у = 75 # 01001011
>>> z = х & у # 01000000
>>> "{0 :b } & {l: b}={2:b}".format (х, у , z)
'1100100 & 1001011 = 1000000 '
>>> х = 100 # 01100100
>>> у = 75 # 01001011
>>> z = х | у # 01101111
>>> "{0:b } | {l: b } = {2: b }".format (x, у, z )
'1100100 | 1001011 = 11011 11 '
^ - ikkilik istisnoli YOKI:
>>> х = 100 # 01100100
>>> у = 250 # 11111010
>>> z = х ^ у # 10011110
>>> "{ 0: b} ^{l :b} = {2 :b} ".format (x, у, z )
'1100100 ^ 11111010 = 1001111 0 '
<< – chapga siljitish –ikkilik ko’rinishdagi soni chapgabir yoki bir nechta o’ringa siljitish va o’ng tomonni nol bilan to’ldirish:
>>> х = 100 # 01100100
>>> у = х << 1 # 11001000
>>> z = у << 1 # 10010000
>>> k = z << 2 # 01000000
>> – o’ngga siljitish – ikkilik ko’rinishdagi sonni o’ngga bir yoki bir nechta o’ringa siljitish va agar son musbat bo’lsa, chap tomonni nol bilan to’ldirish:
>>> х = 100 # 01100100
>>> у = х >> 1 # 00110010
>>> z = у >> 1 # 00011001
>>> k = z >> 2 # 00000110
Agar son manfiy bo’lsa, chap tomondagi razryadar bir bilan to’ldiriladi:
>>> х = -127 # 10000001
>>> у = х >> 1 # 11000000
>>> z = у >> 2 # 11110000
>>> k = z << 1 # 11100000
>>> m = k >> 1 # 11110000
Sonli ketma-ketliklar bilan ishlash operatorlari
Sonli ketma-ketliklar bilan ishlash operatorlari quyidagicha:
>>> print ("1-satr" + "2-satr") # satrlar konkatenasiyasi
1-satr2-satr
>>> [1, 2, 3] + [4, 5, 6 ) # ro’yxatlar
[1, 2, 3, 4, 5, 6]
>>> (1, 2, 3) + ( 4, 5, 6) # Kortejlar
(1, 2, 3, 4, 5, 6)
>>> "s" * 20 # Satr
'ssssssssssssssssssss'
>>> [1, 2]* 3 # Ro’yxat
[1, 2, 1, 2, 1, 2]
>>> (1, 2) * 3 # Kortej
(1, 2, 1, 2, 1, 2)
in – tegishlilikka tekshirish. Agar element sonli satr tarkibida bo’lsa natija sifatida True ni qaytaradi:
>>> "satr" in "Qidirish uchun satr" # Satr
True
>>> "2-satr" in "Qidirish uchun satr" # Satr
False
>>> 2 in [1, 2, 3], 4 in [1, 2, 3] # Ro’yxat
(True , False)
>>> 2 in (1 , 2, 3), 6 in (1, 2, 3) # Kortej
(True , False)
not in – tegishli emaslikka tekshirish. Agar element sonli ketma-ketlik tarkibida bo’lmasa True natija qaytaradi:
>>> "satr" not in "Qidirish uchun satr" # Satr
False
>>> "2-satr" not in "Qidirish uchun satr" # Satr
True
>>> 2 not in [1, 2, 3], 4 not in [1, 2,] # Ro’yxat
(False, True )
>>> 2 not in (1, 2, 3), 6 not in (1 , 2, 3) # Kortej
(False, True)
Do'stlaringiz bilan baham: |