
Python Programming: Variables
Lesson Objectives
After this lesson, you will be able to…
- Create and re-assign numerical and string variables.
- Use numerical operators.
- Print complex variable structures.
What’s a Variable?
Turn to the person next to you, and together come up with as many definitions for the word “variable” as you can.
- Consider contexts such as mathematics, the sciences, weather, etc.
- No cheating! Phones off and laptops closed.
Variable
Variables:
- Are boxes that can hold all kinds of information for you.
- Make it easier to store and re-use values.
- Are the most basic piece of code.
To use a variable, we simply announce that we want to use it (we declare it).
Naming Conventions: Mistakes and Syntax
Some common naming mistakes:
- Not using meaningful names.
delicious = 3 doesn’t mean anything - cupcakes_ive_eaten = 3 does!
- Case sensitivity (
CUPCAKES_IVE_EATEN and cupcakes_ive_eaten are not the same!)
- No spaces or punctuation (“cupcakes i’ve eaten” isn’t allowed)
- This is invalid syntax
- Use snake_case:
lowercase_letters_with_underscores (it’s in the official Python style guide)
Discussion: Changing Values
What if, later, you eat more cupcakes? Now, this is wrong.
What do you think we need to do?
Discussion: Reassigning Variables
In the example below, what do you think the output of the code is?
Quick Review
A variable is a box that holds a value.
It can be declared, called, and changed within your program.
When declaring variables, syntax and naming conventions matter!
Variables can be reassigned as often as you like, but only the most recent declaration counts.
UP NEXT: Math!
Mathematical Operators
Math works on numerical variables, too!
- The
+, -, * (multiply), and / (divide) operators work just like they do with regular math.
Even More Mathematical Operators
Beyond the +, -, * (multiply), and / (divide) operators, we have modulus and exponents.
Math On The Same Variable
You can reassign a variable using that very same variable - or other variables!
Partner Exercise: Mathematical Operators
Pair up and choose roles:
Try to code the below:
Reassignment Shorthand
This is okay:
But this is better:
This works with +=, -=, *=, /= - any math operations.
Partner Exercise: Numerical Reassignment
Get with the same partner, but switch driver and navigator roles.
In the environment below, follow the prompts:
Important Aside: Even or Odd?
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.
Modding by 2 only gives a 0 or a 1.
Quick Review
- A variable is a value that can be defined, declared, called and changed within your program.
- Naming:
- Variable names are case sensitive.
- Use
snake_case!
- Variables can be reassigned as often as you like, but only the most recent declaration counts.
- Python can do math using operators, such as
+, -, *, and /
- You can shorthand the math assignments:
my_num += 7
Taking a Breather
That was a lot of math!
When it comes down to it, computers operate with a simple, straightforward logic.
Let’s switch gears. Up next: Strings!
Introducing Strings
A character is:
- Anything on your keyboard , such as a letter or a number.
- “Apple” is five characters: a, p, p, l, e.
- Spaces count! (they’re on the keyboard!)
A string is:
- A complete list of characters.
- “Apple”
- “Chocolate Cupcake”
- This entire sentence: “Hello, you are 1 of a kind!”
How Do I Create Strings in Python?
You tell Python that your variable will hold a string using quotation marks.
We Do: Declaring Strings
A “We Do” means let’s practice together. Follow along!
- We’ll declare a variable called
name and assign it the value Marty
- We’ll declare a variable called
car and assign it the value Delorean
- We’ll declare a variable called
speed and assign it the string value "88"
- We’ll print out these variables
- We’ll add
4 to speed- what happens?
String Concatenation
+ on:
- Numerical variables adds (
5 + 5 = 10).
- String variables concatenate (
"Doc" + "Brown" = "DocBrown").
- Pssst: Pronunciation tip: con-CAT-en-ATE
- Numerical strings concatenate to new strings! (
"5" +“4”=“54”`)
We Do: Spaces in Concatenation
It’s another “We Do.” Let’s do this together - follow along!
To begin: sentence = name + "is driving his" + car + speed
We expect the sentence to be Marty is driving his Delorean 88mph. Is that what we got?
Strings and Printing: Review
Strings are made with quotes:
String Concatenation - we need to add the spaces!
To easily create spaces while printing:
Discussion: Some Common Mistakes: 1
Do you think this will run? If yes, what does it print?
Discussion: Some Common Mistakes: 2
How about this? Does it run? If so, what does it print?
Discussion: Some Common Mistakes: 3
How about this? Does it run? If so, what does it print?
Discussion: Some Common Mistakes: 4
One last question. What does this do?
Q&A and Summary
We learned a lot today!
- We created, used, and re-assigned number and string variables.
- We used the numerical operators
+ - / * // %
- We did some complex stuff with the
print function!
Congrats! You’ve finished your first programming lesson!
Additional Resources