Menu
Coddy logo textTech

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 = value

Let'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 = 3

To initialize a variable of type float with the name b and the value 13.2:

b = 13.2

Note: Variable names cannot start with a number. Use underscores to separate words (e.g., player_name), not spaces or hyphens..

challenge icon

Challenge

Beginner

Write 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 = value

Number types:

  • int: integers (e.g., 1, -2)
  • float: real numbers (e.g., 1.32, 0.98)

Examples:

a = 3    # int
b = 13.2 # float

Try it yourself

# Type your code below
var = ?

# Don't change the line below
print(f'var = {var}')
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals