Variables and Data Types in Python: The Basics You Need to Know
Share
Variables are one of the first and most important concepts in programming. In Python, a variable is simply a name under which we store data. Unlike many other languages, in Python you don’t need to specify the variable type in advance.
To create a variable, just write its name and assign a value using the equals sign. For example: name = "Oleksandr" or age = 25. Python will automatically understand that the first case is text (string) and the second is a number.
The main data types in Python are:
- int — integers (5, -10, 0)
- float — floating-point numbers (3.14, -2.5)
- str — text strings ("Hello", "Python")
- bool — logical values (True or False)
Python allows easy conversion between data types. For example, you can convert a number to a string using the str() function.
Variable names should be meaningful. Instead of x = 10, it’s better to write user_age = 28. This makes the code much easier to read, especially as the project grows.
An important rule: variable names cannot start with a number and should not coincide with Python reserved words (such as print, if, for).
Python also has dynamic typing. This means the same variable can first store a number and then text. This flexibility is convenient for beginners but requires care.
It is best to practice working with variables on simple tasks. Try creating a program that asks for the user’s name and prints a greeting. This will help you better understand how variables work in practice.