Skip to main content

Concepts about variables and data types in Python with examples

Concepts about variables and data types in Python

Learn what variables and data types are in Python, how to declare them, and what they're used for.

What exactly is a variable?

Imagine you're moving. You have a lot of boxes. To avoid going crazy, you stick a label on one box that says"Libros"and you put your books inside.

In Python, the box is a space in your computer's RAM, the label is the variable name, and the content is the data.

Golden Rule: In Python, variables don't "store" data like physical boxes, but rather "point" to it like arrows. This is called a reference.

Declaration and Assignment

Unlike languages such as Java or C++, in Python you don't need to say "I'm going to store a number." Python is dynamic: it looks at the data and deduces what it is.

Python
# Así de simple se crea una variable
nombre_usuario = "Gabriel"
edad = 28
estatura = 1.75
es_desarrollador = True

Naming Rules

You can't name your variables just any old way. Python has rules and conventions:

  • Allowed: Letters, numbers, and underscores (_).

  • Forbidden: Starting with numbers or using spaces.

  • Case Sensitive: puntuacion and Puntuacion are two different variables.

The Snake Case Style🐍

In Python, by convention (PEP 8), we use snake_case (lowercase words separated by underscores).

✅last_login

❌lastLogin (This is CamelCase, used in JavaScript).

Primitive Data Types

To be an expert, you must know the 4 basic types that you will see 90% of the time:

| Type | Name | Example | Use |

| :-------- | :-----: | :-------: | ---------------------------------: |

|String|str|"Hola"| Text, messages, IDs. |

Integer|int|42| Whole numbers (no decimals). |

|Float|float|3.14| Numbers with decimals (precision). |

Boolean|bool|boolean| Logical values (True/False). |

Dynamic Typing and its Risks

Python allows you to change the type of a variable on the fly. This is very flexible but dangerous in large projects.

Python
puntos = 10      # Es un int
puntos = "Diez" # ¡Ahora es un str! Python no se queja.

Pro Tip: To avoid confusion in advanced courses, we use Type Hints:

Python
# Le decimos a otros devs que 'edad' debería ser un entero
edad: int = 25

Operations with Variables

Variables aren't just there to look pretty; they interact with each other.

Python
precio_producto = 100
impuesto = 0.21

precio_final = precio_producto * (1 + impuesto)
print(f"El total a pagar es: ${precio_final}")

String Manipulation

Python
nombre = "Gabriel"
apellido = "Maza"

# La forma moderna: f-strings
nombre_completo = f"{nombre} {apellido}"
print(nombre_completo.upper()) # Resultado: GABRIEL MAZA

Common Mistakes (To Avoid Frustration)

  1. NameError: Trying to use a variable you haven't created yet.
Python
print(puntos_totales) # Error: name 'puntos_totales' is not defined
  1. TypeError: Trying to combine things that don't belong together.
Python
edad = 25
texto = "Mi edad es " + edad # Error: No puedes sumar texto y números directamente
Resources

More about data types in Python:The Pythonist - Types in Python.