-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_tutorial.py
More file actions
410 lines (238 loc) · 7.09 KB
/
python_tutorial.py
File metadata and controls
410 lines (238 loc) · 7.09 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
Ref: https://github.com/giraffeacademy/
'''
Python is a general purpose, dynamically typed and interpreted, object
oriented programming language that was created in the late 1980s by
Guido van Rossum.
Python's design philosophy revolves around readability. It's meant to be
easy to read and easy to write. This is accomplished by using white-space
to deliniate code blocks instead of the more traditional curly brackets and
semi-colons.
Generally all python code is run using an interpreter. The most popular and
original interpreter is called CPython, because it's implemented in the c
programming language. Several other interpreters exist however, many of
which are implemented in languages other than C like Java and C#.
The most common Python interpreter CPython, uses an automatic garbage collector
to manage memory. And Python is widely known for having a non-traditional,
minimalist syntax which is largly based on white space, and designed to be
clean and readable.
In 2008, the Founder, Guido van Rossum decided to clean up the Python codebase and overhall
a lot of the things in Python 2 that he didn't like, thus creating Python 3.
'''
# P R I N T I N G
print("Hello")
print("World") # unexpected indent
print("!")
# V A R I A B L E S
'''
Names are case-sensitive and may begin with:
letters, $, _
After, may include
letters, numbers, $, _
Convention says
Start with a lowercase word, then additional words are separated
by underscores
ex. my_first_variable
'''
name = "Mike" # Strings
age = 30 # Integer
gpa = 3.5 # Decimal
is_tall = True # Boolean -> True/False
name = "John"
print("Your name is " + name)
print("Your name is", name)
# C A S T I N G & C O N V E R T I N G
print( int(3.14) )
print( float(3) )
print( str(True) )
print( int("50") + int("70") )
# S T R I N G S
greeting = "Hello"
#indexes: 01234
print( len(greeting) )
print( greeting[0] )
print( greeting[-1] )
print( greeting.find("llo") )
print( greeting.find("z") )
print( greeting[2:] )
print( greeting[2:3] )
# N U M B E R S
print( 2 * 3 ) # Basic Arithmetic: +, -, /, *
print( 2**3 ) # Basic Arithmetic: +, -, /, *
print( 10 % 3 ) # Modulus Op. : returns remainder of 10/3
print( 1 + 2 * 3 ) # order of operations
print(10 / 3.0) # int's and doubles
num = 10
num += 100 # +=, -=, /=, *=
print(num)
++num
print(num)
# Math class has useful math methods
import math
print( pow(2, 3) )
print( math.sqrt(144) )
print( round(2.7) )
# U S E R I N P U T
name = input("Enter your name: ")
print("Hello", name + "!")
num1 = int(input("Enter First Num: "))
num2 = int(input("Enter Second Num: "))
print(num1 + num2)
# L I S T S
lucky_numbers = [4, 8, "fifteen", 16, 23, 42.0]
# indexes 0 1 2 3 4 5
lucky_numbers[0] = 90
print(lucky_numbers[0])
print(lucky_numbers[1])
print(lucky_numbers[-1])
print(lucky_numbers[2:])
print(lucky_numbers[2:4])
print(len(lucky_numbers))
# N Dimensional Lists
numberGrid = [ [1, 2], [3, 4] ]
numberGrid[0][1] = 99
print(numberGrid[0][0])
print(numberGrid[0][1])
# L I S T F U N C T I O N S
friends = []
friends.append("Oscar")
friends.append("Angela")
friends.insert(1, "Kevin")
# friends.remove("Kevin")
print( friends )
print( friends.index("Oscar") )
print( friends.count("Angela") )
friends.sort()
print( friends )
friends.clear()
print( friends )
# T U P L E S
lucky_numbers = (4, 8, "fifteen", 16, 23, 42.0)
# indexes 0 1 2 3 4 5
lucky_numbers[0] = 90
print(lucky_numbers[0])
print(lucky_numbers[1])
print(lucky_numbers[-1])
print(lucky_numbers[2:])
print(lucky_numbers[2:4])
print(len(lucky_numbers))
# F U N C T I O N S
def add_numbers(num1, num2=99):
return num1 + num2
sum = add_numbers(4, 3)
print(sum)
# I F S T A T E M E N T S
is_student = False
is_smart = False
if is_student and is_smart:
print("You are a student")
elif is_student and not(is_smart):
print("You are not a smart student")
else:
print("You are not a student and not smart")
# >, <, >=, <=, !=, ==
if 1 > 3:
print("number omparison was true")
if "dog" == "cat":
print("string omparison was true")
# D I C T I O N A R I E S
test_grades = {
"Andy" : "B+",
"Stanley" : "C",
"Ryan" : "A",
3 : 95.2
}
print( test_grades["Andy"] )
print( test_grades.get("Ryan", "No Student Found") )
print( test_grades[3] )
# W H I L E L O O P S
index = 1
while index <= 5:
print(index)
index += 1
# F O R L O O P S
for index in range(5):
print(index)
# lucky_nums = [4, 8, 15, 16, 23, 42]
# for lucky_num in lucky_nums:
# print(lucky_num)
# for letter in "Giraffe":
# print(letter)
# E X C E P T I O N C A T C H I N G
answer = 10 / int(input("Enter Number: "))
try:
answer = 10 / int(input("Enter Number: "))
except:
print("Error")
try:
answer = 10 / int(input("Enter Number: "))
except ZeroDivisionError as e:
print(e)
except:
print("Caught any exception") # Big no-no
try:
answer = 10 / int(input("Enter Number: ")) # do this opeartion
except:
print("something went wring")
else:
print("if error occured, run this block")
finally:
print("Always runs, whether error occured or not")
# finally is helpful to verify the operation.
# For exp: if error occured during file read, then close the file, if it is still opened.
# Classes & Objects
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def read_book(self):
print("Reading", self.title, "by", self.author)
book1 = Book("Harry Potter", "JK Rowling");
# book1.title = "Half-Blood Prince"
print(book1.title)
book1.read_book()
# Getters & Setters
class Book:
def __init__(self, title, author):
self.title = title;
self.author = author
@property
def title(self):
print("getting title")
return self._title
@title.setter
def title(self, value):
print("setting title")
self._title = value
@title.deleter
def title(self):
del self._title
def read_book(self):
print("Reading", self.title, "by", self.author)
book1 = Book("Harry Potter", "JK Rowling");
# book1.title = "Half-Blood Prince"
print(book1.title)
book1.read_book()
#Inheritance
class Chef:
def __init__(self, name, age):
self.name = name
self.age = age
def make_chicken(self):
print("The chef makes chicken")
def make_salad(self):
print("The chef makes salad")
def make_special_dish(self):
print("The chef makes bbq ribs")
class ItalianChef(Chef):
def __init__(self, name, age, countryOfOrigin):
self.countryOfOrigin = countryOfOrigin
super().__init__(name, age)
def make_pasta(self):
print("The chef makes pasta")
def make_special_dish(self):
print("The chef makes chicken parm")
myChef = Chef("Gordon Ramsay", 50)
myChef.make_chicken()
myItalianChef = ItalianChef("Massimo Bottura", 55, "Italy")
myItalianChef.make_chicken()
print(myItalianChef.age);