11/16/November - Practical Python Session

python gif

Pre Requisites

  • Review the course content from last class, and try to continue going through the exercises! If you get stuck on any of them, no problem -- make a note of this so we can review these tasks at the beginning of class on Thursday.

    Review 02/November - Installing Python
  • (Optional) create a repository (local and remote) to gather your Python exercises from last class. Add and commit the file to the repository, and then push these to GitHub.

  • (Optional practice) go through exercises on w3 schools for Python through Functions section OR on particular areas that you would like to review. This is entirely optional but good practice! No need to go over Lambda, Classes, Inheritance, or Modules as we have not seen these yet.


Class Curriculum

Section content Expected time (mins) Pre - Requirements
Lesson Goals 5 minutes ❌
Questions from last class & review of a few exercises 10-15 minutes ❌
Overview of loops (for, while) 10-15 minutes ❌
Hands-on Python exercises (in small groups) 20 minutes ❌
Break (in-person troubleshooting of Python installations if needed) 10 minutes ❌
Continue hands-on Python exercises (in small groups) rest of class ❌
Check-out 5 minutes ❌

0. Lesson Goals

  • Have a working development environment on Python.
  • Build familiarity with the concepts of data types, functions, and control flow (loops, conditionals)
  • Continue writing code in Python

1. Check-In

  • Are there any remaining questions from last class?
  • What exercises were the most challenging? (Respond in the chat)

2. for and while loops

Loops are a tool that help us execute a block of code a specified amount of times or based on a condition. In Python, there are two loops: for and while loops.

while loops

while loops allow us to execute a block of code as long as the condition evaluates to True.

loop diagram

Please excuse the ugly diagram :)

Syntax:

1while condition:
2    # do things

Examples:

1# This is called an "infinite loop" since the condition is always True!!
2while True:
3    print("I'm an infinite loop! I won't stop unless I'm forced to!")
 1count = 0
 2while count < 10:
 3    print("hello #", count)
 4    # increment the value of `count`
 5    count += 1
 6    # outputs:
 7    # hello # 0
 8    # hello # 1
 9    # hello # 2
10    # hello # 3
11    # hello # 4
12    # hello # 5
13    # hello # 6
14    # hello # 7
15    # hello # 8
16    # hello # 9
17

Check-in question: can someone explain what's going on in each of these examples?

for loops

for loops provide a way to go through each value (or set of values) in an iterable (e.g. a list, string, dictionary, etc.) and execute a block of code that uses that value.

Syntax:

1for variable(s) in iterable:
2    # do things

Examples: (feel free to try executing these in your IDLE terminal, notebook cell (in Colab), VSCode, or PyCharm :)

1for num in [1, 2, 3]:
2    print(num)
3    # outputs:
4    # 1
5    # 2
6    # 3
1for letter in 'word':
2    new_word = letter + '!!'
3    print(new_word)
4    # outputs:
5    # w!!
6    # o!!
7    # r!!
8    # d!!
1# python
2for num, letter in [(1, 'a'), (2, 'b')]:
3    # Here, we convert number to string using `str` before concatenating with another string
4    print(str(num) + letter)
5    # outputs:
6    # 1a
7    # 2b
8

With for loops, it can also be useful to use the range operator in Python which creates an iterable of numbers. The syntax of this is range(start, end, step), but you will also see this as just range(start, end) or range(end) (in this last case, the start is assumed to be 0). Note that the end value is not included (in other words, "numbers starting with start up until (not through) end"). Here are some examples:

1for i in range(3):
2    print(i)
3    # outputs:
4    # 0
5    # 1
6    # 2
1list(range(1, 4))  # we can create a list out of a `range`
2# [1, 2, 3]
1list(range(-1, 9, 3))  # here, the step (space between values) is 3
2# [-1, 2, 5, 8]

3. Hands-on Python Tasks

Work in groups to solve the following tasks in your Python Dev setup. As a group exercise, we expect you to discuss with your group members to solve the problems together. This may mean having one person share their screen and talking through the code you will write together, and making sure you all understand before moving on to the next task. You are not expected to get through all of these in class, and they range from easier to harder. We will continue working on these exercises in the next session as well.

To build on the skills that we have learned earlier in the semester, if working in VSCode or PyCharm, please create a new repository and commit your changes as you go. You can check out the reference sheets to help remember the necessary git and command line commands. Click here for the cheat sheet

Optionally, you may send in a link to a GitHub repository with your solutions (if working in VSCode or PyCharm) or share your notebook (if working in Colab) if you would like feedback on your work :)

Note: if you are writing in Colab or a Python Notebook, then try to keep each task in a separate code cell. If you are writing in a pure Python file, consider wrapping each task in a function (that is, write a function to solve the task) and call these functions in the if __name__ == '__main__': block to test out your code. This might look something like:

 1# in your file:
 2def task_n(a, b):
 3    # code to solve task 1
 4    ...
 5
 6    return answer_to_task_n
 7
 8if __name__ == '__main__':
 9    print(task_n(1, 2))  # prints output of task_n
10

