-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython
239 lines (194 loc) · 7.19 KB
/
Python
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Variable Naming Technique:
Camel Case:
variableNameOne = 'John'
Pascal Case
VariableNameOne = 'john'
MyVariableName = 'John'
variable_name_one = 'John'
Variables:
Declare Variable:
VariableName1 = 'VariableValue'
VariableName2 = VariableName1
VariableName3 = VariableName1 + VariableName2
VariableName4, VariableName5, VariableName6 = '1', '2', '3'
VariableName7 = '''I am a teacher and enjoy teaching.
Data Type:
VariableName = 5
VariableName = str('Hello World') str
VariableName = int(20) int
VariableName = float(20.5) float
VariableName = complex(1j) complex
VariableName= list(('apple', 'banana', 'cherry')) list
VariableName= tuple(('apple', 'banana', 'cherry')) tuple
VariableName= range(6) range
VariableName= dict(name='John', age=36) dict
VariableName= set(('apple', 'banana', 'cherry')) set
VariableName= frozenset(('apple', 'banana', 'cherry')) frozenset
VariableName= bool(5) bool
VariableName= bytes(5) bytes
VariableName= bytearray(5) bytearray
VariableName= memoryview(bytes(5)) memoryview
View Data Type:
print(type(VariableName))
Calculations:
print('Addition', 2 + 3) # addition(+)
print('Subtraction', 3 - 1) # subtraction(-)
print('Multiplication', 2 * 3) # multiplication(*)
print('Division', 3 / 2) # division(/)
print('exponential', 3 ** 2) # exponential(**)
print('modulus', 3 % 2) # modulus(%)
print('Division without remiander', 3 // 2) # division without remainder
Replace numbers with variable names to calculate variables.
Operators:
print(3 > 2) # True, because 3 is greater than 2
print(3 >= 2) # True, because 3 is greater than 2
print(3 < 2) # False, because 3 is greater than 2
print(2 < 3) # True, because 2 is less than 3
print(2 <= 3) # True, because 2 is less than 3
print(3 == 2) # False, because 3 is not equal to 2
print(3 != 2) # True, because 3 is not equal to 2
print(len(first_name)) # Number of characters
print(len('mango') == len('avocado')) # False
print(len('mango') != len('avocado')) # True
print(len('mango') < len('avocado')) # True
print(len('milk') != len('meat')) # False
print(len('milk') == len('meat')) # True
print(len('tomato') == len('potato')) # True
print(len('python') > len('dragon')) # False
Boolean:
print('True == True: ', True == True)
print('True == False: ', True == False)
print('False == False:', False == False)
print('True and True: ', True and True)
print('True or False:', True or False)
AND, OR and NOT:
print(3 > 2 and 4 > 3) # True - because both statements are true
print(3 > 2 and 4 < 3) # False - because the second statement is false
print(3 < 2 and 4 < 3) # False - because both statements are false
print(3 > 2 or 4 > 3) # True - because both statements are true
print(3 > 2 or 4 < 3) # True - because one of the statement is true
print(3 < 2 or 4 < 3) # False - because both statements are false
print(not 3 > 2) # False - because 3 > 2 is true, then not True gives False
print(not True) # False - Negation, the not operator turns true to false
print(not False) # True
print(not not True) # True
print(not not False) # False
Strings Manipulators:
\n # Line break
print('This is an \nexample of a lione break')
\t # Tab content
print('Days\tTopics\tExercises'
print(VariableName.capitalize()) # Captitilize first character#
print(VariableName.count('y')) # Count occurances of y
print(VariableName.count('th')) # Count occurances of th
print(VariableName.endswith('on')) # Does value end with 'on'
print(VariableName.endswith('tion')) # Does value end with 'tion'
VariableName = 'I am {} {}. I am a {}. I live in {}.'.format(VariableName, VariableName2, VariableName3, VariableName4)
print(VariableName)
VariableName = 10
VariableName2 = 3.14
VariableName3 = VariableName2
output = 'The area of circle with {} is {}'.format(str(VariableName), str(VariableName3))
print(output)
language = 'Python'
first_letter = language[0]
print(first_letter) # P
second_letter = language[1]
print(second_letter) # y
last_index = len(language) - 1
last_letter = language[last_index]
print(last_letter) # n
first_three = language[0:3] # starts at zero index and up to 3 but not include 3
print(first_three) # pyt
last_three = language[3:6]
print(last_three) # hon
middle_three = language[2:4]
print(last_three) # tho
Lists:
Declare Lists:
fruits = ['banana', 'orange', 'mango', 'lemon'] # list of fruits
# Show list
print('Fruits:', fruits)
# Show number of values in list
print('Number of fruits:', len(fruits))
# First index
first_fruit = fruits[0] # we are accessing the first item using its index
print(first_fruit) # banana
# Second index
second_fruit = fruits[1]
print(second_fruit) # orange
# Last index
last_fruit = fruits[3]
print(last_fruit) # lemon
# Second Last index
last_fruit = fruits[-1]
print(last_fruit) # lemon
# List All list items
all_fruits = fruits[0:4] # it returns all the fruits
orange_and_mango = fruits[1:3] # it does not include the end index
# Change list value
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits[0] = 'Avocado'
print(fruits) # ['avocado', 'orange', 'mango', 'lemon']
fruits[1] = 'apple'
print(fruits) # ['avocado', 'apple', 'mango', 'lemon']
last_index = len(fruits) - 1
fruits[last_index] = 'lime'
print(fruits) # ['avocado', 'apple', 'mango', 'lime']
# checking items
fruits = ['banana', 'orange', 'mango', 'lemon']
does_exist = 'banana' in fruits
print(does_exist) # True
does_exist = 'lime' in fruits
print(does_exist) # False
# Append
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.append('apple')
print(fruits) # ['banana', 'orange', 'mango', 'lemon', 'apple']
fruits.append('lime') # ['banana', 'orange', 'mango', 'lemon', 'apple', 'lime]
print(fruits)
# insert
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.insert(2, 'apple') # insert apple between orange and mango
print(fruits) # ['banana', 'orange', 'apple', 'mango', 'lemon']
fruits.insert(3, 'lime') # ['banana', 'orange', 'apple', 'mango', 'lime','lemon',]
print(fruits)
# remove
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.remove('banana')
print(fruits) # ['orange', 'mango', 'lemon']
fruits.remove('lemon')
print(fruits) # ['orange', 'mango']
# del
fruits = ['banana', 'orange', 'mango', 'lemon']
del fruits[0]
print(fruits) # ['orange', 'mango', 'lemon']
del fruits[1]
print(fruits) # ['orange', 'lemon']
del fruits
print(fruits) # This should give: NameError: name 'fruits' is not defined
# Copy list
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits_copy = fruits.copy()
print(fruits_copy) # ['banana', 'orange', 'mango', 'lemon']
# join
positive_numbers = [1, 2, 3,4,5]
zero = [0]
negative_numbers = [-5,-4,-3,-2,-1]
integers = negative_numbers + zero + positive_numbers
print(integers)
fruits = ['banana', 'orange', 'mango', 'lemon']
vegetables = ['Tomato', 'Potato', 'Cabbage','Onion', 'Carrot']
fruits_and_vegetables = fruits + vegetables
print(fruits_and_vegetables )
# count
fruits = ['banana', 'orange', 'mango', 'lemon']
print(fruits.count('orange')) # 1
ages = [22, 19, 24, 25, 26, 24, 25, 24]
print(ages.count(24)) # 3
# sort
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.sort()
print(fruits)
fruits.sort(reverse=True)
print(fruits)