Python

Calvin (Deutschbein)

W2Fri: 6 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!

Running hello.py

  • Unlike Karel, Python is fast
  • If we run hello.py, it will likely run so fast we won't see anything!
  • So somehow we need to slow it down.
  • My recommendation: the command line / terminal.
    • On Windows: <windows key> ⇒ type "cmd" ⇒ enter
    • On Mac: <spotlight key> ⇒ type "terminal" ⇒ enter
    • In VS Code: View⇒Terminal (or 'ctrl/cmd' + '`', the key with ~ on it)
  • From here we can do two things...

Running hello.py

  • In what I will call the "terminal", type "python" (Win) or "python3" (Mac*)
  • This will change the terminal so that it is expecting to read Python
  • You will be able to tell because it will look like this: Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
  • The shorthand of the three '>' is the how to tell where to type Python code. >>>

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. "do you have Python open?" 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.

Running hello.py

  • You can write Python code after the three '>' marks. C:\Users\cd-desk>python Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("hello world") hello world >>>
  • We will not move on until everyone can do this.

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 hello.py
    • Win: <windows key> ⇒ type "cmd" ⇒ enter
    • Mac: <spotlight key> ⇒ type "terminal" ⇒ enter
    • VS Code: View⇒Terminal (or 'ctrl/cmd' + '`') C:\Users\me>python Python 3.12.5... Type "help"... >>> 2000 * 32 * 365 23360000 >>>
    cal.py# CALvinORIES print(2000 * 32 * 365)

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

  • We could run e.g. karel programs as C:\Users\me>python Karel_Newspaper.py
  • This can be a helpful way to see error messages (in the terminal)

Aside: 'cd'

  • Let's talk about "paths" or "file locations":
  • In the terminal, we usually are working in the home directory.
    • For me, 'C:\Users\cd-desk' at home or 'C:\Users\ckdeutschbein' in class.
  • In this class, we usually are working on Python (.py) files... somewhere else
    • For me, it was this long "path" or "file location" for PS1: C:\Users\cd-desk\Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main

Aside: 'cd'

  • If we want to check our PS1 code, we have use "python Karel_Newspaper.py" in the file location where Karel_Newspaper.py lives.
  • To change our file location, we use the "cd" command, for change directory. I type: cd Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main
  • It looks like this altogether: C:\Users\cd-desk>cd Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main C:\Users\cd-desk\Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main>

Aside: 'cd'

  • Then I can type: python Karel_Newspaper.py
  • It looks like: C:\Users\cd-desk>cd Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main C:\Users\cd-desk\Downloads\problem-set-1-karel-cd-public-main\problem-set-1-karel-cd-public-main>python Karel_Newspaper.py
  • That is a very long file path and could justifiably feel annoying to you, but we can imagine it as: Home\Downloads\PS1zip\PS1folder

=

  • 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.

In the terminal... C:\Users\me>python Python 3.12.5... Type "help"... >>> calories_per_day = 2000 >>> years = 32 >>> days_per_year = 365 >>> days = years * days_per_year >>> calories_per_day * days 23360000

cal.py# 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.