Variables are like labeled boxes that store information. Once you put data in a variable, you can use it over and over again.
To create a variable, use the = sign (assignment operator):
name = "Vidar"
age = 22Now name holds the text "Vidar" and age holds the number 22.
Python has several basic data types:
Strings are text, wrapped in quotes:
greeting = "Hello!"
city = 'Roskilde' # single or double quotes workIntegers are whole numbers:
score = 100
year = 2024Floats are decimal numbers:
price = 19.99
temperature = -5.5Booleans represent yes/no, true/false:
is_raining = True
is_sunny = FalseNote: True and False are capitalized!
Once created, use variables by their name:
name = "Bob"
age = 30
print(name, "is", age, "years old")Output: Bob is 30 years old
You can update a variable's value:
score = 0
print(score) # 0score = 10
print(score) # 10
score = score + 5
print(score) # 15
age and Age are different)user_score not xplayer_nametotal_scoreis_game_overx (not descriptive)2fast (starts with number)my-name (hyphens not allowed)Ready to do math with Python? Check out Math and Arithmetic next!