Understanding data types in Python is one of the first steps to mastering the language. Knowing how to store and manipulate information correctly makes writing code easier and improves efficiency and readability. In this article, I invite you to explore the primitive types, which are the foundation for working with text, numbers, and logical values in Python.
Primitive types are the basic building blocks of data. They let you store and manipulate simple values, such as numbers, text, and logical values. Each primitive type plays an important role in Python development:
Primitive types are simple to use and efficient for everyday tasks. They come with built-in methods and features that make your work easier and more productive.
The string type (str) is essential for handling text. In Python, strings can be declared using single (‘) or double (“) quotes.
name = "Python"
kind = 'Programming Language'
# Concatenate strings
message = name + " is a " + kind
print(message) # Output: Python is a Programming Language
# String length
print(len(name)) # Output: 6
# Slicing
print(name[0:3]) # Output: Pyt Whenever I need to transform or manipulate text, string methods are indispensable tools:
text = " Hello, World! "
print(text.strip()) # Removes spaces at the beginning and end
print(text.lower()) # Converts to lowercase If you want to explore further, I recommend the official documentation for the str type.
The boolean type (bool) is essential for handling conditions and logic. It represents only two values: True or False.
active = True
admin = False
# Checks
if active:
print("The user is active.")
else:
print("The user is not active.")
# Logical operators
print(active and admin) # Output: False
print(active or admin) # Output: True A practical example I use often is checking conditions for flow control. Logical operations, such as the following comparison, also return boolean values:
result = 10 > 5
print(result) # Output: True If you want to dig deeper, the official documentation is a great resource.
The integer type (int) is used to work with whole numbers in Python. It is very versatile, whether for simple or complex calculations.
age = 30
people = 150
# Math operations
total = age + people
print(total) # Output: 180
# Integer division
result = people // 4
print(result) # Output: 37
# Exponentiation
power = 2 ** 3
print(power) # Output: 8 Something I find amazing is that integers in Python have unlimited precision. This is useful in situations that require extremely large values. Have you ever had to deal with huge numbers? If so, this feature will be essential for you.
To learn more, see the documentation for the int type.
The float type is used to handle floating-point (decimal) numbers. It is essential for more precise calculations.
price = 19.99
rate = 0.1
# Final price calculation
final_price = price + (price * rate)
print(final_price) # Output: 21.989
# Round to two decimal places
print(round(final_price, 2)) # Output: 21.99 Despite their limited precision, floats remain indispensable for mathematical operations. Whenever I needed to handle financial or scientific calculations, float was my first choice.
See more details in the documentation for the float type.
Copy the code below, which I created for you, and see the output in your IDE.
# Basic data types
a_string = "text"
a_boolean = True
an_integer = 10
a_float = 5.28
print(type(a_string))
print(type(a_boolean))
print(type(an_integer))
print(type(a_float))
# Long integer
print("*" * 10)
an_integer = 1_000_000_000
print(type(an_integer))
# Float precision
print("*" * 10)
a_float = 0.8742742394729342
print(type(a_float))
# Negative values
print("*" * 10)
an_integer = -100
a_float = -.58
print(an_integer)
print(a_float)
# Type casting
print("*" * 10)
print(str(an_integer))
print(float(an_integer))
print(int(a_float))
In this code, we created variables of the types covered in this article, used the type function to check each variable’s type (something I use a lot), looked at negative numbers, and finally covered type conversion.
The primitive types string, boolean, integer, and float are the foundation of data types in Python. They offer rich, easy-to-use features that let you solve everything from basic problems to more complex challenges.
If you are just getting started, I recommend exploring each of these types and experimenting with them in your code. Test, experiment, and see how these concepts can transform the way you program. The official Python documentation is also an excellent source to deepen your knowledge.
Join me in lesson 4, where we will see how to manipulate strings and use the input function.
Web development has evolved significantly in recent years, and Python frameworks are at the forefront…
Artificial intelligence is revolutionizing every sector of society, from enterprise applications to everyday solutions. At…
In this lesson of the Python mini course, I want to cover two collection types…
Among Python's most versatile data structures, dictionaries stand out. They help us represent structured data…
In this ninth lesson of the mini course, I want to talk about one of…
Understanding variable scope in Python is essential to avoid errors and write more predictable programs.…
Este blog utiliza cookies. Se você continuar assumiremos que você está satisfeito com ele.
Leia Mais...