A variable is a name that points to a value stored in memory. When you assign a value to a variable, Python sets aside space for it, and you can read or change that value as the program runs.
Python variables are dynamically typed: you don't declare a type up front, and the same name can point to a number now and a string later.
Writing Python variables itself also has certain rules, namely:
- The first character must be a letter or underscore
_ - The next character can be a letter, underscore
_or number - Characters in variable names are case-sensitive. This means lowercase and uppercase letters are distinguished. For example, the variable
firstNameandfirstnameare different variables.
To start creating variables in Python it is very easy, you simply write the variable then fill it with a value by adding an equal sign = followed by the value you want to enter.
Creating and printing a variable
name = "John Doe"
print(name)
Changing value and data type
Variables can change their value and even their type during execution:
age = 20
print(age)
print(type(age))
age = "twenty one"
print(age)
print(type(age))
Combining variables
first_name = "Bob"
last_name = "Smith"
full_name = first_name + " " + last_name
age = 22
hobby = "Swimming"
print("Name:", full_name)
print("Age:", age)
print("Hobby:", hobby)
Valid variable names
my_variable = "Hello"
_private = "Hi"
count2 = 100
print(my_variable, _private, count2)
Using variables in calculations
length = 10
width = 5
area = length * width
print("Area:", area)