Python · Variables and types
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Variable assignment
Which operator is used to assign a value to a variable?
Options
• ```=```
• ```==```
• ```->```
• ```:```
Hint : The ```=``` operator is used for assignment. ```==``` is used for comparison.
Exercise 2 — Create a variable
Create a variable named
codemon_health
and assign it the int value 100.
____ ____ ____ print(codemon_health)
Options: codemon_health codemon health = == 100 "100"
Hint : Use snake_case for variable names and ```=``` for assignment.
Exercise 3 — Valid variable name
Which variable name follows Python's snake_case convention?
Options
• ```player_score```
• ```PlayerScore```
• ```player-score```
• ```player score```
Hint : snake_case uses lowercase letters with underscores between words.
Exercise 4 — Match values with types
Match each value with its correct type.
Items to match
• 42
• 3.14
• "Hello"
• True
Definitions
• int
• float
• str
• bool
Exercise 5 — Variable reassignment
What happens to the previous value when you reassign a variable?
Options
• It is lost and replaced by the new value
• It is stored in memory
• You get an error
• Both values coexist
Hint : A variable can only hold one value at a time.
Exercise 6 — Basic operations
Fill in the blanks with the correct operators.
10 ____ 5 = 15 10 ____ 5 = 5 10 ____ 5 = 50
Options: + + - - * * / //
Hint : That's basic math.
Exercise 7 — Division types
What is the result of
7 // 2
?
Options
• ```3```
• ```3.5```
• ```4```
• ```1```
Hint : ```//``` is integer division, it returns only the whole part.
Exercise 8 — Operators and names
Match each operator with its name.
Items to match
• **
• %
• //
• /
Definitions
• Power
• Modulo
• Integer division
• Division
Exercise 9 — Type casting
What does
float(5)
return?
Options
• ```5.0```
• ```5```
• An error
• ```"5.0"```
Hint : ```float()``` converts an integer to a decimal number.
Exercise 10 — Converting types
Convert the float
3.7
to an integer.
number = 3.7 number = ____(number) print(number)
Options: int float str bool type
Hint : To cast to an integer, use ```int()```. This will remove the decimal part.
Exercise 11 — Mixed operations
What type is the result of
5 + 2.0
?
Options
• ```float```
• ```int```
• ```str```
• Error
Hint : Python always returns the most precise type when mixing int and float.
Exercise 12 — Correct string
Which string is correctly written?
Options
• ```message = 'Hello world'```
• ```message = 'Hello world"```
• ```message = "Hello world'```
• ```message = Hello world```
Hint : Strings must start and end with the same type of quotes.
Exercise 13 — Create strings
Complete to create a valid string with quotes inside.
text = ____I'm happy____ print(text)
Options: " " ' ' \ \
Hint : Use double quotes to wrap a string containing single quotes.
Exercise 14 — Boolean syntax
Which is the correct way to write a Boolean in Python?
Options
• ```True```
• ```true```
• ```TRUE```
• ```"True"```
Hint : Booleans in Python start with a capital letter: True or False.
Exercise 15 — Naming errors
Match each incorrect variable name with its error.
Items to match
• 3_players
• player-name
• Player_Name
• player name
Definitions
• Starts with a number
• Contains a hyphen
• Uses capital letters
• Contains a space
Exercise 16 — Type function
Which function shows the type of a variable?
Options
• ```type()```
• ```show()```
• ```typeof()```
• ```getType()```
Hint : Use ```type(variable)``` to see the type of a variable.
Exercise 17 — Power and modulo
Complete the calculations.
2 ____ 3 = 8 10 ____ 3 = 1
Options: ** ** % % // / *
Hint : ```**``` is power, ```%``` is modulo, ```//``` is floor division and ```/``` is regular division.
Exercise 18 — Modulo result
What is the result of
15 % 4
?
Options
• ```3```
• ```4```
• ```3.75```
• ```11```
Hint : ```%``` is the modulo, it returns the remainder of a division.
Exercise 19 — Float to int
What is the result of
int(7.9)
?
Options
• ```7```
• ```8```
• ```7.9```
• ```7.0```
Hint : ```int()``` turns a number into an integer, it does not round, it basically removes the decimal part.
Exercise 20 — Multiple variables
Create two variables:
attack
with value 25 and
defense
with value 30.
____ = 25 ____ = ____ print(attack) print(defense)
Options: attack defense 30 Attack Defense
Hint : Remember to use lowercase for variable names (snake_case).
Exercise 21 — Calculate results
Match each calculation with its result.
Items to match
• 2 ** 4
• 20 // 6
• 20 % 6
• 20 / 6
Definitions
• 16
• 3
• 2
• 3.333...
Exercise 22 — String with apostrophe
How to correctly create a string containing
don't
?
Options
• ```text = "don't"```
• ```text = 'don't'```
• ```text = don't```
• ```text = "don't'```
Hint : Use double quotes to wrap a string containing an apostrophe (single quote).
Exercise 23 — Boolean values only
How many possible values can a Boolean have?
Options
• 2 (True and False)
• 3 (True, False and None)
• Infinite
• 4 (True, False, Yes, No)
Hint : Booleans value are True and False.
Exercise 24 — Reassign value
Change the value of
level
from 1 to 2. The type of the variable should remain the same.
level = 1 ____ ____ ____ print(level, type(level))
Options: level Level = == 2 "2"
Hint : Use the same variable name with ```=``` to reassign a new value.
Exercise 25 — Type after operation
What type will
x
be after this code:
x = 10 / 2
?
Options
• ```float```
• ```int```
• ```str```
• ```bool```
Hint : Division ```/``` always returns a float.
Back to the course