Python Programming: Variables
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!"
**Talking Points**:
"Because a variable is just a box, it can also hold strings. You tell Python that your variable will hold a string using quotation marks."
"Strings can be words, like apple. Strings are made of characters. A character is anything on your keyboard , such as a letter or a number. The string apple is 5 characters."
"A space counts as a character, too (it's on the keyboard). For example, "Marty McFly" is a string. So is this entire sentence:"
How Do I Create Strings in Python?
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.
Talking Points :
"To declare a string in Python, put it in quotes when you assign it to your variable."
Teaching Tips :
Go over the phrase "numerical variable" versus "string variable".
Walk through the difference between strings and numbers here. Be clear that they're both variables, just different types.
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?
We Do: Declaring Strings
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"`)
first_name = "Doc"
last_name = "Brown"
full_name = first_name + last_name
print full_name
# Prints "DocBrown".
**Talking Points**:
"The + operator means addition for numbers, but not for strings. When given string values, the + operator actually behaves differently — it concatenates, or combines, two strings together to make one big string. Using the + operator to combine the two strings together literally puts them next to each other instead of evaluating their total."
"Concatenation is a formal term for when strings are glued together. Here's an example of concatenation."
Stress that the plus sign will not add string variables with numbers in them.
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?
Teaching Tips :
Do this on the slide, but make sure students follow along.
Explain it as you go. After students see the lack of space, show them adding in the spaces yourself: sentence = name + " is driving his " + car + " " + speed. Then, show commas: print(name, "is driving his", car, speed)
Stress that commas only work for printing, not for concatenation.
Talking Points :
Python put the strings together, but do you notice anything wrong? There is no space between the words! This is because we didn't add the spaces in. It's just one of many reasons why we have to carefully watch our spacing and grammar!"
"Since a space is a character - it's on the keyboard - we can make it a string. Therefore, we can add it into our concatenation. By default, concatenation doesn't have spaces - you'll always have to add them yourself."
"You can also print directly; you don't necessarily need an extra variable. To print strings next to each other, you separate them with a comma. Then, Python will add the space for you. This isn't concatenating variables, but it's useful to know!" Change code to: sentence = name + " is driving his " + car + " " + speed
"When printing, commas also create spaces." Change code to: print(name, "is driving his", car, speed)
Repl.it Note : The code is:
name = "Marty"
car = "Delorean"
speed = "88mph"
Strings and Printing: Review
Strings are made with quotes:
name = "Marty"
car = "Delorean"
speed = "88"
String Concatenation - we need to add the spaces!
sentence = name + " is driving his " + car + " " + speed
string_numbers = "88" + "51"
# string_numbers = 8851
To easily create spaces while printing:
print ( name , "is driving his" , car , speed )
1 MINUTE
Teaching Tips
Quickly review what you covered so far.
Check for understanding.
Use the extra time here to check in with students and make sure they've got the hang of declaring, using, and printing numbers and strings.
Make sure students don't try to use commas for adding spaces outside of a print statement!
Discussion: Some Common Mistakes: 1
Do you think this will run? If yes, what does it print?
my_num
print ( my_num )
2 MINUTES
Teaching Tips :
Encourage discussion on this and following slides before giving the answer.
Talking Points :
"Let's look at some code with some common beginner mistakes. What do you think is wrong with the following code? Will it run at all? If yes, what does it print?"
Answer here: No, it won't run; you've declared a variable without assigning it to a value.
Discussion: Some Common Mistakes: 2
How about this? Does it run? If so, what does it print?
my_num = 5
print ()
Answer: It will run, but it won't print anything.
Discussion: Some Common Mistakes: 3
How about this? Does it run? If so, what does it print?
my_num = 5
my_string = "Hello"
print ( my_num + my_string )
Answer: This won't run, because Python will try to add a string and a number and throw an error.
Discussion: Some Common Mistakes: 4
One last question. What does this do?
my_num1 = "10"
my_num2 = "20"
print ( my_num1 + my_num2 )
Answer: There's nothing wrong with this one, except that it makes a string and not a number. It prints the string 1020.
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!
Teaching Tips :
Summarize the lesson and provide a preview of what’ s coming next.
Open your own blank repl.it in a new tab if needed to recap and be sure everyone is clear.
Additional Resources
Teaching tips:
Most lessons have additional resources at the end. Encourage students to explore these on their own time - don't go through them now.