Beginner âĸ Python
Variables
đĻ What is a Variable?
A variable is like a labelled box that stores information. We give the box a name and put a value inside it. Variables can store numbers, words, and other types of information. We can then use the variable name later in our program.
name = "Alex"
age = 10
print(name)
print(age)Alex 10
đ Everything in Variables
Learn how Python stores information using variables and how to work with values. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.
đĻ What is a Variable?
A variable is like a labelled box that stores information. We give the box a name and put a value inside it. Variables can store numbers, words, and other types of information. We can then use the variable name later in our program.
name = "Alex" age = 10 print(name) print(age)đ Changing Variables
The value stored inside a variable can be changed. When we assign a new value to the same variable, Python replaces the old value with the new one. This is useful when information changes, such as a player's score in a game.
score = 10 print(score) score = 20 print(score)đˇī¸ Naming Variables
Good variable names make code easier to understand. A variable name can contain letters, numbers, and underscores, but it cannot start with a number. Variable names are case-sensitive, which means name and Name are treated as different names. Using clear names such as student_name is better than using names such as x.
student_name = "Sam" student_age = 11 print(student_name) print(student_age)đ§Š Variables with Different Values
Variables can store different kinds of values. For example, one variable can store a person's name, another can store an age, and another can store whether someone is a student. Python keeps track of the type of value stored in each variable.
name = "Emma" age = 12 is_student = True print(name) print(age) print(is_student)đ§Ž Using Variables in Calculations
We can use variables in mathematical calculations. Instead of writing the same number again and again, we can store it in a variable and use the variable name in our calculation. This makes programs easier to change and understand.
apples = 5 more_apples = 3 total = apples + more_apples print(total)
