-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_reference.py
543 lines (382 loc) · 12.5 KB
/
python_reference.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/python
# This python script serves as an example to reference
# while learning/re-learning python. There will be different
# Sections added and they will be labeled accordingly.
#---------------------------#
# Object and Data Structure #
#---------------------------#
my_string = 'String'
my_second_string = 'String 2'
my_third_string = "There's an single quote in here!"
my_fourth_string = '"Python is cool"'
my_math_variable = 5*10
my_list = [1,2,3,4,5]
my_second_list = [6,7,8]
my_dictionary = {'key1':'value1','key2':'value2'}
my_dictionary_fmt = {'apple':2.25,
'bread':5.99,
'milk':4.80}
my_flex_dictionary = {'key':'value', # Key value pair
'keylist':[1,2,3], # Key contains list as value. Can pull from list like: my_flex_dictionary['keylist'][2]
'kik':{'insidekey':100}, # Key inside of key (call using my_flex_dictionary['kik']['insidekey']
'ree':['one', 'two', 'three']} # ^ returns 100
my_tuple = (1,2,3)
# Strings, indexing, and slicing
#-------------------------------#
# Print string variable...
print(my_fourth_string)
# Print tri from the word string...
print('string'[1:4])
# Print ring from the word string...
print('string'[2::])
# Concatenation - merging
#-------------------------------#
# Concatenate lists...
print(my_list + my_second_list)
# Using methods
#-------------------------------#
# Print uppercase variable with method...
print(my_third_string.upper())
# Methods, append 9...
my_second_list.append(9)
print(my_second_list)
# Methods, remove 9...
my_second_list.pop()
print(my_second_list)
# Using the sort method to sort lists...
my_third_list = ['a','e','x','b','c']
print(my_third_list)
my_third_list.sort() # Actually sorts variable assigning new sorted var.
print(my_third_list)
my_num_list = [4,1,8,3]
print(my_num_list)
my_num_list.sort() # Actually sorts variable assigning new sorted var.
print(my_num_list)
# Reverse method...
my_third_list.reverse() # Actually reverses variable assigning new reversed var.
# Math and numbers
#-------------------------------#
# Print number variable (integer)...
print(my_math_variable)
# Do math with variable and set new variable (float)...
my_math_variable_2 = my_math_variable / 11
print(my_math_variable_2)
# Use float formatting... "{value:width:precision f}"
print("The formatted result is {r:4.2f}".format(r=my_math_variable_2))
# Print formatting
#-------------------------------#
# Print formatting examples...
print('Python formatting {}'.format('rules!'))
name = "Sam"
age = 3
print(f'{name} is {age} years old.')
# Dictionaries
#-------------------------------#
# Print entire dictionary...
print(my_dictionary)
# Print key1 value...
print(my_dictionary['key1'])
print(my_dictionary_fmt['milk'])
# Print specific from list and format with method...
print(my_flex_dictionary['ree'][2].upper())
# Add new key value pair to dictionary...
my_flex_dictionary['newkey'] = 300
print(my_flex_dictionary)
# List all keys in dictionary...
print(my_dictionary_fmt.keys())
# List all values...
print(my_dictionary_fmt.values())
# Tuples (Immutable)
#-------------------------------#
# Tuples are like lists, but immutable and have less methods...
t2 = ('one', 2, 'one')
print(t2)
# Count instances of 'one' in t2...
print(t2.count('one'))
# Show index of where 'one' appears in tuple the first time...
print(t2.index('one'))
# Sets (unordered collections of unique elements)
#-------------------------------------------------#
my_set = set()
# Add values to my_set...
my_set.add(1)
print(my_set)
# Convert messy list to set and rid of the duplicate values...
shitlist = [1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,3]
print(shitlist)
set(shitlist) # results in {1, 2, 3}
# Booleans (T or F)
#-------------------------------#
# Must have capitalized True or False
print(1 > 2) # returns False
print(1 == 1) # returns True
# I/O with Basic Files
#-------------------------------#
# To open file called file.txt OLDER WAY OF DOING THIS...
my_file = open('./scratch/file.txt')
# Read contents of file.txt
my_file.read()
# To re-read, you must seek the cursor back to 0
my_file.seek(0)
my_file.read()
# Output with cleaner lines, allows for looping through list...
my_file.seek(0)
my_file.readlines()
# Close file after using...
my_file.close()
# Better way to open file... This will close the file when you are done using it. Call contents to see content of file.
with open('./scratch/file.txt') as my_new_file:
contents = my_new_file.read()
# Permissions:
# r = read
# w = write
# a = append
# r+ = read and write
# w+ = write and read (overwrites existing file or creates new)
# Open with read-only mode...
with open('./scratch/file.txt',mode='r') as my_new_file:
contents = my_new_file.read()
# Open with write-only mode...
with open('./scratch/0sdf3we9edf.txt',mode='w') as my_new_file:
contents = my_new_file.write('Created by Python')
# Append new lines to end of file...
with open('./scratch/file.txt',mode='a') as my_new_file:
my_new_file.write('\nNewly appended line')
# Comparison Operators
#-------------------------------#
# Equals...
2 == 2 # True
2 == 1 # Not true
'hello' == 'goodbye' # False
'2' == 2 # False
# Not Equals...
3 != 3 # False
4 != 5 # True
# Greater than...
2 > 1 # True
2 < 2 # False
2 >= 2 # True
# Logical operators
#-------------------------------#
# Multiple comparisons with logical operators... and, or, not
# True, lets use logical operators though
1 < 2 < 3
1 < 2 and 2 < 3
'h' == 'h' and 2 == 2
# For organization, we can use (). Not REQUIRED though...
('h' == 'h') and (2 == 2)
# Or...
1 == 1 or 1 == 100 # True
# Not...
not(1 == 1) # False
not 1 == 1 # False
not 400 > 5000 # True
# Python if statements
#-------------------------------#
# if some_condition:
# execute some code
# elif: some_other_condition:
# do something different
# else:
# do something else
hungry = True
if hungry:
print('Feed me!')
else:
print("Don't feed me!")
# Another example...
loc = 'Bank'
if loc == 'Auto Shop':
print('Fix my car!')
elif loc == 'Bank':
print('Money is nice!')
elif loc == 'Store':
print('Welcome to the store.')
else:
print('This isnt the car shop?')
# Python for statements
#-------------------------------#
# my_iterable_list = [1,2,3]
# for item in my_iterable_list:
# print(item)
my_list = [1,2,3,4,5,6,7,8,9,10]
for num in my_list:
print(num)
# Use list to do something different...
for num in my_list:
print('Hello')
# Check for even or odd... If divided by 2 == 0...
for num in my_list:
if num % 2 == 0:
print(f'Even number: {num}')
else:
print(f'Odd number: {num}')
# For every number in initial my_list variable, do list_sum + number and assign sum to list_sum...
list_sum = 0
for num in my_list:
list_sum = list_sum + num
print(list_sum)
# If the print were to be outside the indentation, you would only get the final number. (55)
# If we do not want to use a variable to iterate through something, just use an _...
for _ in 'Hello World':
print('Cool!')
# Tuple un-packing pairs...
my_tuple_list = [(1,2),(3,4),(5,6)]
for item in my_tuple_list:
print(item)
# Or, alternatively...
for a,b in my_tuple_list:
print(a) # If you want to only see value a in tuple pair, only print a
print(b)
# Unpack items from a dictionary...
dict = {'type':'Test','num':1,'class':'None'}
for item in dict:
print(item) # Note how this only prints the keys, if we want the values as well, we must do the following
for item in dict.items(): # Can also use .values()
print(item)
# Again, we can use unpacking by copying the format...
for key,value in dict.items(): # Can also use .values()
print(value)
# Python while loops
#-------------------------------#
# Simple example...
x = 0
while x < 5:
print(f'The value of x is {x}')
x += 1 # Add x by 1
else:
print('The value of x is greater than 5.')
# Using break, continue, pass...
# break = Breaks out of the current closest enclosing loop.
# continue = Goes to the top of the closest enclosing loop.
# pass = Does nothing at all.
# Break example:
w = 'Sammy'
for letter in w:
if letter == 'a': # If the letter is a, break out of the loop
break
print(letter)
#--
r = 0
while r < 5:
if r == 2:
break # Once r is equal to 2, break.
print(r)
r += 1
# Continue example:
for letter in w:
if letter == 'a': # If the letter is a, go back to the top of the loop and continue, dont print a.
continue
print(letter)
# Pass example: (Instead of erroring, just pass and let me continue.
q = [1,3,5]
for item in q:
# Nothing happens here...
pass
print('This comment outside of the for loop still works.')
# Useful Operators
#-------------------------------#
# Range for iteration...
#################################
# Prints 0 - 10
for num in range(10):
print(num)
# Prints 3 - 9
for num in range(3,10):
print(num)
# Prints 0 - 10 in a step size of 2...
for num in range(0,10,2):
print(num)
# Using range to generate a list... 0-10 in step size of 2
print(list(range(0,11,2)))
# Enumeration
###################################
# Without enumerating...
index_count = 0
for letter in 'abcde':
print('At index the letter is {}'.format(index_count,letter))
index_count +=1
# With enumerating...
word = 'abcde'
for item in enumerate(word):
print(item)
# This provides us with tuples... (0, 'a') and so on...
for index,letter in enumerate(word):
print(index)
print(letter)
print('\n')
# Zip
###################################
# Zip list together from 2 seperate list, giving us tuples (1, a) (2, b)...
from builtins import zip
list_1 = [1,2,3]
list_2 = ['a','b','c','d'] # If there is an extra value in a list, it will be left out.
list_3 = [11,12,13]
for item in zip(list_1,list_2, list_3):
print(item)
# To list the zip values...
print(list(zip(list_1,list_2)))
# In operator
###################################
# Check to see if x is in a list, dictionary, etc...
print('x' in [1,2,3]) # False
# Dictionary example...
t = {'mykey':345}
345 in t.values()
# Min, Max, & Random
###################################
minmax = [10,20,30,40,50,60,70,80]
# Print min of list...
print(min(minmax))
# Print max of list...
print(max(minmax))
# Using random to shuffle list...
from random import shuffle, randint
rando_list = [3,4,5,6,7,8,9]
print(rando_list)
shuffle(rando_list)
print(rando_list)
# Using random to grab random integer from range...
print(randint(0,100))
# Set variable as random number... Not sure why you would do this...
rando_var = randint(22,35)
print(rando_var)
# User Input
###################################
# Input always takes a string...
result = input('Enter a number here: ')
print(result)
# If we want to get a format other than string, we have to do the following...
print(float(result))
print(int(result))
# Or, to get around this, just wrap the var in the type when prompting for input
# result = int(input('Enter a number here: '))
# List Comprehensions
#-------------------------------#
# Creating a list using the append method...
hello_string = 'hello'
ex_list = []
for letter in hello_string:
ex_list.append(letter)
print(ex_list)
# More efficient way of doing this...
ex_list = [letter for letter in hello_string]
print(ex_list)
num_list = [num for num in range(0,11)]
print(num_list)
# You can get more versatile with this as well...
num_list = [x for x in range(0,11) if x%2==0] # Number in range provided, but only if divisible by 2...
print(num_list)
num_list = [x**2 for x in range(0,11) if x%2==0] # Same thing, but the square root of the even numbers...
print(num_list)
# Converting celcius to fahrenhite example...
celcius_temp = [10,20,35,36.8]
fahrenhite_temp = []
for temp in celcius_temp:
fahrenhite_temp.append(( (9/5)*temp + 32))
print(fahrenhite_temp)
# Lets flatten this out...
fahrenhite_temp = [( (9/5)*temp + 32) for temp in celcius_temp]
print(fahrenhite_temp)
# Methods and Functions
#-------------------------------#