Python Basics Flashcards
Variable Names
What do computers use variables for?
To remember informaion
Variable Names
To create a variable, we start by typing it name. Variable names need to be single words and therefore have no spaces.
city
Variable Names
If we want a variable name with multiple words, we use snake case. Snake case means using _ to connect the additional words.
home_city
Variable Names
What’s wrong with this variable name?
high score
There is a space between the words.
Variable Names
To help us understand what’s inside a variable we pick descriptive names.
home_city_province
Variable Names
What do we use snake case for?
To create variable names with multiple words.
Updating Variables
Why do we give variables descriptive names like city or population instead of c or p?
To help us understand what’s inside them.
Expressions
We can add string values together with a + sign
“Followers:” + “55”
Expressions
We call adding string values and expression as the output creates a single value.
In this example, only oh string is displayed output when we add “55” inside print().
print(“Followers:” + “55”)
Followers:55
Expressions
When expressions contain variables, they use the values in the variables, which we can see when adding “Followers:” to followers.
followers = “55”
print (“Followers:”+followers)
Expressions
Since expressions become values, we can store them in variables the same way as values.
See the example her where we’ll code label to display the expression.
label = “Posts:” + “13”
print(label)
Posts:13
Expressions
What’s the value of label?
label = “Name:” + “Joe”
“Name:Joe”
Expressions
What does the code display in the console?
user = “snoopdogg”
print(“Username:” + user)
Username:snoopdogg
Expressions
Add temperature variable to the expression.
temperature = “14”
print(temperature + “ degrees”
14 degrees
Expressions
Display Posts:55 in the console
Print (“Posts:” + “55”)
Posts:55
Expressions
Display Ms. Irene in the console
title = “Ms. “
name = “Irene”
print(title + name)
Ms. Irene
Expressions
Code the string value “likes” so that the code displays 40 likes.
number_of_likes = “40 “
print(number_of_likes + “likes”)
40 likes
Numbers
We’ve seen before that variables can also store numbers. Unlike strings, numeric values don’t use quotes “”
active_users = 5
Numbers
We can create expressions with numbers, too. Here, we can add numbers together with + 1
number_of_applications = 5 + 1
print(number_of_applications)
6
Numbers
We use the * sign to multiply numbers and the / sign to divide numbers. We’ll turn 0.5 into its percent value by multiplying it by 100.
percent = 0.5 * 100
print(percent)
50.0
Numbers
We can use variables with numbers for calculations, too. We’ll see it in action by adding 1 to number_of_steps.
number_of_steps = 70
print(“You’re on step:”)
print(number_of_steps + 1)
You’re on step:
71
Numbers
Since expressions become values, we can store calculation results in variables, like her where total contains private + public.
private = 3
public = 10
total = private + public
print(“Total posts:”)
print(total)
Total posts:
13
Numbers
What’s the value of speed?
speed = 300 + 5
305
Numbers
Why does this code display 31 instead of 4 in the console?
temperature = “3” + “1”
print(temperature)
31
Because “3” and “1” are string values
Numbers
How do we know that the score variable stores a number?
score = 40 + 4
Because there are no double quotes around 40 and 4
Numbers
What does the code display in the console?
area = “3 * 5”
print (area)
3 * 5
Numbers
Multiply the savings variable by the interest variable.
savings = 100
interest = 0.04
print (savings * interest)
4.0
Numbers
Divide sum_of_grades by students to get the average grade of a class.
sum_of_grades = 460
students = 5
print(sum_of_grades / students)
92.0
Numbers
Subtract discount from total and display the calculation result in the console.
total = 100
discount = 20
print(“Discounted price:”
print(total - discount)
Discount price:
80
Set months_per_year to 12
months_per_year = 12
print(months_per_year)
12