-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths03_pfc_conditions.py
More file actions
204 lines (136 loc) · 3.12 KB
/
s03_pfc_conditions.py
File metadata and controls
204 lines (136 loc) · 3.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-
"""S03 - PFC - Conditions.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1u9kN_l-AT_FrpOvphTMqeDQFLg-NsafH
"""
"""# Program Flow Control: Conditions
## **`if`** condition
"""
grade = int(input("What is your grade? :"))
if grade < 12:
print("Failed")
a = 30
b = 3
if b > a:
print("b is larger")
print("than a!")
"""**`else`**"""
a = 13
b = 10
if b > a :
print("b is greater than a")
else:
print("a is greater than b or equal to b")
"""**`elif`**"""
grade = int(input("What is your grade? :"))
if grade >= 17:
print("A")
elif grade >= 14:
print("B")
elif grade >= 12:
print("C")
else:
print("F")
"""**short versions of `if`**"""
if a > b : print("a is greater than b")
print("A") if a > b else print("B")
"""**Logical Operators in Conditions**"""
a = 100
b = 50
c = 200
if c>a and c>b:
print("c larger than both a and b")
"""**Nested `if`s**"""
x = 19
if x > 10:
print("Above Ten: Pass!")
if x > 17:
print("and above 17: A!")
else:
print("but not A!")
"""## Keeping more than one value: `list`s, `tuple`s, `set`s, `dict`iornaries
| | `list` | `tuple` | `set` | `dict` |
|---------|--------|---------|-------|--------|
| mutable | ✔ | ❌ | ✔ | ✔ |
| duplicate values | ✔ | ✔ |❌ |❌(key)/✔(value)|
| ordered | ✔ | ✔ | ❌ |❌ |
| how to build | `[a,b,a,d]` | `(a,b,a,d)` | `{a,b,c,d}` | `{a:b,c:d}` |
| how to build| `list()` | `tuple()` | `set()` | `dict()` |
| indexing | `[i]` | `[i]` | ❌ | `[key]` |
| add new value | `.append(v)` | ❌ | `.add(v)` | `.update({k:v})` |
| remove item | `.pop(i)` | ❌ | `.pop()` | `.pop(k)` |
| sort | `.sort()` | ❌ | ❌ | ❌ (you can do some stuff utilizing`sorted()` |
| find first element | `.index(v)` | `.index(v)` | ❌ | `.get(k)` |
| count all found elements | `.count(v)` | `.count(v)` | ❌ | ❌ |
| reverse | `.reverse()` | ❌ | ❌ | ❌ |
"""
l = [1,2,4,5,3,64,"23",5,5,33,9]
print(type(l))
print(l)
print(l[5])
print(type(l[5]))
print(type(l[6]))
l.append(34)
print(l)
t = l.pop(6)
print(l)
print(t)
l.sort()
print(l)
names = ["Parsa","Amirhosein","Amin","Pouria","Moein"]
names.sort()
print(names)
i = l.index(9)
l.pop(i)
print(l)
l.remove(34)
print(l)
l.count(5)
l.reverse()
print(l)
print(l[3])
print(l[2:4]) # items 2 up to (not including) 4
print(l[2:])
print(l[:4])
print("e" in "Majid")
print(5 in l)
t = (1,2,4,5,3,64,"23",5,5,33,9)
print(type(t))
print(t)
print(t[5])
print(type(t[5]))
print(type(t[6]))
i = t.index(9)
print(i)
print(t.count(5))
l1 = [2,3,4,5]
l2 = l1.copy()
print(l1 is l2)
l2.append(16)
print(l1 is l2)
print(l1)
tp = tuple(l2)
print(l2)
print(tp)
l3 = list(tp)
print(l3)
myset = {1,8,34,7,12}
print(myset)
myset.add(14)
print(myset)
m = myset.pop()
print(myset)
print(m)
print(7 in myset)
my_dict = {'a':1,'c':8,'e':7,'d':12,'b':13,'f':11,'g':8}
print(my_dict)
print(type(my_dict))
print(my_dict['e'])
my_dict.update({'h':12})
print(my_dict)
o = my_dict.pop("b")
print(my_dict)
print(o)
del my_dict['a']
print(my_dict)