Skip to content

Commit 855642c

Browse files
authored
Update README.md
1 parent 023d93a commit 855642c

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

2020_07_14_course/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Thank you for taking the time to read this write-up on the Python introductory c
1818
- [Functions](#functions)
1919
- [Lambdas](#lambdas)
2020

21+
22+
2123
## Setting up Python
2224

2325
Depending on your OS you will want to install the latest version of Python available from the official [Download Page](https://www.python.org/downloads/). The course will be using Python 3.8 or newer, and while most features work in previous versions it's best to be up-to-date with all the latest updates and security patches.
@@ -187,3 +189,80 @@ print(multiply(5, 2))
187189
```
188190

189191
Lambdas automatically return the value they're assigned to, so the `return` keyword can be omitted.
192+
193+
### Loops and If statements
194+
195+
#### While loops
196+
While loops work by running a certain piece of code while a given statement is true. So for example if you wanted to print all numbers up to 10 with a while statement, you would do this:
197+
```py
198+
i = 0
199+
while i<10:
200+
print(i)
201+
i += 1 # increment the counter
202+
```
203+
A trace table for this can be seen here:
204+
```markdown
205+
| Iteration | I | While | Output |
206+
|-----------|----|-------|-----------|
207+
| 1 | 0 | True | 0 |
208+
| 2 | 1 | True | 1 |
209+
| 3 | 2 | True | 2 |
210+
| 4 | 3 | True | 3 |
211+
| 5 | 4 | True | 4 |
212+
| 6 | 5 | True | 5 |
213+
| 7 | 6 | True | 6 |
214+
| 8 | 7 | True | 7 |
215+
| 9 | 8 | True | 8 |
216+
| 10 | 9 | True | 9 |
217+
| 11 | 10 | False | No output |
218+
```
219+
#### For loops
220+
For loops are similar to while loops however they work with ranges. These ranges can be different things, you can have ranges between numbers but similarly you can have ranges on strings. We will replicate the numbers between 1 and 10 with a for loop.
221+
```py
222+
for i in range(0,10):
223+
print(i)
224+
```
225+
As you can see this is much more efficient, you do not need to increment the variable manually or even initialise it, the for loop does this itself. As stated above you can also do this on strings so i will show this below too:
226+
```py
227+
my_str = "hello world"
228+
for i in my_str:
229+
print(i)
230+
"""
231+
Output:
232+
h
233+
e
234+
l
235+
l
236+
o
237+
238+
w
239+
o
240+
r
241+
l
242+
d
243+
"""
244+
```
245+
As you can see it goes over each character individually, in an iterative process.
246+
247+
#### If / else/ elif statements
248+
If statements are a fundamental concept in the majority of programming languages and python is no different, this is used to check if a given condition is true. If it is then it will execute the code it is supplied if not it will go to through elif until one is true (if supplied) if none are true then it will go to the else statement. An example of this will be:
249+
```py
250+
age = int(input("Enter your age"))
251+
if age < 18:
252+
print('You cannot enter the club')
253+
elif age > 18:
254+
print('Welcome to the club')
255+
else:
256+
print('Please provide ID, 18 year olds must be checked')
257+
258+
"""
259+
>>> Enter your age
260+
>18
261+
Please provide ID, 18 year olds must be checked
262+
>>> Enter your age
263+
>16
264+
You cannot enter the club
265+
>>>Enter your age
266+
>38
267+
Welcome to the club
268+
"""

0 commit comments

Comments
 (0)