-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_tips.py
More file actions
91 lines (77 loc) · 1.91 KB
/
simple_tips.py
File metadata and controls
91 lines (77 loc) · 1.91 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
def func1():
#run through a list and output its index as well - use enumerate
cities = ['Colombo','Ottawa','Toronto', 'Rangoon']
for i,city in enumerate(cities):
print(i,city)
def func2():
#printing the values from two lists at the same time
xl = [1,2,3]
yl = [2,4,6]
#zip creates tuples of two lists (1,2), (2,4), (3,6)
for x, y in zip(xl,yl):
print(x,y)
def func3():
#swap variable values (generally used temp variables)
# y=-10
# x=10
x,y = 10,-10
print('Before x is ', x, ' and y is', y)
#tmp = y
#y=x
#x=tmp
#tuple unpacking
x, y = y, x
print('After x is ', x, ' and y is', y)
def func4():
#how to determine if a person is in the dict or not
ages = {
'Mary': 31,
'Kohn': 32
}
#gets the age in the ages, but if its not there the default value is
age = ages.get('Dick','unknown')
print('Dick is ', age, ' years old')
def func5():
#searching a list for a specific value
needle = 'd'
haystack = ['a','b','c']
#generally use a for loop and an if statement which has a else statement
for letter in haystack:
if needle==letter:
print('Found')
break
else: # If no break occured this will be executed
print('Not Found')
def func6():
#file read
f = open('tensor_flow_readme.txt')
for line in f:
print(line)
f.close()
#with statement allows us to not close the file - using context
with open('tensor_flow_readme.txt') as f:
for line in f:
print(line)
def func7():
print('Converting')
try:
print(int('1'))
except:
print('Failed')
else:
print('Passed')
finally: # finally will always execute before the program crashes even if except is not there or exception occurs
print('Done!')
def func8():
my_dict = {"j":4,"h":5}
for key,value in my_dict.iteritems(): #returns one item at a time instead of all if you use items()
print(my_dict[key])
def func9():
x = []
for i in range(10):
if i%5==0:
x.append(i)
print(x)
f = [i for i in range(10) if i%5==0]
print(f)
func9()