Python Tutorials

Choose a lesson to begin

Variables and Data Types

Variables are like labeled boxes that store information. Once you put data in a variable, you can use it over and over again.

Creating Variables

To create a variable, use the = sign (assignment operator):

name = "Vidar"
age = 22

Now name holds the text "Vidar" and age holds the number 22.

Data Types

Python has several basic data types:

Strings (Text)

Strings are text, wrapped in quotes:

greeting = "Hello!"
city = 'Roskilde'  # single or double quotes work

Numbers

Integers are whole numbers:

score = 100
year = 2024

Floats are decimal numbers:

price = 19.99
temperature = -5.5

Booleans (True/False)

Booleans represent yes/no, true/false:

is_raining = True
is_sunny = False

Note: True and False are capitalized!

Using Variables

Once created, use variables by their name:

name = "Bob"
age = 30
print(name, "is", age, "years old")

Output: Bob is 30 years old

Variables Can Change

You can update a variable's value:

score = 0
print(score)  # 0

score = 10 print(score) # 10

score = score + 5 print(score) # 15

Naming Rules

Good names: Bad names:

Try It Out!

  • Create variables for your name, age, and favorite color
  • Print them in a sentence
  • Change one variable and print again
  • Store two numbers in variables and print their sum
  • Next Steps

    Ready to do math with Python? Check out Math and Arithmetic next!

    🐍 Python Runner