CS: Introduction to Python: Variables and Print
Practice storing values and displaying output in Python
CS: Introduction to Python: Variables and Print
Practice storing values and displaying output in Python
CS - Grade 6-8
- 1
What will this Python code display? age = 12 print(age)
Look at the value assigned to the variable before the print() statement.
The code will display 12 because the variable age stores the value 12, and print(age) displays that value. - 2
Write one line of Python code that displays the message Hello, Python!
One correct line is print("Hello, Python!"). The quotation marks tell Python that the words are text. - 3
Write two lines of Python code. First, create a variable named favorite_color and store a color in it. Second, print the variable.
Use quotation marks around a word like "green" or "blue" because it is text.
One correct answer is favorite_color = "blue" followed by print(favorite_color). This stores the text blue in the variable and then displays it. - 4
What will this Python code display? name = "Maya" print("Hi", name)
The code will display Hi Maya. The print() function displays the text "Hi" and then the value stored in the variable name. - 5
What will this Python code display? score = 10 score = score + 5 print(score)
A variable can be changed after it is first created.
The code will display 15 because score starts at 10 and then is updated by adding 5. - 6
Explain what a variable does in a Python program.
A variable stores a value so the program can use it later. The value can be text, a number, or another kind of data. - 7
Fix the error in this code so it displays the message Good morning. message = Good morning print(message)
Text values in Python strings need quotation marks.
The corrected code is message = "Good morning" followed by print(message). The words Good morning need quotation marks because they are text. - 8
Circle or list the valid Python variable names: 2score, score2, first_name, first name, class_count
Python variable names can use letters, numbers, and underscores, but they cannot start with a number.
The valid variable names are score2, first_name, and class_count. A variable name cannot start with a number and cannot contain a space. - 9
What will this Python code display? item = "stickers" count = 4 print(count, item)
The code will display 4 stickers. The print() function displays the value of count, then the value of item, with a space between them. - 10
Write Python code that stores the number 7 in a variable named apples and then displays I have 7 apples.
You can use commas inside print() to display both text and variable values.
One correct answer is apples = 7 followed by print("I have", apples, "apples."). This uses commas in print() to combine text and the variable value. - 11
Write a short Python program that stores length = 6 and width = 3, calculates area, and prints the area.
Use the multiplication symbol * in Python.
One correct program is length = 6, width = 3, area = length * width, and print(area). The program displays 18 because 6 times 3 equals 18. - 12
What will this Python code display? x = 5 y = 2 print(x + y) print("x + y")
The first print() statement will display 7 because it adds the values of x and y. The second print() statement will display x + y because the expression is inside quotation marks. - 13
Explain the difference between a variable name and a variable value using this example: pet = "dog".
The part on the left of the equal sign is the variable name.
In the example pet = "dog", the variable name is pet and the variable value is "dog". The name is how the program refers to the stored value. - 14
Fill in the blanks to create a variable named city with the value Denver and then print it. _____ = "Denver" print(_____)
The completed code is city = "Denver" and print(city). The same variable name must be used in both lines. - 15
Find and fix the problem in this code. Name = "Jordan" print(name)
Python is case-sensitive, so uppercase and lowercase letters must match exactly.
The problem is that Name and name are different variable names in Python because capitalization matters. One fix is Name = "Jordan" followed by print(Name).