Python Data Types: A Beginner’s Guide to Type Casting
Have you ever wondered how software, like your banking app, manages to store your name, age, and account balance all in one place? How does it know that your age is a number it can calculate with, while your name is just text? The secret lies in data types—a fundamental concept in every programming language, including Python.

Understanding data types is like knowing the difference between storing water in a bottle and storing books on a shelf; you need the right container for the right kind of stuff. In this guide, you’ll learn about the essential built-in data types in Python and, crucially, how to convert between them—a process known as type casting. By the end, you’ll be confidently storing and manipulating different kinds of data in your programs.
What Are Python Data Types?
In the real world, we categorize things instinctively. We know that “28” is a number and “Yogesh” is a name. Python does the same thing with data. It assigns a type to every piece of data it handles. This is important because it tells Python how much memory to allocate and what operations can be performed on that data.
Think of it this way: you can mathematically add your age (28) to your friend’s age (25), but it makes no sense to “add” your name to your friend’s name in the same way. Data types help Python enforce these rules.
Let’s explore the core data types you’ll use every day.
The Core Data Types in Python
We can open a Python shell (like the one you get by typing python
in your Command Prompt or Terminal) and start experimenting. We’ll use the type()
function, which tells us the data type of any value we give it.
Integers and Floats (Numeric Types)
Numbers without a decimal point are called integers (int
).
# Checking the type of a whole number
type(28)
Output: <class 'int'>
This confirms that 28
is an integer. Even negative numbers are integers.
type(-5)
Output: <class 'int'>
Numbers with a decimal point are called floating-point numbers (float
).
# Checking the type of a decimal number
type(28.9)
Output: <class 'float'>
Strings (Text Type)
Text in Python is stored as a string (str
). You must enclose it in single or double quotes.
# Checking the type of a name
type("Yogesh")
Output: <class 'str'>
Without quotes, Python would think “Yogesh” is the name of a variable, which would cause an error.
Booleans (True/False Type)
A boolean (bool
) can only be one of two values: True
or False
. This is incredibly useful for making decisions in your code (e.g., is_user_logged_in = True
).
# Checking the type of True and False
type(True)
Output: <class 'bool'>
type(False)
Output: <class 'bool'>
Crucial Note: If you put quotes around True
or False
, they become strings, not booleans!
The None Type
Sometimes, you need to represent the absence of a value. In Python, this is done with None
, which has its own data type, NoneType
.
Representing the absence of a value
type(None)
Output: <class 'NoneType'>
How to Convert Between Data Types (Type Casting)
What if you have an integer but your program needs a string? Or you have a string of a number that you need to do math with? This is where type casting comes in. You can convert values from one type to another using built-in functions like int()
, str()
, float()
, and bool()
.
Converting to Integer and Float
You can convert a float to an integer using int()
. Be careful: this simply chops off the decimal part; it does not round the number.
# Converting a float to an integer
int(90.8)
Output: 90
You can convert an integer to a float using float()
. This adds a decimal point.
# Converting an integer to a float
float(28)
Output: 28.0
Converting to and from Booleans
The conversion rules for booleans are logical:
- Any number that is 0 (or
0.0
) converts toFalse
. - Any other number converts to
True
. - The boolean
True
converts to the integer1
. - The boolean
False
converts to the integer0
.
# Converting booleans to integers
int(True) # Output: 1
int(False) # Output: 0
# Converting numbers to booleans
bool(1) # Output: True
bool(0) # Output: False
bool(-5) # Output: True (any non-zero number is True)
Converting to and from Strings
This is one of the most common operations. You can convert numbers and booleans to strings easily.
# Converting an integer to a string
str(100)
Output: '100'
(Notice the quotes, indicating it’s now a string)
# Converting a boolean to a string
str(True)
Output: 'True'
Converting from a string is trickier. You can only convert a string to an integer or float if the string’s content “looks like” a number.
# This works because the string "255" is a number
int("255")
Output: 255
# This will cause an ERROR because "Hello" can't be a number
int("Hello")
Output: ValueError: invalid literal for int() with base 10: 'Hello'
Ready to Go from Basics to Pro?
You’ve just taken your first steps into the world of Python data handling—a critical skill for any developer. While experimenting in the shell is a great start, building real-world applications requires a deeper, structured understanding of Python’s power. Imagine creating your own data-driven projects with the guidance of experts and a clear learning path.
If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.
Conclusion
In this guide, you’ve learned the pillars of Python data: integers, floats, strings, booleans, and None. More importantly, you’ve learned how to convert between them using type casting with functions like int()
, str()
, and bool()
. This is the foundation upon which all data manipulation in Python is built. Remember, the key to internalizing these concepts is practice. Open your Python IDE, repeat these examples, and try creating your own. Every expert was once a beginner who kept trying.
Common Questions About Python Data Types
What is the difference between int
and float
?
An int
(integer) is a whole number without a decimal point (e.g., 10
, -5
). A float
(floating-point number) has a decimal point (e.g., 10.0
, -5.2
, 3.14159
). They are stored differently in the computer’s memory.
Can I convert any string to an integer?
No. You can only convert a string to an integer if the entire string represents a valid whole number. For example, int("100")
works, but int("100.5")
or int("Hundred")
will result in an error.
When should I use the None
type?
Use None
to represent the absence of a value. It’s often used as a default placeholder for a variable that you may assign a real value to later in your program. It’s Python’s way of saying “nothing here.”