1 of 16

OpenBrackets

Intermediate Python

Week 1 Day 2

Welcome!

2 of 16

Welcome to class!

This is the Intermediate Python Programming class.

Today this will be our schedule:

  • 40 minutes on variable types
  • 10 minutes for a break
  • 40 minutes on conditionals, loops, and lists

3 of 16

Asking Questions

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

4 of 16

How are you doing?

  1. From a scale of 1 to 10, how’s your day so far and why?
  2. How would you place the emotes on this scale? (From left to right, these emotes are named weary, frowning, neutral_face, slight_smile, smile, and sunglasses.)

1 2 3 4 5 6 7 8 9 10

5 of 16

More Variable Types, Type Conversion

6 of 16

Variable Conversions

  • We can convert variable types
    • To convert something to a number, we can use int(), and put it in the parentheses
    • Same with a string -- we’d use str() and put the variable we want to convert in the parentheses
    • "5" + 5 doesn’t work, but int("5") + 5 does!
    • Make sure variables can be converted!
      • int("asdf") won’t work
  • Ask the user for their favorite number, and print that number plus 8

7 of 16

Variable Types Exercises

  • What will these print out (or what error will they cause)?
    • Try to answer before testing them yourself!
    • It’s okay to be wrong — a wrong answer is better than no answer. These are designed to be tricky!

print(2 + 3)

print("2" + 3)

print("2" + "3")

print(int("2") + int("3"))

print(str(2) + int("3"))

print(2 * 3)

print("2" * 3)

print("2" * "3")

print(int("2") * int("3"))

print(str(2) * int("3"))

8 of 16

Other Variable Types: float

  • A float stores a number with a decimal, like 3.14 or 2.5
    • All floating-point numbers can run into precision issues
    • Computers can’t store the exact floating-point number
    • Try printing 0.1 + 0.2. What do you get, and why do you think that’s what’s printed?
  • Converting a float to an int will chop off anything after the decimal
    • 7.0, 7.5, and 7.99999 will all become 7

9 of 16

Other Variable Types: bool

  • Checking equality (==) or comparing (< and >) produces booleans
  • A bool stores True or False
  • Conversions to a bool are a bit complicated
    • For now, 0, 0.0, and "" (an empty string) are all False
    • Everything else is True
  • Converting bool to int: True is 1, False is 0
  • Exercises:
    • Use Python to check if 5 equals 3 + 2
    • Use Python to check if "abc" equals "ab" + "bc"
    • Use Python to check if 234 * 32 is less than 35 * 231

10 of 16

10 minute break!

  • Turn your camera off
  • Get a drink of water
  • Stand up and stretch

11 of 16

Conditionals, Loops, Lists

12 of 16

Conditionals

  • Sometimes we only want the computer to do things if something is True
  • This lets our program react to situations and user input
  • Usually, this uses the if and else keywords�

Exercise: write a program that asks for a number.�Then check if the number is less than 8 or not.�If it is, print "This number is less than 8".�Otherwise, print "This number is greater than or equal to 8".

13 of 16

For Loops

  • A lot of programs are written to do repetitive tasks
  • Instead of writing the same code over and over, we can tell the computer to loop the code
  • For loops can loop through a range or list
    • It can loop through lots of other things too, but for now we’ll stick with range and list
  • Remember that range(x) will include 0, but will not include x
  • Try printing all the numbers from 0 to 19, using a for loop

14 of 16

While Loops

  • While loops give us more control over the loops
  • Instead of looping through a range of list, we can check a condition
  • Be careful with infinite loops!
    • Make sure your loop will eventually stop�
  • Try printing the numbers from 3 to 25 using a while loop

15 of 16

When you need help outside class

Email contact@openbrackets.us with your class name, teacher, and question

If you are age 13 or older, you can try Discord

  • https://discord.gg/ZRtk534

The age restriction is because of Discord’s Terms of Service

16 of 16

Thanks for coming to class!

Please let us know if you have any questions!