-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrays_loops_conditionals.py
More file actions
144 lines (96 loc) · 2.54 KB
/
arrays_loops_conditionals.py
File metadata and controls
144 lines (96 loc) · 2.54 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
"""
Ok, here is an array/list/whatever you want to call it:
(Technically there are specific names you should use, don't worry about it for now)
"""
array = ['banana', "apple", 12345, 66.04534] # As you can see, they can hold multiple types! (Dynamic typing)
print(array[0])
print(array[1])
# print(array[7]) # Oops!
print(array[-1]) # Wait, we can do that!?!?!!?
print(array[-2]) # There's no way!!!!
print(len(array)) # That's how you get the length
"""
We can append/remove to lists like so:
"""
print(array)
array.append('one_more_thing')
print(array)
array.pop()
print(array)
"""
Here is a basic if statement
"""
if 2 > 1:
print('Math is not broken') # Note: THE INDENTATION MATTERS!!! yes, python has forced whitespace, unlike C++
elif 2 == 1:
print('Doesn\'t sound right!') # elif = else if
else:
print('No other case was entered into!')
"""
You can also stick em in a single line
"""
x = 'Yippee' if 2 > 1 else 'Nopieee'
print(x)
"""
Really, in the example above think of the if/else statement as a single expression
"""
x = ('Yippee' if 2 > 1 else 'Nopieee') + ' and some other string'
print(x)
"""
A string is basically a list of characters
"""
x = 'hello'
print(x[0], x[1], x[2], x[3], x[-1])
# Actually, the Python builtin "list" function converts things to lists (arrays)
print(list(x))
"""
For loops work like this:
"""
for i in range(0, 5):
print(i)
for i in range(5): # Works the same as above, as by default we start at 0
print(i)
"""
Here's a nested for loop:
"""
for x in range(5):
for y in range(5):
print(f'({x}, {y}) ', end='')
print()
print() # <--- We do this to add an extra newline to the output
"""
Note: probably better to use r, c over i, j or x, y
r represents [r]ow and c represents [c]olumn!!!!
"""
for r in range(5):
for c in range(5):
print(f'({r}, {c}) ', end='')
print()
print()
"""
Let's take the array from earlier and print it out piece by piece
"""
array = ['banana', "apple", 12345, 66.04534]
for i in range(len(array)):
print(array[i])
print()
"""
Let's try that again in a more ~pythonic~ way
"""
for item in array: # wow nice! we can use whatever variable name we want instead of "item"
print(item)
print()
"""
Here's one more thing, in case you want the index and the element together
"""
for i, item in enumerate(array):
print(i, item)
print()
"""
An important thing is that Python "ranges" are inclusive-exclusive, meaning
that the left bound is inclusive, and the right bound is exclusive
Importantly, that means this prints from 0 up to 4
"""
for i in range(0, 5):
print(i, end=' ')
print()