What are data types and why do we need them?
The data type determines what kind of information the variable stores and what operations can be performed with it. It's like containers of different shapes: some can hold liquid, others can hold solid objects, and others are suitable for something specific.
Understanding data types helps you avoid mistakes and write more efficient code. For example, you cannot add a number and text without a special conversion, just as you cannot pour water into a colander.
Numbers
The numeric data type is used to store numeric values. In most programming languages, there are two main subtypes of numbers:
Integers are numbers without a fractional part:
возраст = 25
количество_товаров = 100
температура = -5Floating-point numbers are numbers with a fractional part:
цена = 99.99
пи = 3.14159
температура_точная = 36.6Operations with numbers
You can perform mathematical operations with numbers:
# Addition
сумма = 10 + 5 # 15
# Subtraction
разность = 10 - 5 # 5
# Multiplication
произведение = 10 * 5 # 50
# Division
частное = 10 / 5 # 2.0
# Exponentiation
степень = 2 ** 3 # 8
# Remainder of division
остаток = 10 % 3 # 1It is important to remember: when dividing integers in many languages, the result can be a fractional number, even if it is divisible by a whole number.

Strings
Strings are used to store text information. It can be a single letter, a word, a sentence, or an entire text.
Strings are usually enclosed in quotation marks — single or double:
имя = "Maria"
приветствие = 'Hello, world!'
адрес = "10 Pushkin Street"Working with strings
Concatenation (string joining):
имя = "Alexey"
фамилия = "Ivanov"
полное_имя = имя + " " + фамилия # "Alexey Ivanov"Line length:
текст = "Programming"
длина = len(текст) # 16Reference to symbols:
слово = "Python"
первая_буква = слово[0] # "P"
последняя_буква = слово[-1] # "n"String methods:
текст = "hello world"
верхний_регистр = текст.upper() # "HELLO WORLD"
замена = текст.replace("world", "Python") # "hello Python"Escaping special characters
Sometimes you need to use quotation marks or other special characters in the string. To do this, use a backslash:
цитата = "He said: \"Hello!\""
путь = "C:\\Users\\Documents"
многострочный_текст = "First line\nSecond line"Boolean values
The logical data type is the simplest type that can only have two values: true or false. It is named after the mathematician George Boole.
is_active = True
has_subscription = False
is_admin = TrueApplying logical values
Logical values are most often used in conditional statements:
возраст = 20
совершеннолетний = возраст >= 18 # True
if совершеннолетний:
print("Access allowed")
else:
print("Access denied")Comparison operations
Comparison operations return logical values:
5 > 3 # True (more)
5 < 3 # False (less)
5 == 5 # True (equals)
5 != 3 # True (not equal)
5 >= 5 # True (greater than or equal to)
5 <= 3 # False (less than or equal to)Logical operations
Logical values can be combined using logical operators:
# AND — true if both conditions are true
True and True # True
True and False # False
# OR — true if at least one condition is true
True or False # True
False or False # False
# NOT — inverts the value
not True # False
not False # TruePractical example:
возраст = 25
имеет_права = True
может_водить = возраст >= 18 and имеет_права # TrueData type conversion
Sometimes it is necessary to convert data from one type to another. This process is called type casting or conversion.
# String to number
строка_число = "42"
число = int(строка_число) # 42
# Number in line
возраст = 25
текст_возраст = str(возраст) # "25"
# String to boolean
булево = bool("True") # True
пустая_строка = bool("") # False
# Number to boolean
bool(0) # False
bool(1) # True
bool(42) # TrueImportant: be careful when converting types, as not all conversions are intuitive. For example, a non-empty string will always be True when converted to a logical type, even if it is a "False" string.
Common mistakes of beginners
Attempt to perform operations with incompatible types:
# Error!
результат = "5" + 5
# Correct:
результат = int("5") + 5 # 10
# or
результат = "5" + str(5) # "55"Using the comparison operator instead of assignment:
# Incorrect:
if возраст = 18: # Error!
# Correct:
if возраст == 18:Comparison of strings and numbers:
"10" == 10 # False, because they are different typesData type validation
In Python, you can check the type of a variable using the type() function:
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type(True)) # <class 'bool'>Conclusion
Understanding basic data types is the foundation of programming. Numbers allow you to perform calculations, strings work with text, and logical values help you make decisions in the program. As you learn, you will encounter more complex data types such as lists, dictionaries, and objects, but they are all built on these basic concepts.
We recommend learning the basics in Kodik app!
You will find:
Interactive lessons with step-by-step explanations
Cool practical tasks, which will help to consolidate the material
Real projects, where you will use if, else, and elif to create games and useful programs
Tasks of different levels of complexity - from simple to advanced
Instant code verification and tips if something went wrong
Practice working with different types of data, experiment with transformations and operations. Over time, working with data types will become intuitive, and you will automatically select the appropriate type for each task.
Join our Telegram channel and get communication with like-minded people and support from experienced programmers.
