Skip main navigation

How do variables and operators work in programming?

Variables and operators are essential tools for programmers. Here, we take a look at how operators work and give you some workable examples.

Variables are essential elements of any programming language, not only because they can store information in memory, but because you can use them to work with that information. You might need to combine data, compare values, perform calculations and more. Here we take a look at operators, which are the basic tools you need.

Operators explained

To perform operations on your values and variables in Python programming (or any programming language, for that matter), you need to use certain operators. These operations can be arithmetic such as addition, subtraction, or multiplication, or they can be comparative.

These operators are some of the basic building blocks of software development. Almost every programme you create will need to manipulate variables, perform calculations, compare values and test conditions. So try to memorise as many of these operators as possible, to save yourself time.

Arithmetic operators

Arithmetic operations are used to perform mathematical operations. Here are the arithmetic operators you will use in Python:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus: This operator performs division, but returns the remainder. For example, 3 divides into 5 once, with a remainder of 2, so: 5 % 3 = 2 (5 / 3 = 1.6667).
** Exponent: Use this operator to raise the left- hand value to the power of the right-hand value. For example, 4** 2 = 16 ( 4 * 4 = 16).
// Floor division: This operator is more-or-less the opposite of modulus. It performs division, but returns the largest integer that fits into the divisor. For example, 3 divides into 5 once, with a remainder of 2, so: 5 // 3 = 1. But if one of the values is negative, the operation returns the total value rounded up to the next highest negative integer. For example, -5 //3 = -2(-5 / 3 = -1.6667).

 

Run this code snippet to practice some arithmetic operations:

small = 10
big = 20 
print("small + big = ",small + big)
print("small - big = ",small - big)
print("small * big = ",small * big)

Output:

small + big = 30 
small - big = -10 
small * big = 200 

Try to modify the operators and numbers. How do the results change?

Comparison operators

Just as arithmetic operations are used to perform mathematical operations, Python’s comparison operators compare the values of the operands, and return True or False based on the relationship. These are used to compare two values, and this kind of operation usually results in Boolean values.

Here is some code that returns True because both variables have the same value:

admin_password = 1234
user_input = 1234
print(admin_password == user_input)

Output:

True

Here are the comparison operators you will use in Python:

Operator Description
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

 

This code snippet demonstrates some comparison operators:

small = 10 
big = 20 
print(small == big)
print (small != big)

Output:

False 
True 

Try modifying the operators and values. How does the output change?

 

It’s easy to mix up how Python uses the equals character. Remember:

 

    • = assigns a value to a variable

 

    • == tests if two values or variables are the same.

 

If you have learnt other programming languages, such as Java, you might have noticed that they have the same or similar operators to Python.

Assigning user input to a variable

So far, you have assigned a value to a variable in the code. What happens if you need to get a value, such as a name or password, from the user while the programme is running and store that value in a variable? In this case, you can use the input() function.

Try this code example:

username = input('Enter your username: ')
print(username)

In this example, you took input from the user with the input() function. You included a prompt message that is printed to the user to let them know that you need some input. You also stored the input in the variable username.

Bear in mind that input taken with this function is stored as a string. You need to use the int() function to convert the string into numbers if you want to perform calculations with the data.

Variable naming conventions

When it comes to naming variables, there are certain conventions you need to follow. Your programme will work even if you don’t, but it’s good practice and other people will expect to see them:

    • Use a lowercase single letter, word, or words to name variables.
    • Separate words with underscores (_) to improve readability.
    • Variable names should be understandable and descriptive.

For example, supposeyou are making some kind of game and you need a variable to set three lives for the player. What should you name the variable?

    • A good approach: number_of_lives = 3
  • A bad approach: x = 3

If another person has to use or maintain your code later, or even if you need to work on your own programme again after a long break, it’s much easier to make sense of the code when there are descriptive variable names.

This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now