CS50 - Spring 2026 - Class 1
Example code in this lecture
bbq.py
Lecture notes
What is Computer Science?
What previous experience do you have with programming/computer science?
Why are you taking this class?
About the class
Course Administrative (
http://www.cs.pomona.edu/classes/cs50/administrivia.html
)
- Schedule
- T/Th class
- Roughly weekly assignments, released on Tuesday and due the following Monday
- All material will be posted on the course web page
- We will use slack for discussion
- Use this to ask any content-based questions
- Check it regularly
- Late work policy
- Grading
- Academic honesty and collaboration (read the policy!)
- AI tools are *NOT* to be used in any way on the assignments
Style of the course
- interactive!
- ask lots of questions
- participate
- be expected to do group work in class
- You'll be expected to be here in class
- I'll post my notes and examples online. You may still want to write some things down, but you don't have to write every word down
Experience
- I assume no prior computer science, programming or science background
- If you have previous programming experience or have take the AP CS class, come talk to me and we can figure out if this is the right place for you.
VS Code
- We'll be using the VS Code IDE for this course as our interface into Python
- What is an IDE?
- Integrated Development Environment
- Incorporates a text editor with other tools for running, debugging and navigating through the code
- When it starts, a number of different parts of the window
- For now (and most of this course), we're only going to be using two main components:
- Main editor window
- Terminal window on the right
- (and also the Explorer, which allows you to navigate your files)
- You can rearrange them however you'd like, but I prefer main editor on left and Terminal, etc. on right
Terminal
- The terminal is another way to interact with your computer (specifically, the operating system) using typed commands rather than point-and-click
- In VS Code, we can get the terminal window by selecting "View->Terminal"
- We won't use the terminal extensively in this class beyond using it to start up Python
Python shell
- Python is an interpreted language
- you can type commands and get an immediate response back
- many programming languages require you to compile the program first and then run it
- To run Python, type "python3" (often just "python" works
- Python makes a great calculator
- the ">>>" is the shell prompt where you type statements
- when you hit enter/return, Python executes the code and gives you the answer (if there is one)
- Python has all of the standard mathematical operations
- What math operations might you want?
- +, -, *, /
- ** (power or exponentiation)
- % (mod aka remainder)
>>> 4+4
8
>>> 15*20
300
>>> 20/4
5.0
>>> 10-20
-10
>>> 10+4*2
18
>>> (10+4)*2
28
>>> 2**10
1024
>>> 2**30
1073741824
>>> 2**-2
0.25
>>> 10%4
2
>>> 11 % 7
4
- What is operator precedence?
- Python follows the normal operator precedence you're used to for math
- things in parenthesis get evaluated first
- ** is next
- %, * and / next
- + and - last
Expressions
- Why are these different?
>>> 2 + 3
5
>>> 10/2
5.0
- anything that represents a value (e.g. a number) is called an expression
- contrast this with a statement, which tells the computer to do something
- for example, "walk over there" is a statement in English
- or, for computers, something like, "draw something on the screen"
- All the things we've seen so far have been expressions
- Even just typing a number, e.g. '5' is an expression
- In Python (and many programming languages) all expressions have a type
types
- the "type" of an expression determines how Python interprets and understands an expression
- Python is a "strongly typed" language: every expression in Python has a type
- what are the types of the two expressions above?
- they are both numbers, but Python makes a distinction between integers and floating point numbers (i.e. numbers with decimals)
- For now, just make sure that you understand:
1) There are two different types of numbers in Python (integers and floating point numbers, aka floats)
2) When you'll end up with an integer or a float
- Division always ends up with a float
- If any one number in an expression is a float, then the whole expression will be a float
>>> 2+5*6
32
>>> 2.0 + 5 * 6
32.0
>>> 2**3
8
>>> (10/5)**3
8.0
>>> (10%3)*4
4
bbq party
- you're having a party and you're trying to figure out how many hot dogs to buy. Here's what you know:
- teran isn't a big fan of hot dogs, so he'll only eat 1
- jasmin generally eats 2
- chris always eats twice as many as jasmin
- brenda eats one less than chris
- grace eats half as many as brenda at the party and also likes to take one extra one home
- try and do this on paper
- 13 (assuming that if someone eats half a hot dog, we still have to count the whole thing)
- how did you do it?
- how could you figure this using math operations in Python?
- might be able to do it, but would require a lot of remembering (or writing things down)
variables
- variables allow us to store things and then use them in other expressions
- a variable is storage for a value
- it holds a value
- we can change its value
- we change the value of a variable using '=' (also known as assigning to a variable)
>>> teran = 1
>>> teran
1
- changing the value of a variable is a statement. It tells the interpreter to do something, but does NOT represent a value
- Using variables, we can do our hot dog calculation as a sequence of assignments
>>> teran = 1
>>> jasmin = 2
>>> chris = 2 * jasmin
>>> brenda = chris - 1
>>> grace = brenda/2 + 1
>>> total_hotdogs = teran + jasmin + chris + brenda + grace
>>> total_hotdogs
12.5
- Why did we get 12.5?
- brenda/2 gave us 1.5
- There are many ways to fix this so that we get an integer answer
- One way: the // operator
- "floor" division, rounds the answer towards negative infinity
- for positive integers, drops the decimal portion
>>> 11//2
5
>>> 10//3
3
>>> 11//3
3
>>> 11.0//2
5.0
Note that if you use // on integers, it gives you an integer answer and if use it on a float, it gives you a float, but still with the decimal part gets truncated
- How does this help us?
grace = (brenda + 1) // 2 + 1
- we add one to force it to round up
- if it's an odd number, it does what we want
>>> 3 // 2
1
>>> (3 + 1) // 2
2
- if it's an even number, it doesn't change the answer
>>> 4 // 2
2
>>> (4 + 1) // 2
2
>>> teran = 1
>>> jasmin = 2
>>> chris = 2 * jasmin
>>> brenda = chris - 1
>>> grace = (brenda + 1) // 2 + 1
>>> total_hotdogs = teran + jasmin + chris + brenda + grace
>>> total_hotdogs
13
naming variables
- generally you want to give good names to variables (x and y are not good names unless they represent x and y coordinates)
- variables should be all lowercase
- multiple words should be separated by an '_' (underscore)
variable assignment: when assigning to a variable, Python evaluates what is on the right hand side of the equals sign and then assigns this value to the variable
- After typing the above, if I then type jasmin = 4, what happens to chris?
- chris stays the same!
- '=' is not mathematical equivalence
what if you get a text from jasmin and she now says she's planning on eating 4?
- we'd have to re-enter each of the lines (well except the first one)
- I already had to do this once to change for the // and it was annoying. I don't want to have to keep doing it!
programs in Python
- besides interacting with the shell, we can write statements in a file and then run them
- the file editing window in VS Code allows us to do this
- on many levels, it behaves a lot like a text editor (e.g. Word). You can:
- create new files
- open files
- save files
- edit files
look at
bbq.py code
- I've typed in the same code we typed into the shell, but now in the text editing section
- I've saved it in a file called bbq.py
- we'll use the extension .py by convention to indicate a Python program
- Anything different from what I'd typed before into the shell?
- I've used whitespace
- you can use spaces without affecting how the code executes
- we use blank lines to make the code more understandable
- I've included "comments"
- These don't affect how the program runs, but are important for writing programs that you (and others) can understand
- this is part of the "style" of the program
comments
- comments in Python are designated using '#' (the pound sign)
- python ignores everything from the # to the end of the line
- you can put them on lines by themselves
- or if you have short comments, you can add them at the end of a line
- comments are VERY important
- they allow you to communicate in plain English (to others and to yourself when you look at the code later)
- you will be required to put them in your programs for this course
running the code
- With an IDE, not only can you edit code, you can also run it
- The easiest way is to click the "run" arrow
- it executes your program a line at a time from the top
- it's like you typed all of those commands in the shell
- the one difference is that you don't get line-by-line feedback
- for example, if you put 4+4 in a program on a line by itself, you won't see anything
- if you want to see the value of a variable or an expression, you need to "print" it (more on this next time)
- Often, it can be useful to run the program and then still be able to interact with python
- You can run python "interactive" from the terminal, by typing "python3 -i your_program_name.py"
- If you do this, just like running a program normally, it executes each statement a line at a time
- However, when the program finishes, it leaves you in the python shell.
- You can look at the value of any variables, etc. E.g.,
>>> total_hotdogs
13
What is the syntax of a language (say English)?
- It describes what are valid things to say in that language
- "I like dogs" is syntactically correct (it's a determiner followed by a verb followed by a noun)
- "I dog like" is not syntactically correct (it's a determiner followed by a noun followed by a verb)
- programming languages also have their own syntax
- it describes what is valid in a language
when syntax fails...
- have we seen any examples of syntax so far?
- all of the math operations have implicit syntax
>>>4 +
File "<input>", line 1
4 +
^
SyntaxError: invalid syntax
- python explicitly tells us that we had a syntax error
- we also get some warning with the red squiggly line
- assignment as well
>>> dave =
File "<input>", line 1
dave =
^
SyntaxError: invalid syntax
- the nice thing about using the VS Code is that it checks the syntax and tries (but doesn't always succeed) at highlighting where the syntax error is
- sometimes look at the line right before it if you can't find it on that line