Variables and Data Types

Understanding how variables and data types work is essential to master any programming language. In this article we will review the basic concepts of variables, operators, data types and type conversions using the Python language. We will cover both theory and practical examples so you can apply these concepts in your own programs.


A variable is a container to store data in the computer’s memory. We can think of it as a box with a label. The label is the variable name and inside the box its value is stored.

To declare a variable in Python we just write the name and assign a value:

1age = 28
2price = 19.95
3student = True

Variable names must start with letters or underscore, and can only contain letters, numbers and underscores. It is recommended to use meaningful names that represent the purpose of the variable.

In Python variables do not need to be declared with a particular type. The type is inferred automatically when assigning the value:

1age = 28 # age is integer type
2price = 19.95 # price is float type
3single = True # single is boolean type

Once assigned, a variable can change its value at any time:

1age = 30 # We change age to 30

Scope and lifetime

The scope of a variable refers to the parts of the code where it is available. Variables declared outside functions are global and available throughout the file. Variables inside a function are local and only visible within it.

The lifetime is the period during which the variable exists in memory. Local variables exist while the function executes, then they are destroyed. Global variables exist while the program is running.


Assignment with the = operator allows changing or initializing a variable’s value:

1number = 10
2number = 20 # Now number is 20

There are also compound assignment operators like += and -= that combine an operation with assignment:

1number += 5 # Adds 5 to number (number = number + 5)
2number -= 2 # Subtracts 2 from number

Data types

Data types define what kind of value a variable can store. Python has several built-in types, including:

Numerical: To store integer, float, and complex numeric values:

1integer = 10
2float = 10.5
3complex = 3 + 4j

Strings: To store text:

1text = "Hello World"

Boolean: For True or False logical values:

1true_variable = True
2false_variable = False

Collections: To store multiple values like lists, tuples and dictionaries:

  • Lists: Mutable sequences of values:

    1list = [1, 2, 3]
  • Tuples: Immutable sequences of values:

    1tuple = (1, 2, 3)
  • Dictionaries: Key-value pair structures:

    1dictionary = {"name":"John", "age": 20}

It is important to choose the data type that best represents the information we want to store.


Operators allow us to perform operations with values and variables in Python. Some common operators are:

  • Arithmetic: +, -, *, /, %, //, **

  • Comparison: ==, !=, >, <, >=, <=

  • Logical: and, or, not

  • Assignment: =, +=, -=, *=, /=

Let’s see concrete examples of expressions using these operators in Python:

 1# Arithmetic
 25 + 4 # Addition, result 9
 310 - 3 # Subtraction, result 7
 44 * 5 # Multiplication, result 20
 6# Comparison
 75 > 4 # Greater than, result True
 87 < 10 # Less than, result True
10# Logical
11True and False # Result False
12True or False # Result True
13not True # Result False
15# Assignment
16number = 10
17number += 5 # Adds 5 to number, equivalent to number = number + 5

Each type of operator works with specific data types. We must use them consistently according to our variable data types.

Type conversions

Sometimes we need to convert one data type to another to perform certain operations. In Python we can convert explicitly or implicitly:

Explicit: Using functions like int(), float(), str():

1float = 13.5
2integer = int(float) # converts 13.5 to 13
4text = "100"
5number = int(text) # converts "100" to 100

Implicit: Python automatically converts in some cases:

1integer = 100
2float = 3.5
3result = integer + float # result is 103.5, integer converted to float

Some conversions can generate data loss or errors:

1float = 13.5
2integer = int(float)
4print(integer) # 13, decimals are lost

To prevent this we must explicitly choose conversions that make sense for our data.


In this article we reviewed key concepts like variables, operators, data types and conversions in Python. Applying these concepts well will allow you to efficiently manipulate data in your programs. I recommend practising with your own examples to gain experience using these features. Good luck in your Python learning!