Python

Calvin (Deutschbein)

W2Fri: 5 Sep


Announcements

  • Sections check-in.
    • Help us help you - if you have a conflict, get sick, etc. - reach out ASAP.
  • By MONDAY 11:59 PM: "Problem Set 1: Karel" assignment.
    • One problem (probably) using "def" and "for"
    • One problem using at least "while" and maybe the other three.

Today

  • Do we need homework screencapture slides?
    • Doing homework: It's fun.
  • I can live-code any homework questions.
    • Let's crush this homework... together.
  • Introducing Python
    • Arithmetic
    • Variables
    • Functions

Post-Karel

  • Thus far we Karel'ed.
  • But what if we don't want to only navigate imaginary robots?
    import karel def main(): move() # no import def main(): move() # this won't work

What does Python do?

High Level

  • Computer science as the science of solving problems.
  • Python as the way of specifying solutions to problems.
  • We used Karel to solve imaginary problems, like newspaper and painting tasks.
  • What do we do in Python?
  • Let's consider a famous example: the "Hello, World!" program
  • This solves the task of "How can a computer communicate data to a human?"

Hello World!

  • Like with Karel, we can create a file, such as hello.py
    some_karel.py import karel def main(): move() hello.pyprint("Hello, World!")
  • "Print" like main, is an old term.
  • It goes back to when computers didn't have monitors and "printed" the results of calculations!

Make hello.py

  • Open VS Code
  • File->New File
  • Type "hello.py" (it will be a box in the upper middle of the screen).
    • It matters that you spell this correctly.
    • You may need to confirm you wish to create the file, press enter until the file appears.
  • Click in the main window, it may say Open chat (Ctrl+I), or select a language (Ctrl+K M), or fill with template to get started. Start typing to dismiss or don't show this again.
  • Type print("hello world")

Running hello.py

  • Click VS Code "play" button
    • Top right
  • A window called the terminal will pop up at the bottom of your screen.
  • It will contain lots of text, but will have one line reading "hello world".

Running hello.py

  • My experience: Students often do not read the text in the terminal.
  • That is less than a tweet of text.
  • I will often ask e.g. "did Python start?" to 🦗🦗🦗
  • Be present; be thoughtful; be curious.
  • This terminal may look scary, but it tries to help you and I know you can understand much of it.

High Level

  • In Python, we will begin the famous example of "math problems" which I will call "arithmetic".
  • Here is an example of an arithmetic problem that is hard for a human and easy for a computer: 1234 * 5678 # its 7006652... # but I would never be able to figure that out

Suppose...

  • Like many other living beings, I eat food.
    • Vegetable check!
  • But how much food?
  • I am approximately 32 years old, eat approximately 2000 calories per day.
  • I couldn't even guess when I started eating 2000/day, so I'll assume always.
  • How can I use Python to calculate this? 2000 * 32 * 365
  • Okay... but how does that help me?
  • How do I see the solution?

cal.py

  • Like with Karel, we can create a file, such as cal.py # CALvinORIES print(2000 * 32 * 365)
  • When we "play" we will see, in the terminal...C:\Users\me>python cal.py 23360000
  • We'll see other stuff too! That's okay.

=

  • We can also assign variables with the equals sign.
    • The format is <variable name> = <numerical value>
    • Variable names must not contain spaces, so usually we replace spaces with underscores '_' variable_name = 7 # one of my fav numbers

=

  • We can use this to make arithmetic easier to understand and appreciate.
    # CALvinORIES calories_per_day = 2000 years = 32 days_per_year = 365 days = years * days_per_year print(calories_per_day * days)

    In the terminal...
    C:\Users\me>python cal.py 23360000

    • Try it!

Review: Control Flow

    Does it happen more than once?
    No Yes (it's a loop)
    Is it conditional
    (do we ask Karel a question)?
    No
    • 'def' functions
    • Define a series of actions
    • We can "call" functions
    • Code in 'def' doesn't run unless called
    • 'for' loop
    • Code in the loop runs i times
    • Good for repeated actions
    Yes
    • 'if' statement
    • Code in the statement runs if Karel answers "yes" to some question
    • Good if what you want to do depends on walls/beepers/etc
    • 'while' loop
    • Code in the statement runs as long as Karel answers "yes" to some question
    • Good for completing tasks that require repeated actions.

Exercise

  • Do "Problem Set 1: Karel"
    • Use comments
    • The first problem "newspaper" benefits from functions, and perhaps for loops
      • It is possible, but annoying, to do without either
    • The second problem, "painting" benefits from 'while' loops (and could use 'if')
      • I used four (4) 'while' loops, and no 'if' statements or 'for' loops

Announcements

  • By MONDAY 11:59 PM: "Problem Set 1: Karel" assignment.
    • One problem (probably) using "def" and "for"
    • One problem using at least "while" and maybe the other three.