Note: it can be helpful to test out your functions by making sure that the functions return what you expect for a few different inputs. Make sure to try out a variety of inputs including some "edge cases", or cases that are trickier and can cause problems. These edge cases often have to do with "0" or empty-inputs. (Optional): In Python, you can also write assert statements, where you would state what you believe the function to output to be, and it fails if this is not the case.

Let's use an example task 0 to illustrate all the points above.

Task 0 (warmup)

Write a function called add_10 that takes in an integer, adds 10, and returns this value. Test out these steps and make sure you feel comfortable with the process. The following tasks won't have a walk through, but you should follow the same rough process.

Steps:

  1. Write out your function skeleton, or the empty function with the name and parameters. (Note: n is commonly used to denote a number, but you can also write more descriptive variable names):
1def add_10(n):
2    # TODO: add logic to add 10
3    return "Not implemented"
  1. Let's call this function somewhere so we can see what it's outputting. In a .py file:
1if __name__ == '__main__':
2    # the text is not required, but it helps me see what I called to get the output
3    print("add_10(3): ", add_10(3))  # for now, this returns "add_10(3): Not implemented"
4
  1. Let's add in some logic and check the values that it is outputting. If these values don't match your expectations (which often happens in programming), then take another look and try to understand what's going wrong. You can add print statements in your code as well to tell you what the values of different variables and conditions are while you are running your code. You should feel encouraged to look things up when you run into issues as well.
1def add_10(n):
2    return n + 10
3
4if __name__ == '__main__':
5    print("add_10(3): ", add_10(3))  # this should now print "add_10(3): 13"
6    print("add_10(-10): ", add_10(-10))  # this should now print "add_10(-10): 0"
7
  1. Optional: you can also use assert statements to confirm your expectations. This is a way of writing simple "test cases" to make sure your logic works. This looks something like:
1def add_10(n):
2    return n + 10
3
4if __name__ == '__main__':
5    assert add_10(3) == 13
6    assert add_10(-10) == 0
7    assert add_10(0) == 10
8

What happens if the assert statement is wrong? (Hint: try out something like assert add_10(3) == 5 which we expect to fail and see what happens.)

You can also add this in to a test function to organize your code:

 1def add_10(n):
 2    return n + 10
 3
 4def test_add_10():
 5    assert add_10(3) == 13
 6    assert add_10(-10) == 0
 7    assert add_10(0) == 10
 8
 9
10if __name__ == '__main__':
11    test_add_10()  # expect no output if all assertions passed
12

Task 1

Write a function called longer_string that takes in two strings and returns the longer of the two. If they are the same length, return the first string.

Task 2

Write a function called is_odd that takes in an integer and returns True if the number is odd and False otherwise.

Hint: look up (Google) what the modulo (%) operator in Python is and use it to determine if a number is odd or even.

Task 3 (parts 1, 2)

Write a function called hello_world_n that takes in an integer n and prints "Hello, world!" n times.

  1. do this using a for loop
  2. do this using a while loop

Task 4

Write a function called sum_list that takes in a list of integers (i.e. [3, 0, 10, 4, 5, 3]) and returns the sum of them. Do not use the sum function in your logic, but you can use this to test your logic! (Hint: assert sum(lst) == ...)

Task 5

Write a function that takes in a list of integers (i.e. [3, 0, 10, 4, 5, 3]) and prints the value of each element greater than 4. For example:

  • input: [3, 0, 10, 4, 5, 3]

    1# output:
    2# 10
    3# 5
    
  • input: [4, 5, 6, 6, 5, 4]

    1# output:
    2# 5
    3# 6
    4# 6
    5# 5
    
  • input: [4, 3, 2, 1]

    1# no output
    

Optional Challenge: change your function to take in a second parameter, min_val. Now, print all values in the list greater than this value.

Example input: [4, 3, 2, 1], 2

1    # output:
2    # 4
3    # 3

(Challenge) Task 6 (parts 1, 2)

Note: this one is optional and meant to be challenging! :)

Like with task 4, do not use the Python function max in your function, but you can use this to test it out!

  1. Write a function that takes in a list of integers (i.e. [3, 0, 10, 4, 5, 3]) and returns largest value in the list.
  2. Write a function that takes in a list of strings (i.e. ['a', 'asdf', '']) and returns the longest string in the list.

(Challenge) Task 7 (parts 1, 2, 3)

Note: this one is optional and meant to be challenging! :)

  1. Write a function that takes in an integer and prints all numbers below it that are divisible by 3. (Hint: recall Task 2 -- how can you check if a number is divisible by 3?)

    Example input: 12

    1# output:
    2# 0
    3# 3
    4# 6
    5# 9
    
  1. Modify the function to return how many numbers are divisible by 3, in addition to printing each number. (i.e. the returned value for the example input above would be 4 (0, 3, 6, 9)).

  2. Modify this function to make it general -- if the function takes in n and k, find out how many numbers less than n are divisible by k.

    Example input: 8, 2

    1# output:
    2# 0
    3# 2
    4# 4
    5# 6
    6# returns 4
    

Additional Resources