forked from atbudak/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaries.py
More file actions
125 lines (101 loc) · 3.85 KB
/
Dictionaries.py
File metadata and controls
125 lines (101 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Dictionaries
# 2 şekilde dict tanımlanır. Collectionlardan tuple,list i value ekleyebiliriz
grocer1 = {"fruit": "apple", "drink": "water"}
grocer2 = dict(fruit="apple", drink="water")
print(grocer1)
print(grocer2)
# key ve value'ye int string boolean değer atanabilir
# dict' dict() şeklindeyken key değişken şeklinde olduğundan int atılamaz
# çünkü stringe çevrilir
first_Dict = {1: "one", "two": 2, False: [1, 2, 3]}
print(first_Dict)
# sec_dict'teki key listeyi kabul etmezken tuple'ı kabul eder.
# sec_dict = {[1, 2, 3]: "Listele", "clarus": "the best"}
third_dict = {(1, 2, 3): "Listele", "clarus": "the best"}
# print(sec_dict)
print(third_dict)
# key değeri olarak dictionary atanamaz ama value dict. atanabilir.
# fourth_dict = {{1: "bir", 2: "iki"}: "sözlük"}
fifth_dict = {"erkekler": {"ahmet": 35, "mehmet": 20, "recep": 55}, \
"kadınlar": {"aslı": 22, "selman": 42, "fatma": 19}}
# print(fourth_dict)
print(fifth_dict)
# dict. key'i parenteze yazarak value'sünü döndürür.
print(fifth_dict['erkekler'])
# aşağıdaki şekilde dict.'e ekleme yapabiliriz
fifth_dict['hayvanlar'] = 'Kedi'
print(fifth_dict)
# Task
# Dictionary oluşturma
family = {
'mother': 'Türkan',
'father': 'Ömer',
'brother': 'Adem'}
print(family)
family['sister'] = 'Hacer'
# append ile listeye ekleme yapabiliriz.
sayılar = {'tek': [], 'çift': []}
sayılar['tek'].append(1)
print(sayılar)
# fromkeys gönderilen değerleri key olarak atıyor
meyveler = {}.fromkeys(['elma', 'armut'], 0)
print(meyveler)
# int değeri artırma += şeklinde yapılabilir
meyveler['elma'] += 3
print(meyveler)
dict_by_dict = {'animal': 'dog',
'planet': 'Neptun',
'number': 40,
'pi': 3.14,
'is_good': True}
# item, key, value değerlerini döndürür
# iterable şeklindedir, döngü içine girebilir
print("Items : ", dict_by_dict.items())
print(dict_by_dict.keys())
# dict. leri list şekline çevirir
print(dict_by_dict.values())
print(list(dict_by_dict.items()))
print(list(dict_by_dict.values()))
# dict. kalıcı olarak değiştirir, sonuna ekler
# {} kullanarak eklenir ve birden fazla ekleme yapılabilir
# yoksa ekler varsa değiştirir
dict_by_dict.update({'is_bad': False, 'is_qualified': True})
print(dict_by_dict)
# value-key silme işlemi
# yan yana yazarakta silme işlemi yapabiliriz
del dict_by_dict['is_qualified'], dict_by_dict['is_bad']
print(dict_by_dict)
# using 'in' and 'not in' operator,
# you can check if key is in dictionary,
# aramaları sadece key 'lerde yapar
clarusway = {'ali': 12, 'veli': 32, 'ahmet': 22}
print('zeka küpü' in clarusway)
# dict. i value metoduyla çağırarak value araması yapabiliriz.
print(12 in clarusway.values())
school_records = {
'personal_info':
{'kid': {'tom': {'class': 'intermediate', 'age': 10},
'sue': {'class': 'elemantary', 'age': 8}
},
'teen': {'joseph': {'class': 'college', 'age': 19},
'marry': {'class': 'high school', 'age': 16}
},
},
'grades_info':
{'kid': {'tom': {'math': 88, 'speech': 69},
'sue': {'math': 90, 'speech': 81}
},
'teen': {'joseph': {'coding': 80, 'math': 89},
'marry': {'coding': 70, 'math': 96}
},
}
}
print("school_records uzunluğu : ", len(school_records))
print("Marry'nin yaşı :", school_records['personal_info']['teen']['marry']['age'])
# Liste biçiminde joseph'in ders ve notları
print("joseph (Liste):", list(school_records['grades_info']['teen']['joseph'].items()))
print("joseph (Dict):", school_records['grades_info']['teen']['joseph'])
print("joseph :", school_records['grades_info']['teen']['joseph'])
# İki dictionary birleştirmenin kısayolu
combined_dict = dict(**dict_by_dict, **school_records)
print(combined_dict)