Data Types in Python: Understanding Primitive Types – Lesson 3

tipos de dados em python

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.

What Are Primitive Types 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:

  • String: for working with text.
  • Boolean: for representing logical values (true or false).
  • Integer: ideal for whole numbers.
  • Float: perfect for floating-point numbers.

Advantages of Primitive Types

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.

Data Types in Python: Working with Strings

The string type (str) is essential for handling text. In Python, strings can be declared using single (‘) or double (“) quotes.

String Examples

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.

Data Types in Python: Booleans, True or False?

The boolean type (bool) is essential for handling conditions and logic. It represents only two values: True or False.

Boolean Examples

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.

Data Types in Python: Working with Integers

The integer type (int) is used to work with whole numbers in Python. It is very versatile, whether for simple or complex calculations.

Integer Examples

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.

Data Types in Python: Representing Decimal Numbers

The float type is used to handle floating-point (decimal) numbers. It is essential for more precise calculations.

Float Examples

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.

Let’s get to work

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.

Conclusion

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.

Você também pode gostar

PT | EN