-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnotes1.py
executable file
·108 lines (84 loc) · 1.87 KB
/
notes1.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
# Comparisons can be chained!
1 < 2 < 3 # => True
2 < 3 < 2 # => False
1j*1j == -1
# math vs cmath
# Strings are created with " or '; multiline strings with ''' or """
"This is a string."
'This is also a string.'
"""
This is a
multiline
string
"""
pass
# A string can be treated like a list of characters
"This is a string"[0] # => 'T'
# Negative indices
"This is a string"[-2] # => 'n'
#'==' vs 'is'
None is None # => True
1 is 1 # => True
[1] is [1] # => False
# None, 0, and empty strings/lists all evaluate to False.
# All other values are True
bool(0) # => False
bool("") # => False
"yahoo!" if 3 > 2 else "no!"
li = []
li.append(1)
li.append(2)
li.append(3)
li.append(4)
li.insert(0,100)
li.pop()
# Lists as stacks, queues
li[-1]
li[1:3]
li[::-1]
del li[2]
1 in li
len(li)
# Tuples are like lists but are immutable.
tup = (1, 2, 3)
d, e, f = 4, 5, 6
e, d = d, e # Neatest swap
# Dictionaries store mappings
empty_dict = {}
filled_dict = {"one": 1, "two": 2, "three": 3}
filled_dict.keys()
filled_dict.values()
filled_dict.get("four", 4)
some_set = set([1, 2, 2, 3, 4])
filled_set = {1, 2, 2, 3, 4}
filled_set.add(5)
filled_set | other_set
filled_set & other_set
{1, 2, 3, 4} - {2, 3, 5}
if True:
print("Hello")
elif 1==1:
print("Bye")
else:
print("Huh")
for animal in ["dog", "cat", "mouse"]:
print("{} is a mammal".format(animal))
# For - Else
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
def add(x, y=0):
print("x is %s and y is %s" % (x, y))
return x + y # Return values with a return statement
add(y=6, x=5)
add(5, 6)
add(1)
def create_adder(x):
def adder(y):
return x + y
return adder