1-misol. To‘plamga yangi element qo‘shish:
Dastur kodi:
01 >>> vil={'Toshkent', 'Namangan', 'Jizzax'}
02 >>> vil.add('Navoiy')
03 >>> print(vil)
{'Navoiy', 'Namangan', 'Jizzax', 'Toshkent'}
Bunda:
(02) To‘plamga 'Navoiy' elementini qo‘shadi.
-------------------------------------------------------------
2-misol. To‘plamdagi elementni o‘chirish uchun element kaliti bilan ko‘rsatiladi:
Dastur kodi:
04 >>> vil.remove('Jizzax')
05 >>> print(vil)
{'Navoiy', 'Namangan', 'Toshkent'}
Bunda:
(04) 'Jizzax' qiymatli element to‘plamdan o‘chirildi.
-------------------------------------------------------------
3-misol. To‘plamdagi elementlar sonini aniqlash:
Dastur kodi:
06 >>> count=len(vil)
07 >>> print(count)
3
Bunda:
(06) To‘plamdagi elementlar sonini aniqlaydi va “count” ga tenglaydi.
(07) “count” ni ekranga chiqaradi.
-------------------------------------------------------------
To‘plam ustida amallar
Amallar
|
Tavsifi
|
A | B
|
A.union(B)
|
A va B to‘plamini birlashtirib, yangi to‘plam hosil qiladi.
|
A |= B
|
A.update(B)
|
B to‘plamdagi elementlarni A to‘plamga qo‘shadi
|
A & B
|
A.intersection(B)
|
A va B to‘plamlari kesishmasi, ya’ni ikkala to‘plam uchun umumiy bo‘lgan elementlarni oladi.
|
A &= B
|
A.intersection_update(B)
|
A to‘plamda B to‘plamda mavjud elementlarni qoldiradi.
|
A - B
|
A.difference(B)
|
A va B to‘plamlarning ayirmasi, ya’ni A to‘plamda mavjud va B to‘plamida mavjud bo‘lmagan elementlarini qaytaradi.
|
A -= B
|
A.difference_update(B)
|
A to‘plamdan B to‘plamda mavjud elementlarni o‘chiradi.
|
A <= B
|
A.issubset(B)
|
A to‘plami B to‘plamining qism to‘plami ekanligini tekshiradi.
|
A >= B
|
A.issuperset(B)
|
B to‘plami A to‘plamining qism to‘plami ekanligini tekshiradi.
|
A < B
|
A<=B and A!=B
|
A to‘plam B to‘plamdan kichik.
|
A > B
|
A>=B and A!=B
|
A to‘plam B to‘plamdan katta.
|
Dastur kodi:
01 >>> sets={0, 1, 2, 3}
02 >>> fset=frozenset({2, 3, 4})
03 >>> lists=[5, 6, 7]
04 >>> tuples=(7, 8, 9)
05 >>> sets.union(lists, tuples, fset)
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
06 >>> fset.union(sets, lists, tuples)
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
07 >>> sets | lists | tuples | fset
Traceback (most recent call last):
File "
", line 1, in
sets | lists | tuples | fset
TypeError: unsupported operand type(s) for |: 'set' and 'list'
08 >>> sets | frozenset(lists) | frozenset(tuples) | fset
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
09 >>> fset | set(lists) | sets | set(tuples)
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
Bunda:
(05) (06) Birlashganda, takrorlanuvchi elementlar e’tiborga olinmaydi
(07) Bitli operator yordamida birlashganda barcha ob‘ektlar to‘plam bo‘lishi kerak.
(08) (09) Olingan natijaning turi birinchi operandga bog‘liq.
-------------------------------------------------------------
Dastur kodi:
10 >>> sets={0, 1, 2, 3}
11 >>> fset=frozenset({3, 4, 7, 10})
12 >>> lists=[3, 5, 6, 7]
13 >>> tuples=(0, 1, 7, 8, 9)
14 >>> sets.update(lists, tuples)
15 >>> print(sets)
{0, 1, 2, 3, 5, 6, 7, 8, 9}
16 >>> sets.clear()
17 >>> print(sets)
set()
18 >>> sets.update(fset, tuples)
19 >>> print(sets)
{0, 1, 3, 4, 7, 8, 9, 10}
20 >>> sets.clear()
21 >>> print(sets)
set()
22 >>> sets |= lists | tuples
Traceback (most recent call last):
File "
", line 1, in
sets |= lists | tuples
TypeError: unsupported operand type(s) for |: 'list' and 'tuple'
23 >>> sets |= frozenset(lists) | set(tuples) | fset
24 >>> print(sets)
{0, 1, 3, 4, 5, 6, 7, 8, 9, 10}
Bunda:
(15) (18) Takroriy ketma-ketliklar e'tiborga olinmaydi.
(22) Bitli operator yordamida birlashganda barcha ob‘ektlar to‘plam bo‘lishi kerak.
-------------------------------------------------------------
Dastur kodi:
25 >>> sets={0, 1, 2, 3}
26 >>> fset=frozenset({0, 1, 2, 3, 4, 7})
27 >>> lists=[0, 1, 3, 5, 6, 7]
28 >>> tuples=(0, 1, 7, 8, 9)
29 >>> sets.intersection(lists)
{0, 1, 3}
30 >>> fset.intersection(sets, lists, tuples)
frozenset({0, 1})
31 >>> sets & lists & tuples & fset
Traceback (most recent call last):
File "
", line 1, in
sets & lists & tuples & fset
TypeError: unsupported operand type(s) for &: 'set' and 'list'
32 >>> sets & frozenset(lists) & frozenset(tuples) & fset
{0, 1}
33 >>> fset & set(tuples)
frozenset({0, 1, 7})
Bunda:
(29) (30) Takroriy ketma-ketliklar e'tiborga olinmaydi.
(31) Bitli operator yordamida barcha ob‘ektlar to‘plam bo‘lishi kerak.
-------------------------------------------------------------
Dastur kodi:
34 >>> sets={0, 1, 2, 3, 7}
35 >>> lists=[0, 1, 3, 5, 6, 7]
36 >>> tuples=(0, 3, 5, 7, 8, 9)
37 >>> sets.intersection_update(lists)
38 >>> print(sets)
{0, 1, 3, 7}
39 >>> sets.intersection_update(lists, tuples)
40 >>> print(sets)
{0, 3, 7}
41 >>> sets.clear()
42 >>> print(sets)
set()
43 >>> sets.intersection_update(lists, tuples)
44 >>> print(sets)
set()
45 >>> sets={0, 1, 2, 3, 7}
46 >>> sets &= lists & tuples
Traceback (most recent call last):
File "
", line 1, in
sets &= lists & tuples
TypeError: unsupported operand type(s) for &: 'list' and 'tuple'
47 >>> sets &= frozenset(lists) & set(tuples)
48 >>> print(sets)
{0, 3, 7}
Bunda:
(37) (39) Kesishma barcha ob'ektlarda qidiriladi.
(46) Bitli operator yordamida barcha ob‘ektlar to‘plam bo‘lishi kerak.
-------------------------------------------------------------
Dastur kodi:
49 >>> sets={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
50 >>> fset=frozenset({0, 1, 2, 3, 4})
51 >>> lists=[3, 5, 6, 7]
52 >>> tuples=(1, 7, 8, 9)
53 >>> sets.difference(lists, tuples)
{0, 2, 4, 10, 11}
54 >>> fset.difference(lists, tuples)
frozenset({0, 2, 4})
55 >>> sets - lists - tuples - fset
Traceback (most recent call last):
File "
", line 1, in
sets - lists - tuples - fset
TypeError: unsupported operand type(s) for -: 'set' and 'list'
56 >>> sets - frozenset(lists) - frozenset(tuples) - fset
{10, 11}
57 >>> fset - frozenset(lists)
frozenset({0, 1, 2, 4})
-------------------------------------------------------------
Dastur kodi:
58 >>> sets={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
59 >>> lists=[2, 3, 5, 6, 7, 11]
60 >>> tuples=(1, 7, 8, 9)
61 >>> sets.difference_update(lists)
62 >>> print(sets)
{0, 1, 4, 8, 9, 10}
63 >>> sets.difference_update(lists, tuples)
64 >>> print(sets)
{0, 4, 10}
65 >>> sets={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
66 >>> sets -= lists | tuples
Traceback (most recent call last):
File "
", line 1, in
sets -= lists | tuples
TypeError: unsupported operand type(s) for |: 'list' and 'tuple'
67 >>> sets -= frozenset(lists) | set(tuples)
68 >>> print(sets)
{0, 4, 10}
-------------------------------------------------------------
Dastur kodi:
69 >>> set_x={0, 1, 2, 3, 4}
70 >>> set_y={4, 0, 3, 1}
71 >>> set_x.issubset(set_y)
False
72 >>> set_y.issubset(set_x)
True
73 >>> set_x <= set_y
False
74 >>> set_y <= set_x
True
75 >>> set_x={0, 1, 2, 3, 4}
76 >>> list_y=[4, 0, 3, 1]
77 >>> set_x.issubset(list_y)
False
78 >>> set_x <= list_y
Traceback (most recent call last):
File "
", line 1, in
set_x <= list_y
TypeError: '<=' not supported between instances of 'set' and 'list'
79 >>> set_x <= set(list_y)
False
80 >>> list_y.append(2)
81 >>> set_x.issubset(list_y)
True
82 >>> set_x <= frozenset(list_y)
True
83 >>> list_y.append(5)
84 >>> set_x <= set(list_y)
True
85 >>> print(set_x)
{0, 1, 2, 3, 4}
86 >>> print(list_y)
[4, 0, 3, 1, 2, 5]
-------------------------------------------------------------
Dastur kodi:
87 >>> set_x={'march', 'feb', 'may'}
88 >>> set_y={'march', 'dec', 'feb', 'may'}
89 >>> set_x.issuperset(set_y)
False
90 >>> set_y.issuperset(set_x)
True
91 >>> set_x >= set_y
False
92 >>> set_y >= set_x
True
93 >>> set_x={'march', 'feb', 'may'}
94 >>> list_y=['march', 'dec', 'feb', 'may']
95 >>> set_x.issuperset(list_y)
False
96 >>> set_x >= list_y
Traceback (most recent call last):
File "
", line 1, in
set_x >= list_y
TypeError: '>=' not supported between instances of 'set' and 'list'
97 >>> set_x >= set(list_y)
False
98 >>> set_x.add('dec')
99 >>> set_x.issuperset(list_y)
True
100 >>> set_x >= frozenset(list_y)
True
101 >>> set_x.add('jul')
102 >>> set_x >= set(list_y)
True
103 >>> list_y.append(5)
104 >>> set_x >= frozenset(list_y)
False
105 >>> print(set_x)
{'may', 'march', 'dec', 'jul', 'feb'}
106 >>> print(list_y)
['march', 'dec', 'feb', 'may', 5]
-------------------------------------------------------------
Dastur kodi:
107 >>> set_x={4, 0, 3, 1}
108 >>> set_y={0, 1, 2, 3, 4}
109 >>> set_x <= set_y
True
110 >>> set_x != set_y
True
111 >>> set_x <= set_y and set_x != set_y
True
112 >>> set_x={0, 1, 2, 3, 4}
113 >>> set_y={4, 0, 3, 1}
114 >>> set_x <= set_y
False
115 >>> set_x <= set_y and set_x != set_y
False
-------------------------------------------------------------
Dastur kodi:
116 >>> set_a={'jan', 'feb', 'march'}
117 >>> set_b={'jan', 'feb', 'march', 'may'}
118 >>> set_a >= set_b
False
119 >>> set_a != set_b
True
120 >>> set_a >= set_b and set_a != set_b
False
121 >>> set_a={'jan', 'feb', 'march', 'may'}
122 >>> set_b={'jan', 'feb', 'march'}
123 >>> set_a >= set_b
True
124 >>> set_a >= set_b and set_a != set_b
True
-------------------------------------------------------------
Misol. To‘plamga yangi element qo‘shish:
Dastur kodi:
01 >>> viloyat={'Toshkent', 'Namangan', 'Jizzax'}
02 >>> >>> voha={'Jizzax', 'Qashqadaryo', 'Surxondaryo'}
03 >>> >>> a=viloyat & voha
04 >>> >>> print(a)
{'Jizzax'}
Bunda:
(01) 'viloyat' nomli to‘plam yaratildi.
(02) 'voha' nomli to‘plam yaratildi.
(03) 'viloyat' va 'voha' nomli to‘plamlar kesishmasini, ya’ni ikkala to‘plamda mavjud elementni chiqaradi.
-------------------------------------------------------------
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
AMALIY MASHG‘ULOT
Do'stlaringiz bilan baham: |