Variables and Data Types

In this class, we will learn how to declare and assign variables, the basic data types in Python, and how to perform operations on variables.

Variable declaration and assignment: Link to heading

A variable is a space in computer memory that is used to store a value. To declare a variable, use the name of the variable followed by the equals sign (=).

name = "John"
age = 20

In this example, two variables are declared: name and age. The variable name is assigned to the text string “John” and the variable age is assigned to the integer value 20.

As you could see in the previous example, variables can be of different types. Next we will know the most basic ones.

Basic data types: Link to heading

  • Integers (int): Whole numbers, such as 1, 2, 3, etc.
  • Floats (float): Decimal numbers, such as 1.2, 3.14, etc.
  • Character strings (str): Text strings, such as “Hello World”, “Python”, etc.
  • Boolean (bool): Boolean values, such as True or False.

Examples: Link to heading

# int
integer_number = 10

#float
decimal_number = 3.14

#str
character_string = "Hello World"

#bool
true = True
false = False

Some aspects that must be taken into account when creating variables are the following:

1. You can assign values ​​to multiple variables on a single line

Example:

x, y = 10, 20
print(x, y)

2. Basic operators can be used with numerical variables and character strings

Example:

ten, twenty = 10, 20
thirty = ten + twenty
print(thirty)

name = 'David'
last name = 'Smith'
full_name = first name + " " + last name
print(full_name)

3. Numeric variables and character strings cannot be combined with operators

Example:

x, y = 10, 20
name = 'David'
print(x + y + name)

Exercise Link to heading

Print variables that describe a person to your console. For example, name, age, height, etc.

<< Hello World Lists >>