Variables#
Variables store data in a computer’s memory. A variable refers to a memory address in which data is stored. A variable can have a short name (like x, y, z), but a more descriptive name (firstname, lastname, age, country) is highly recommended. Number at the beginning, special character, hyphen are not allowed when naming a variable.
Python Variable Naming Rules#
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (firstname, Firstname, FirstName and FIRSTNAME) are different variables)
Here are some example of valid variable names:
first_name
last_name
firstname
lastname
age
country
city
capital_city
_if # when we want to use reserved word as a variable
year_2021
year2021
current_year_2021
birth_year
num1
num2
Invalid variables names:
first-name
first@name
first$name
num-1
1num
We will use standard Python variable naming style which has been adopted by many Python developers, which is the snake case (snake_case
) variable naming convention. We use an underscore character after each word for a variable containing more than one word (eg. first_name, engine_rotation_speed, day_of_week). The example below is an example of standard naming of variables, underscore is required when the variable name is more than one word.
When we assign a certain data type to a variable, it is called a variable declaration. For instance in the example below my first name is assigned to a variable first_name. The equal sign is an assignment operator. Assigning means storing data in the variable. The equal sign in Python is not equality as in Mathematics.
Example:
# Variables in Python
first_name = "Aurora"
last_name = "Luna"
country = "Indonesia"
city = "Palembang"
age = 6
is_student = True
skills = ["HTML", "CSS", "JS", "React", "Python"]
person_info = {
"first_name": "Aurora",
"last_name": "Luna",
"country": "Indonesia",
"city": "Palembang",
}
Let us use the print()
and len()
built-in functions. The print()
function takes an unlimited number of arguments. An argument is a value which we can be passed or put inside the function parenthesis, see the example below.
Example:
print("Hello, World!") # The text Hello, World! is an argument
print(
"Hello", ",", "World", "!"
) # it can take multiple arguments, four arguments have been passed
print(len("Hello, World!")) # it takes only one argument
Hello, World!
Hello , World !
13
Let us print and also find the length of the variables declared at the top:
Example:
# Printing the values stored in the variables
print("First name:", first_name)
print("First name length:", len(first_name))
print("Last name: ", last_name)
print("Last name length: ", len(last_name))
print("Country: ", country)
print("City: ", city)
print("Age: ", age)
print("Student: ", is_student)
print("Skills: ", skills)
print("Person information: ", person_info)
First name: Aurora
First name length: 6
Last name: Luna
Last name length: 4
Country: Indonesia
City: Palembang
Age: 6
Student: True
Skills: ['HTML', 'CSS', 'JS', 'React', 'Python']
Person information: {'first_name': 'Aurora', 'last_name': 'Luna', 'country': 'Indonesia', 'city': 'Palembang'}
Declaring Multiple Variable in a Line#
Multiple variables can also be declared in one line:
Example:
first_name, last_name, country, age, is_student = (
"Aurora",
"Luna",
"Indonesia",
6,
True,
)
print(first_name, last_name, country, age, is_student)
print("First name:", first_name)
print("Last name: ", last_name)
print("Country: ", country)
print("Age: ", age)
print("Student: ", is_student)
Aurora Luna Indonesia 6 True
First name: Aurora
Last name: Luna
Country: Indonesia
Age: 6
Student: True
Getting user input using the input()
built-in function. Let us assign the data we get from a user into first_name
and age
variables.
Example:
first_name = input("What is your name: ")
age = input("How old are you? ")
print(first_name)
print(age)
Aurora
6