set
- to’plam turi
Kirish
set
turi matematikadagi to’plamlarni ifodalayditartiblanmagan
o’zgartirib bo’lmaydigan
indeks operatori mavjud emas
takrorlanuvchi elementlar mavjud bo’lmaydi
faqat heshlash mumkin bo’lgan turlarni saqlaydi
[91]:
thisset = {"apple", "banana", "cherry"}
print(thisset)
{'cherry', 'banana', 'apple'}
[92]:
thisset = {"apple", "banana", "cherry"}
thisset[0] = 'orange'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[92], line 2
1 thisset = {"apple", "banana", "cherry"}
----> 2 thisset[0] = 'orange'
TypeError: 'set' object does not support item assignment
[94]:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
{'cherry', 'banana', 'apple'}
[95]:
thisset = {"apple", "banana", "cherry", True, 1, 2}
print(thisset)
{True, 2, 'cherry', 'banana', 'apple'}
[96]:
thisset = {"apple", "banana", "cherry", 1, True, 2}
print(thisset)
{1, 2, 'cherry', 'banana', 'apple'}
[100]:
thisset = {"apple", "banana", "cherry", True, 1, 2, 0, False, ''}
print(thisset)
{0, True, 2, 'cherry', '', 'banana', 'apple'}
[104]:
thisset = {"apple", "banana", "cherry", True, 1, 2, 0, False, '', (2, 3)}
print(thisset)
{0, True, 2, 'cherry', '', 'apple', (2, 3), 'banana'}
Mutable va immutable
Mutable
list
Immutable
tuple
,str
,set
, etc
[105]:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
3
[19]:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
[106]:
set1 = {"abc", 34, True, 40, "male"}
[107]:
myset = {"apple", "banana", "cherry"}
print(type(myset))
<class 'set'>
[109]:
thisset = set(("apple", "banana", "cherry", 'apple')) # note the double round-brackets
print(thisset)
{'cherry', 'banana', 'apple'}
[113]:
thisset = {0, 'orange', "apple", "banana", "cherry", 1}
for x in thisset:
print(x)
0
1
cherry
orange
banana
apple
[116]:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
True
[117]:
thisset = {"apple", "banana", "cherry"}
print("banana" not in thisset)
False
[119]:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
{'orange', 'cherry', 'banana', 'apple'}
[121]:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
{'pineapple', 'papaya', 'mango', 'cherry', 'banana', 'apple'}
[122]:
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
{'cherry', 'orange', 'kiwi', 'apple', 'banana'}
[125]:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
{'cherry', 'apple'}
[126]:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
{'cherry', 'apple'}
[127]:
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
cherry
{'banana', 'apple'}
[128]:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
set()
[129]:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[129], line 5
1 thisset = {"apple", "banana", "cherry"}
3 del thisset
----> 5 print(thisset)
NameError: name 'thisset' is not defined
set
larni qo’shish
union()
update()
intersection()
difference()
symmetric_difference()
[131]:
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
{1, 2, 'a', 3, 'b', 'c'}
[132]:
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1 | set2
print(set3)
{1, 2, 'a', 3, 'b', 'c'}
[133]:
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}
myset = set1.union(set2, set3, set4)
print(myset)
{1, 2, 'a', 3, 'cherry', 'apple', 'b', 'bananas', 'John', 'c', 'Elena'}
[134]:
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}
myset = set1 | set2 | set3 |set4
print(myset)
{1, 2, 'a', 3, 'cherry', 'apple', 'b', 'bananas', 'John', 'c', 'Elena'}
[136]:
x = {"a", "b", "c"}
y = [1, 2, 3]
z = x.union(y)
print(z)
{1, 2, 'a', 3, 'b', 'c'}
[137]:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
{1, 2, 'a', 3, 'b', 'c'}
[138]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.intersection(set2)
print(set3)
{'apple'}
[139]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 & set2
print(set3)
{'apple'}
[140]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set1.intersection_update(set2)
print(set1)
{'apple'}
[141]:
set1 = {"apple", 1, "banana", 0, "cherry"}
set2 = {False, "google", 1, "apple", 2, True}
set3 = set1.intersection(set2)
print(set3)
{False, 1, 'apple'}
[143]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.difference(set2)
print(set3)
{'cherry', 'banana'}
[144]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 - set2
print(set3)
{'cherry', 'banana'}
[145]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set1.difference_update(set2)
print(set1)
{'cherry', 'banana'}
[146]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.symmetric_difference(set2)
print(set3)
{'microsoft', 'cherry', 'banana', 'google'}
[50]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 ^ set2
print(set3)
{'microsoft', 'cherry', 'banana', 'google'}
[51]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set1.symmetric_difference_update(set2)
print(set1)
{'microsoft', 'cherry', 'banana', 'google'}
dict
- lug’at turi
[147]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
[148]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Ford
[149]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
[150]:
print(len(thisdict))
3
[152]:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
[153]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(type(thisdict))
<class 'dict'>
[154]:
thisdict = dict(name = "John", age = 36, country = "Norway")
print(thisdict)
{'name': 'John', 'age': 36, 'country': 'Norway'}
[159]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print(x)
Mustang
[164]:
x = thisdict.get("model1")
print(x)
None
[165]:
x = thisdict.keys()
x
[165]:
dict_keys(['brand', 'model', 'year'])
[167]:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
dict_keys(['brand', 'model', 'year'])
dict_keys(['brand', 'model', 'year', 'color'])
[168]:
car
[168]:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}
[169]:
x = thisdict.values()
print(x)
dict_values(['Ford', 'Mustang', 1964])
[170]:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 2020])
[171]:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["color"] = "red"
print(x) #after the change
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 1964, 'red'])
[172]:
x = thisdict.items()
print(x)
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
[173]:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2020)])
[174]:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["color"] = "red"
print(x) #after the change
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964), ('color', 'red')])
[175]:
for key, value in car.items():
print(key, value)
brand Ford
model Mustang
year 1964
color red
[179]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Yes, 'model' is one of the keys in the thisdict dictionary
[180]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
[182]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020, 'brand': 'Tiko', 'color': 'red'})
print(thisdict)
{'brand': 'Tiko', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
[183]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
{'brand': 'Ford', 'year': 1964}
[184]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang'}
[185]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
{'brand': 'Ford', 'year': 1964}
[76]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[76], line 7
1 thisdict = {
2 "brand": "Ford",
3 "model": "Mustang",
4 "year": 1964
5 }
6 del thisdict
----> 7 print(thisdict) #this will cause an error because "thisdict" no longer exists.
NameError: name 'thisdict' is not defined
[77]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
{}
[186]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
brand
model
year
[188]:
for x in thisdict:
print(x, thisdict[x])
brand Ford
model Mustang
year 1964
[189]:
for x in thisdict.values():
print(x)
Ford
Mustang
1964
[190]:
for x in thisdict.keys():
print(x)
brand
model
year
[191]:
for x, y in thisdict.items():
print(x, y)
brand Ford
model Mustang
year 1964
[192]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
[193]:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
[87]:
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
[88]:
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
[194]:
print(myfamily["child2"]["name"])
Tobias
[195]:
for x, obj in myfamily.items():
print(x)
for y in obj:
print(y + ':', obj[y])
child1
name: Emil
year: 2004
child2
name: Tobias
year: 2007
child3
name: Linus
year: 2011