Python one more time

Ok, we already kinda did this, but this time its going to be better. Hopefully

SCC 2024

What are we going to do?

  • Today: Setup IDE, learn how to use it.
    • Hello world and the formalities
    • How to read error messages
  • Next 2 weeks
    • Exercises (2-3/class)
    • Cheat-sheet
  • Last week
    • Game project
SCC 2024

Codespaces setup.

  1. Open codespaces for the "Python" repo in the Seycove Coding Club team.

  2. Create a new folder inside learn with your name

QR Code

SCC 2024

Extensions

go into the extensions menu in the sidebar, then search for and install the following extensions:

  • Error Lens
  • Ruff
  • Jupyter
  • Black Formatter
SCC 2024

First program

Make a new file called hello.py
in the file, write a hello world:

print("Hello, world!")

Then, click the "Run" button in the top corner to see the output:

> Hello, world!
SCC 2024

Your first program

Most of the larger exercises we do will be in single files (hellp.py, fizzbuzz.py, etc..) but if we need to just mess around with snippets and experiment, we might use a python notebook.

What is a Python Notebook?

A Python notebook is an interactive environment where you can write and execute Python code in cells. This allows for a mix of code, visualizations, and markdown text, making it ideal for experimentation, and sharing code with explanations.

SCC 2024

Creating a notebook

To create a new Python notebook, hit new file and make a file called hello.ipynb

Your first notebook

You should now see a new tab open with your notebook. You can add new cells by clicking the + button in the toolbar, and select the type of cell you want to add.
You can run a cell by clicking the "play" button in the cell toolbar, or you can hit "Run all" in the toolbar to run all cells in the notebook.

SCC 2024

Your first notebook

Try adding a new cell with the following code:

message = "Hello, world!"

Then, add another cell with the following code to print the message:

print(message)
SCC 2024

Your first notebook

Try running the second cell before the first cell. What happens?
Now, try running the first cell, then the second cell. What happens now?
What if you use the Run all button?

SCC 2024

How notebooks work

Notebooks run code in whatever order you run the cells, this can be useful for testing chunks of code, but you need to remember that cells might need information from other cells to run properly.
If you want to reset all variables, you can hit the "Reset" button in the toolbar.

SCC 2024

Error messages

When you run(or write) code, you might get an error message, sometimes called a traceback. These messages are Python's way of telling you what went wrong, and where it went wrong.

SCC 2024

Reading error messages

When you get an error message, the first thing you should do is read it. It might look like a lot of text, but there's a few ways to make it easier to understand:

  • Look at the last line first, as the last line is usually the most important.
  • Read bottom to top, as the error is in the order that the code was run, so the last line is the most recent.
  • Check the line number, this tells you where the error occurred.
  • If all else fails, dont be afraid to ask for help!
SCC 2024

Error Types

There are a few different types of errors you might encounter:

  • SyntaxError: You wrote something that Python doesn't understand
  • IndentationError: You didn't indent your code properly
  • NameError: You tried to use a variable that doesn't exist, or is out of scope
  • TypeError: You tried to use a variable in a way that Python doesn't understand, like dividing a string by a number
  • ValueError: You tried to use a variable in a way that Python doesn't understand, like converting a string to a number when it cant be.
  • ZeroDivisionError: You tried to divide by zero, why would you do that?
SCC 2024

Lets try it out!

When we run this program, we get an error.

print("Hello, world!"

Lets take a look at the error message:

Cell In[1], line 1
    print("Hello World )
          ^
SyntaxError: unterminated string literal (detected at line 1)

What's the problem here?

SCC 2024

A harder example

What about this code?

def greet(someone):
    print('Hello, ' + someon)

greet('Bryan')

when we run this code, we get the following error:

Traceback (most recent call last):
  File "/workspaces/Python/learn/finn/hello.py", line 4, in <module>
    greet('Chad')
  File "/workspaces/Python/learn/finn/hello.py", line 2, in greet
    print('Hello, ' + someon)
                      ^^^^^^
NameError: name 'someon' is not defined. Did you mean: 'someone'?
SCC 2024

A harder example - part 2

This error is a bit more complicated, but lets still try.
going from the bottom up we can figure out:

  • The error is a NameError
  • The error is because the variable someon is not defined
  • The error is on line 2 of the hello.py file print('Hello, ' + someon)
  • The error is in the greet function
  • The error happens when we call the greet function on line 4.
SCC 2024

An interpreted language

Python is an interpreted language, which means that it runs code line by line, if it encounters an error, it stops running the code. This means that if you have an error on line 2, the code will stop running at line 2, and you won't see any output from the rest of the code.
It also means that sometimes errors can be caused by code that ran earlier in the program, but the error only shows up later on.

SCC 2024

Recap? sure.

  • How to setup a Python environment in Codespaces
  • How to write and run a simple Python program
  • How to create and run a Python notebook
  • How to read and understand error messages
SCC 2024