
After this lesson, you will be able to…
Think about a box of chocolates.
How do you know how many chocolates are in it?
What if I ask you again tomorrow?
Should you count each time? Or should you get a label? “Contains 15 chocolates.”
What if you come across that same scenario in programming?
# This box has 15 chocolates
print("This chocolate box 15 chocolates")
# Later....
print("This chocolate box has", "...How many chocolates, again?")(It’s a delicious program!)
The same way, in Python, we can use labels to save information.
Instead, we could tell Python:
Labels in Python are called variables.
Variables…
This is very helpful for things that change.
How many cupcakes have you eaten today?
In a program, where do you think variables could be helpful?
To use a variable, we simply announce that we want to use it. How?
Simply write it (a.k.a declare it).
Now, you can print it.
Side note: See that underscore? We’ll look at variable naming in a second!
Try it yourself - run the code below!
You can name a variable anything you’d like. …But is it a good idea?
You don’t need to be meaningful, but you should try.
Do you think there are any variable names that could actually break your code?
Some common naming mistakes:
CUPCAKES_IVE_EATEN and cupcakes_ive_eaten are not the same!)lowercase_letters_with_underscores (it’s in the official Python style guide)Try using different cases (“CUPCAKES”) and spaces (“cupcakes ive eaten”) to declare variables, and see what happens:
What if, later, you eat more cupcakes? Now, this is wrong.
What do you think we need to do?
In the example below, what do you think the output of the code is?
6Math works on numerical variables, too!
+, -, * (multiply), and / (divide) operators work just like they do with regular math.We can also perform math between variables!
cupcakes_in_a_box = 3
boxes_of_cupcakes = 5
cupcakes_i_will_eat = boxes_of_cupcakes * cupcakes_in_a_box # This is 15Try declaring more variables and doing math with them:
You can reassign a variable using that very same variable.
cupcakes_ive_eaten = 3
cupcakes_ive_eaten = cupcakes_ive_eaten + 1 # cupcakes_ive_eaten is now 4.
cupcakes_left_in_box = 6
cupcakes_left_in_box = cupcakes_left_in_box - 1 # cupcakes_left_in_box is now 5.
cupcakes_left_in_box = cupcakes_left_in_box - cupcakes_ive_eaten # cupcakes_left_in_box is now 1.Try to code the below:
So, what are some operators are available to us in Python?
| Operator | Example | Description |
|---|---|---|
| + | 5 + 14 | Addition |
| - | 7 - 4 | Subtraction |
| * | 3 * 2 | Multiplication |
| / | 10 / 5 | Division |
| ** | 10**3 | Exponents |
| % | 10 % 4 | Modulus: Like division, but gives remainder instead of quotient |
An exponent multiplies a number by itself. In regular math, it’s written 103.
Try making an exponent!
Is 6 even or odd?
Is 7 even or odd?
How do you think a computer knows?
Modulus operator shows the remainder of a division problem.
What do you think the following prints out?
Try finding the modulus of 15 and various numbers, like 15 % 2.
Let’s look at some even and odd examples.
If I asked you to take a number and add 7 to it, you could do:
How?
9 + 7 happens before the reassignment.This is okay:
But this is better:
Let’s try this.
In the empty environment below, follow the prompts:
A character is:
A string is:
You tell Python that your variable will hold a string using quotation marks.
box_contents = "cupcakes" # This is a string
print(box_contents) # It's a normal variable - we can print it.
best_snack = "Frosted Cupcakes" # This is a string.
cupcakes_ive_eaten = 5 # No quotes - this is a number.
cupcakes_ive_eaten_as_string = "5" # Because this is in quotes, this is a string.Let’s practice.
name and assign it the value Martycar and assign it the value Deloreanspeed and assign it the value 88mphWhat do you think happens below?
Guess before running it!
Make sure that, when you want a numerical value, you do not use quotes!
+ on:
5 + 5 = 10)"Doc" + "Brown" = "DocBrown")
Try adding this below:
We expect the sentence to be Marty is driving his Delorean 88mph. Is that what we got?
Let’s fix this, by adding in the spaces ourselves:
When printing, commas also create spaces.
Change your code below to:
This only works for printing, not for concatenation.
Do you think this will run? If yes, what does it print?
How about this? Does it run? If so, what does it print?
How about this? Does it run? If so, what does it print?
One last question. What does this do?
We learned a lot today!
+ - / * // %print function!Congrats! You’ve finished your first programming lesson!