top of page

Python - Variables


Variables in Python


Introduction

In this lesson, we will learn about the basic data types and how to store data in computer memory.


Variables

In programs, we use variables to store data. Intuitively, we can think of them as boxes with defined names in which information is stored. Each variable has a unique name that is used to refer to it. The name can be made of lowercase and uppercase letters, underscores , and numbers. It cannot start with a number. Some variable names could be for example: name, height or birthdate. Pay attention to the case of letters, as it does matter - age and Age are two different variables. It is important to name the variables in a way that the name describes what is stored in the variable.


Variables can store, among other things:

  • integers (type called int in Python),

  • real numbers (float),

  • strings (str).

Assignment operator

To assign a value to a variable, we use the = operator. We put the variable name on the left side of the operator, and the value we want to assign to it on the right side. For example:


age = 20
height = 1.75
name = "Adam"
 

creates three variables:

  • age - storing an integer (int),

  • height - storing a real number (float), where the fractional part is written after the dot,

  • name - storing a string (str). When defining a string, we put it in quotes.

To use a variable (access its value), we simply write its name. We can only refer to variables that already exist, i.e. variables that we have previously created. Let’s look at an example program that calculates the area of a rectangle:


side1 = 3
side2 = 4
area = side1 * side2
print(area)
 

When calculating the value of the area variable, we referred to the variables side1 and side2. Variables can be used as part of equations. In such cases, their values are used in place of their names. The above program will display:


12
 

Type conversions

We have seen two numeric types (int and float), a string type (str), and basic operations on them. Let’s say we have two values: one in a numeric variable and the other in a string variable. We want to calculate their sum. For example:


number = 5
text = "12"
 

Unfortunately, we cannot perform the number + text operation, because Python does not support the addition of numbers and strings. In this situation, we should convert both values to a common type. To convert a variable:

  • to an integer, we use the int(x) function - inside the parentheses, we specify the value x, that we want to convert;

  • to a real number, we use the float(x) function;

  • to a string, we use the str(x) function.

So, the solution in this case could be the following statement:


print(number + int(text))
 

which will display:


17
 

If we had converted the number variable to a string:


print(str(number) + text)
 

the program would display:


512
 

The plus sign for two strings in a concatenation operator (joining strings).

 

Train with us!

example lesson on codeforia

Programming is a crucial aspect of learning to code. You can practice it by solving tasks (micro-projects) available on our educational platform Codeforia. Solutions are automatically checked, and shortly after submitting your code, you'll find out whether it's correct or needs modification. We also invite you to join our regular online classes, where you can further develop your skills under the guidance of an experienced trainer.

Check out our available courses.

Comments


Kursy programowania dla dzieci i młodzieży.

bottom of page