# դատարկ tuple ստեղծել
= ()
tup # կամ
= tuple()
tup
print(tup)
06 Tuple, Set, Dictionary
լուսանկարի հղումը, Հեղինակ՝ Naira Babayan
Set
the Controls for the Heart of the Sun
(c) Pink Floyd
🎦 Տեսադասեր + լրացուցիչ
ToDo 1. Տեսություն 2025
2. Տեսություն 2023 (ToDo)
3. Գործնական 2025
4. Գործնական 2023 (ToDo)
5. Որոշ տնայինների լուծումներ (ToDo)
Google Forms ToDo
📚 Նյութը
Tuple-ներ
Ըստ էության tupleը նույն listնա, որին որ չի կարելի փոփոխել (immutable են)
tuple-ի ստեղծում
# էլեմենտներով tuple ստեղծել
= (1, "ողջույն", True)
tup = 1, "ողջույն", True
tup
print(tup)
# եթե լիստ ունենք կարանք դարձնենք իրան tuple, երևի գուշակում եք թե ոնց՝
= [1, "ողջույն", True]
lst = tuple(lst)
tup print(tup)
# եթե ուզում ենք tupleի էլեմենտները բաժանենք տարբեր փոփոխականների մեջ, կարանք անենք՝
print(tup)
= 15, 509
a, b
= tup
a, b, c print(a, b, c, sep="\n")
tuple(i**2 for i in range(10))
# եթե զուտ (i**2 for i in range(10)) գրենք կսարքենք generator կոչվող բան, որը հետո ենք անցնելու
Tupleին էլեմենտ ավելացնել
1) tup.append(
"asdas"[0] = "A"
# tup.append("պանիր")
= (15, 508)
tup print(tup[0])
0] = 12
tup[print(tup)
- immutable - string, tuple
- mutable - list
0] = 0 # TypeError tup[
ճարներս ինչ, պետքա նոր tuple սարքենք
= tup + ("ժարիտ", 509)
new_tuple print(new_tuple)
indexing / slicing (նույն ձև ոնց listը)
print(new_tuple[1]) # prints: "ողջույն"
print(new_tuple[1:4]) # prints: ('ողջույն', True, 'ժարիտ')
print(new_tuple[-1]) # Output: 509
ցիկլ պտտվելը (լրիվ նույն ձև)
for element in new_tuple:
print(element)
max((1,2,3))
tuple(i**2 for i in range(10))
Առավելություններ
- tuple-ների հետ ավելի ապահովա, որովհետև կարանք վստահ լինենք որ ոչ մեկ մեր tuple-ը չի փոխի
- հիշողության տեսանկյունից ավելի օպտիմալ են
set-եր (բազմություններ)
= set() # my_set = {}
my_set
print(type(my_set))
# my_set = {}
# print(type(my_set))
= {4, 3, 3, 2}
my_set print(my_set)
= set()
my_set
for _ in range(5):
input()) # append-ի փոխարեն add
my_set.add(print(my_set)
for i in my_set:
print(i)
= {}
my_set1 print(type(my_set1)) # զգույշ ենք լինում
էլմենտ ավելացնել / հեռացնել
= {4, 3, 3, 2}
my_set print(my_set)
print(my_set)
# my_set.append()
5) # ոչ թե գրում ենք my_set = my_set.add(4)
my_set.add(print(my_set)
3)
my_set.remove(print(my_set)
print(my_set.pop())
print(my_set)
for i in my_set:
print(i)
Բազմությունների հետ գործողություններ
= {1, 2, 3, 4}
set1 = {3, 4, 5, 6}
set2
# Միավորում
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5, 6}
# կամ էլ
# set1 | set2 (կամ) # caps lock + right shift-ի կողքը 6
# Հատում
print(set1.intersection(set2)) # Output: {3, 4}
# կամ էլ
# set1 & set2 (և) # uppercase 6
# Տարբերություն
print(set1.difference(set2)) # Output: {1, 2}
# կամ էլ
# print(set1 - set2)
# Սիմետրիկ տարբերություն
print(set1.symmetric_difference(set2)) # Output: {1, 2, 5, 6}
# կամ էլ
# set1 ^ set2 # uppercase 6
Set comparison
= {1, 2, 3, "a", "b"}
a = {2, 1, 3, "b"}
b
print(a >= b)
# >, >=, <, <=
Հերթականություն
forum-ում քննարկում set-երի հերթականության վերաբերյալ
= ['պանիր', 'փիղ', 11, 12]
a print(a)
print(set(a))
set(a)[0]
print(a)
# print(set(a)[0])
print()
print(list(set(a)))
print(list(set(a))[0])
= {1, 4, 4, 3, 2}
my_set
for element in my_set:
print(element)
set comprehension
-5, 6 թվերի քառակուսիների բազմությունը ստեղծել
= []
lst
for i in range(-5, 6):
**2)
lst.append(iprint(lst)
= [i**2 for i in range(-5,6)]
lst print(lst)
= set() # s = {}
s
for i in range(-5, 6):
**2)
s.add(i# print(s)
print(s)
- list
[] - tuple
() - set {}
= [i**2 for i in range(-5,6)]
s print(s)
= tuple(i**2 for i in range(-5,6))
s print(s)
= {i**2 for i in range(-5,6)}
s print(s)
Կարանք հանգիստ in, sum len ․․ օգտագործենք
print(s)
print(f"{4 in s = }")
print(f"{len(s) = }")
print(f"{sum(s) = }")
Dictionary (բառարան)
Json
= ["aik", "bik"]
anun = ["+1", "+2"]
hamarner
= [["aik", "+1"], ["bik", "+2"]] contacts
Alice-ը՝ 25 տարեկան ա, Bob-ը՝ 30, Charlie-ն 35: Python-ով պահեք էդ ինֆորմացիան
Ներածություն
= {}
d = dict()
d
print(type(d))
= {"Alice": 25, "Bob": 30,
tariqner "Charlie": 35}
print(type(tariqner))
print(tariqner)
print(tariqner["Alice"]) # [0]
# Նոր մարդ ավելացնել
"Dumbledore"] = 40
tariqner[print(tariqner)
# փոփոխել արժեքը
"Dumbledore"] += 1
tariqner[print(tariqner)
# ջնջել մարդու
del tariqner["Charlie"] # delete
print(tariqner)
print(tariqner.keys())
print(tariqner.values())
print(tariqner.keys()[0])
print(list(tariqner.keys()))
print(list(tariqner.keys())[0])
1] = "12312" tariqner[
print(tariqner)
False] = "Barevner"
tariqner[
print(tariqner)
False] tariqner[
3.14] = "pi" tariqner[
1,2]] = 12312 tariqner[[
for mard in tariqner: # կամ tariqner.keys()
print(mard)
for t in tariqner.values():
print(t)
print(tariqner.items())
for i in tariqner.items():
print(i, type(i))
for i in tariqner.items():
= i # i[0], i[1]
name, age print(name, age)
for name, age in tariqner.items():
print(name, age)
Methods
print(len(tariqner))
key, value, items
print(tariqner.keys())
print(list(tariqner.keys()))
print(tariqner.values())
print(list(tariqner.values()))
print(tariqner.items())
print(list(tariqner.items()))
Արժեք ստանալ (get)
'ժուլիեն'] tariqner[
'Alice')
tariqner.get(# գրում ենք բանալին, ու թե ինչ պետք ա վերադարձնի եթե չկա էդ բանալին
print(tariqner.get('ժուլիեն'))
print(tariqner.get('ժուլիեն', "Չկա"))
print(tariqner.get('Alice', "Չկա"))
update
print(tariqner)
= {"John": 32, "Smith": 32, "Alice": 27}
urish_dict
tariqner.update(urish_dict)
print(tariqner)
Dict comprehension
for i in range(-5,6)} {i
**2 for i in range(-5,6)} {i:i
for ...} {key:value
Nested (ներդրված) dictionaries
= {
contacts "Alice": {"phone": "123-456-7890", "email": "alice@gmail.com"},
"Bob": {"phone": "098-78-88-78", "address": "Bobastan"}
}
print(contacts.keys())
print(contacts['Bob'].keys())
print(contacts["Bob"]["phone"])
'Bob']['email'] = "123123@..$"
contacts[print(contacts['Bob'])
🛠️ Գործնական (ToDo)
Հաշվել որ մրգից քանի հատ կա
= ["apple", "banana", "apple", "orange", "banana", "apple"] mrger
= set(mrger)
mrger_set
print(mrger_set, len(mrger_set))
= {}
count_dict
for i in mrger_set:
= mrger.count(i)
count_dict[i]
for i in mrger_set} {i:mrger.count(i)
= {}
count_dict
for mirg in mrger_set:
= 0
count for j in mrger:
if mirg == j:
+= 1
count = count
count_dict[mirg]
count_dict
= {}
count_dict
# if len(mrger) == len(set(mrger)):
# print({i:1 for i in mrger_set})
for mirg in mrger:
print(mirg, count_dict)
if mirg not in count_dict:
= 1
count_dict[mirg] else:
+= 1
count_dict[mirg]
count_dict
= {i:0 for i in mrger_set}
count_dict
for mirg in mrger:
+= 1 count_dict[mirg]
= {}
count_dict print(count_dict)
for mirg in mrger:
if mirg not in count_dict:
= 0
count_dict[mirg]
+= 1
count_dict[mirg] print(count_dict)
Telegram
Settings -> Advanced -> (Scroll down) Export Telegram data -> (Scroll down) Choose “Machine readable JSON” -> Export
import json
= "result.json"
PATH
with open(PATH, "r") as f:
= json.load(f) data
= data["chats"]["list"]
chats
0]["type"] chats[
'public_supergroup'
= [i["type"] for i in chats]
types
= {i:0 for i in types}
count_dict
for i in types:
+= 1
count_dict[i]
count_dict
{'public_supergroup': 4, 'public_channel': 23}
= 0
count_has_message = []
chats_not_empty
for chat in chats:
if chat["messages"]:
+= 1
count_has_message "name"])
chats_not_empty.append(chat[
print(f"Perc - {count_has_message / len(chats) * 100:.2f}")
print(chats_not_empty)
Perc - 18.52
['Չատերի պահով, երևացող նամակներ', 'Մետրիկ Դասընթաց', 'Հայախոս մաթեմատիկայի համայնք', 'ՄաթԹիմ և ԻԿՄ ֆակուլտետի ՈՒԳԸ', 'դըբդֆհֆտվդվ']
= [chat for chat in chats if chat["messages"]] chats_list_not_empty
= {}
msg_counts
for chat in chats_list_not_empty:
= 0
count
= chat["messages"]
msgs for msg in msgs:
if msg["type"] == "message":
+= 1
count
"name"]] = count
msg_counts[chat[
print(msg_counts)
# if chat["name"][""]
# print(*chat["messages"][10:15], sep="\n")
{'Չատերի պահով, երևացող նամակներ': 5, 'Մետրիկ Դասընթաց': 133, 'Հայախոս մաթեմատիկայի համայնք': 12, 'ՄաթԹիմ և ԻԿՄ ֆակուլտետի ՈՒԳԸ': 13, 'դըբդֆհֆտվդվ': 18}
🏡Տնային
Հիմնական տնային
Լուծումներ
🎲 6
- ▶️Johnny Harris
- ▶️Video 🔥
- 🇦🇲🎶Ցայգ
- 🌐🎶Jethro Tull
- 🤌Կարգին