DEV Community

Cover image for Understanding Python Data Types: A Beginner’s Guide
Mary Nyandia
Mary Nyandia

Posted on

Understanding Python Data Types: A Beginner’s Guide

When learning Python, one of the first things you’ll encounter is data types. Data types define the kind of values your program can work with whether it’s numbers, text, or logical values. In this article, I’ll walk you through the basics using a simple program that demonstrates Python’s core data types.

1.Integers (Whole Numbers)

Integers are numbers without decimals. They can be positive, negative, or zero.

age = 25
score = 100
temperature = -5

print(f"Age: {age}")
print(f"Score: {score}")
print(f"Temperature: {temperature}")
print(f"Type of age: {type(age)}")

Enter fullscreen mode Exit fullscreen mode

2.Floats (Decimal Numbers)

Floats are numbers with decimals. They’re useful when precision matters.

height = 1.75
price = 9.99
pi = 3.14

print(f"Height: {height}")
print(f"Price: ${price}")
print(f"Pi: {pi}")
print(f"Type of height: {type(height)}")

Enter fullscreen mode Exit fullscreen mode

3.Strings (Text)

Strings represent text data. They’re enclosed in quotes (" " or ' ').

name = "Alice"
city = "New York"
message = "Hello, World!"

print(f"Name: {name}")
print(f"City: {city}")
print(f"Message: {message}")
print(f"Type of name: {type(name)}")

Enter fullscreen mode Exit fullscreen mode

4.Booleans (True/False)

Booleans represent logical values: either True or False.

is_student = True
is_raining = False

print(f"Is student? {is_student}")
print(f"Is raining? {is_raining}")
print(f"Type of is_student: {type(is_student)}")

Enter fullscreen mode Exit fullscreen mode

5.Finding Data Types

Python makes it easy to check the type of any value using the type() function.

num_int = 42
print(f"Value: {num_int}")
print(f"Data Type: {type(num_int)}")

is_true = True
print(f"Value: {is_true}")
print(f"Data Type: {type(is_true)}")

text = "Python"
print(f"Value: {text}")
print(f"Data Type: {type(text)}")

decimal = 3.14
print(f"Value: {decimal}")
print(f"Data Type: {type(decimal)}")

Enter fullscreen mode Exit fullscreen mode

My Take

Understanding data types is the foundation of programming in Python. Once you know how to work with integers, floats, strings, and booleans, you’ll be ready to explore more advanced concepts like lists, dictionaries, and classes that we are going to learn later as we proceed.

Top comments (0)