Archangel Macsika Sikademy Image
Archangel Macsika

Learn About Variables and How to Create and Use them in Python

Tutorial 7 of 11 | 7 minutes read
Learn About Variables and How to Create and Use them in Python - Sikademy

Variables in Python

Variables are containers reserved in memory for storing data values.

When you create a variable, some space is allocated in the computer memory to hold the variable.

In python programming, the python interpreter allocates memory and decides what can be stored in the reserved memory depending on the data type of the variable.

Creating Variables in Python

Creating variables in python is fairly easy compared to other programming languages.

However, there are some things you should keep in mind when working on variables.

Variable Names

In Python programming, there are some rules for naming variables:

  • A variable name can be short e.g a, b, c, or a more descriptive name consisting of a single word or multiple combinations of words e.g gender, brand_of_detergent, area_of_circle.

  • Example:

    
    
    a = "Tablet"
    gender = "Female"
    brand_of_detergent = "Tide"
    
    
  • Variable names are case-sensitive. This means that gender, Gender, and GENDER are three different variables.

  • Example:

    The variables below are different.

    
    
    gender = "Male"
    Gender = "Female"
    GENDER = "Binary"
    
    
  • A variable name must start with either a letter or an underscore (_) character.

  • Example:

    The two variables below are different and acceptable in python.

    
    
    _gender = "Male"
    gender = "Female"
    
    
  • A variable name cannot start with a number.

  • Example:

    The variable names below are unacceptable in python programming.

    
    
    8 = "Male"
    12gender = "Female"
    
    
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

  • Example:

    The variable names below are acceptable in python programming.

    
    
    factor_of_8 = 2
    airspace_8_gate = "Flying to Mars"
    
    

    The variable names below are unacceptable in python programming.

    
    
    hashtag_#none = "Unacceptable variable due to # symbol"
    [email protected] = "Unacceptable due to the @ symbol"
    first-name = "Unacceptable due to the - symbol"
    
    
  • There should be no space between variable names in Python programming. Hence, a variable name made up of multiple words must not be separated with one or more spaces.

  • Example:

    The variable names below are unacceptable in python due to space.

    
    
    full name = "Archangel Macsika"
    name of school = "American University of Nigeria"
    
    

Variable Names with More than One Word

In Python programming, separating variable names with multiple words using space will provide an error.

The combination of more than one word for variable names can be difficult to read.

However, there are several techniques you can use to make them more readable:

Snake Case

Separate each word with an underscore character:

Example:



full_name = "Archangel Macsika"
name_of_school = "American University of Nigeria"

Pascal Case

Begin each word with a capital letter.

Example:



FullName = "Archangel Macsika"
NameOfSchool = "American University of Nigeria"

Camel Case

Begin each word with a capital letter, except for the first word.

Example:



fullName = "Archangel Macsika"
nameOfSchool = "American University of Nigeria"

Assigning Values to Variables in Python

Variable declarations and memory allocation happen automatically when you assign a value to a variable.

Python variables do not need explicit variable declarations like others to reserve memory space.

The equal sign = is used to assign values to variables.

The operand at the left side of the = operator is the name of the variable while the operand at the right side of the = operator is the value stored in the variable.

There are different methods of assigning values to variables:

One Value to One Variable

For starters, you can assign one value to one variable in Python programming language.

Example:



age = 24
full_name = "Archangel Macsika"

One Value to Multiple Variables

In python programming, you can assign the same value to multiple variables in a single line of code.

Example:



january_salary, february_salary, march_salary = 25000


This is same as writing:



january_salary = 25000
february_salary = 25000
march_salary = 25000

Multiple Values to Multiple Variables

In python programming, you can also assign multiple values to multiple variables in a single line of code.

Ensure the number of variables matches the number of values, otherwise, you will get an error.

  • Sikademy Tips

Example:



_2012_salary, _2013_salary, _2014_salary = 25000, 30000, 40000
first_name, last_name = "Archangel", "Macsika"

This is same as writing:



_2012_salary = 25000
_2013_salary = 30000
_2014_salary = 40000


first_name = "Archangel"
last_name = "Macsika"

Assign Values of a Collection to Variables

In python programming, if you have a collection of values in a list, tuple etc. you can extract the values and assign them to variables. This process is called unpacking in Python programming.

Example:



car_brands = ["Tesla", "Ford", "Mercedes"]
car_1, car_2, car_3 = car_brands
print(car_1)
print(car_2)
print(car_3)



Output:

Tesla
Ford
Mercedes

Global Variables in Python

Global variables are variables that can be accessed by anyone, both inside and outside of functions.

They are usually created outside of a function.

Example:



full_name = "Archangel Macsika"

def printName():
    print("Full name is " + full_name)

printName()

Output:

Archangel Macsika

When you create a variable with the same name as a global variable inside a function, this variable will be a local variable.

Local variables created within a function can only be used inside the function.

The global variable with the same name as the local variable will remain global and hold the original value.

Example:



name = "Archangel"

def printName():
    name = "Macsika"
    print("My name from the local variable is " + name)

printName()

print("My name from the global variable is " + name)

Output:

Macsika

Archangel

How to Use the global Keyword in Python

As we discussed earlier, when a variable is created inside a function, that variable becomes a local variable, and can only be used inside that function.

You also know that global variables are variables that are created outside of a function and can be accessed both inside and outside of functions.

Another way to create a global variable, inside a function, is to use the global keyword.

When you use the global keyword on a local variable, the variable becomes a global variable and can be accessed both inside and outside of functions.

Example:

The example below will produce an error because full_name is a local variable defined in the printName() function.



def printName():
    full_name = "Archangel Macsika"
    print("Full name is " + full_name)

print("Full name is " + full_name)

Let's fix the error using the global keyword.

Example:



def printName():
    global full_name
    full_name = "Archangel Macsika"

printName()

print("Full name is " + full_name)

Output:

Archangel Macsika

The global keyword can also be used to change a global variable inside a function.

Example:



name = "Archangel"

def printName():
    global name
    name = "Macsika"

printName()

print("My name from the global variable is " + name)

Output:

Macsika

Output Variables in Python

The print statement is often used in python to display variables.

Example:



full_name = "Archangel Macsika"
print(full_name)

Output:

Archangel Macsika

You can use the + operator to combine two or more variables or a variable with a string.

This is called concatenation in programming.

Example:



first_name = "Archangel"
last_name = "Macsika"
full_name = first_name + " " + last_name
print(full_name)
print("My full name is " + first_name + " " + last_name)

x = 15
y = 20
print(x + y)

Output:

Archangel Macsika
My full name is Archangel Macsika
35

Wrap Off

Variables are containers reserved in memory for storing data values. When you create a variable, some space is allocated in the computer memory to hold the variable. Now you have learned a lot about variables, and how to use them in Python. Practice the question below.

If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.

Tutorial Exercise

Create a variable for your name, favorite food, favorite subject, and assign the accurate value to it.


Enjoy this Tutorial? Please don't forget to share.