Numbers
Part of the Fundamentals section of Coddy's Python journey — lesson 4 of 77.
Variables are containers that hold data values. They are used to store, manipulate, and display information within a program.
In short, a variable is like a memory unit that we can access by typing the name of the variable.
Each variable has a unique name and a value that can be of different types. Python is capable of automatically detecting the variable type, which makes coding more efficient.
To initialize a variable, we use the following format:
variable_name = valueLet's take a look at the different types of numbers:
int - an integer, such as 1 or -2.
float - real number, such as 1.32 or 0.98.
For example:
To initialize a variable of type int with the name a and the value 3:
a = 3To initialize a variable of type float with the name b and the value 13.2:
b = 13.2Note: Variable names cannot start with a number. Use underscores to separate words (e.g., player_name), not spaces or hyphens..
Challenge
BeginnerWrite code that initializes a variable named var with the value 5.
- Replace the
?with the correct value - Lines starting with
#are comments - they explain what to do but don't affect your code - Only change the line that says
var = ?
Cheat sheet
Variables in Python:
- Containers that hold data values
- Used to store, manipulate, and display information
- Have a unique name and a value
- Python automatically detects variable type
Variable initialization format:
variable_name = valueNumber types:
int: integers (e.g., 1, -2)float: real numbers (e.g., 1.32, 0.98)
Examples:
a = 3 # int
b = 13.2 # floatTry it yourself
# Type your code below
var = ?
# Don't change the line below
print(f'var = {var}')This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2