Skip to content

Commit d3162d3

Browse files
committed
Dictionaries and loops
1 parent 3b1663b commit d3162d3

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

Dictionaries.py

+33
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,36 @@
2929

3030

3131
print(se)
32+
33+
34+
35+
inventory={
36+
"item1":100,
37+
"item2":200,
38+
"item3":300
39+
}
40+
41+
inventory['item1']-=3
42+
print(inventory['item1'])
43+
44+
if 'item4' not in inventory:
45+
inventory['item4']=345
46+
print(inventory)
47+
48+
text = "I am a multimillionare a boy"
49+
word={}
50+
# for w in text.split():
51+
# if w in word:
52+
# word[w]+=1
53+
# else:
54+
# word[w]=1
55+
for w in text.split():
56+
word[w]= word.get(w,0)+1
57+
print(word)
58+
59+
new_d = dict.fromkeys(word.keys(),'')
60+
print(new_d)
61+
liss = [33,44,55,32]
62+
dit = dict.fromkeys(liss,'yes')
63+
print(dit)
64+

loops.ipynb

+73-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {},
77
"outputs": [
88
{
@@ -13,14 +13,23 @@
1313
"1\n",
1414
"2\n",
1515
"3\n",
16-
"4\n"
16+
"4\n",
17+
"2\n",
18+
"3\n",
19+
"4\n",
20+
"5\n",
21+
"5\n"
1722
]
1823
}
1924
],
2025
"source": [
2126
"a = (2,3,4,5,5);\n",
2227
"for i in range(len(a)):\n",
23-
" print(i)"
28+
" print(i) # print index becaue using range\n",
29+
"\n",
30+
" #but if using directly name of tuple\n",
31+
"for x in a:\n",
32+
" print(x) # print elements directly.\n"
2433
]
2534
},
2635
{
@@ -74,6 +83,67 @@
7483
"\n",
7584
"\n"
7685
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"import numpy as np\n",
94+
"abc = [2,3,2,3,3,6,7]\n",
95+
"\n",
96+
"for i in range(0,len(abc),2): # increment int\n",
97+
" print(i)\n",
98+
"\n",
99+
"for i in np.arange(0,len(abc),0.5): # increment float\n",
100+
" print(i) "
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 23,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"43\n",
113+
"4\n",
114+
"10\n",
115+
"22\n",
116+
"12\n",
117+
"43\n",
118+
"4\n",
119+
"10\n",
120+
"22\n",
121+
"12\n",
122+
"860 yes\n",
123+
"200 yes\n",
124+
"240 yes\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"h = [12,22,10,4,43]\n",
130+
"\n",
131+
"for i in reversed(h):\n",
132+
" print(i) # start printing from last index. or reverse\n",
133+
"\n",
134+
"for i in h[::-1]: # same work\n",
135+
" print (i) \n",
136+
"\n",
137+
"for i in range(len(h)-1,-1,-2):\n",
138+
" print(h[i]*20,'yes')\n"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": []
77147
}
78148
],
79149
"metadata": {

0 commit comments

Comments
 (0)