Understanding Data Types in Python
Variables in Python have a data type that determine what kind of value it holds and what you can do with it. If you’re working with data, understanding data types is important.
Last week, we introduced the dataset we’ll be using throughout the series. We’ll return to it soon, but first, we need to understand some additional aspects of Python, especially how it handles different types of data.
Common Data Types in Python
Python has several built-in data types. Below are the most commonly used ones.
1. Strings (str
) – Used for text values
name = "Alice" # A string (text value representing a name)
print(name) # Output the string value
2. Integers (int
) – Used for whole numbers
age = 25 # An integer (a whole number representing age)
print(age) # Output the integer value
3. Floats (float
) – Used for decimal numbers
height = 5.7 # A floating-point number (a decimal representing height)
print(height) # Output the float value
4. Booleans (bool
) – Used for True/False values
is_student = True # A boolean (True/False value indicating student status)
print(is_student) # Output the boolean value
5. None Type (NoneType
) – Represents a missing or undefined value
grade = None # This means "no value"
print(grade) # Output None
A quick note on NonType
None is a special value in Python that means “nothing” or “no value here.” When you assign a variable to None, Python gives it the data type NoneType.
It’s more of a descriptive way to refer to a variable that doesn’t hold a value of a built-in type like int
, float
, str
, list
, dict
, bool
, etc.
It’s useful when:
- You want to say “this doesn’t have a value yet.”
- A function doesn’t return anything.
You might hear someone say a variable is “non-type,” but what they really mean is that the variable’s value is None.
How to Check a Variable’s Data Type
Use the type()
function to find out what kind of value you’re working with.
Its pretty much all the same thing, but i’m showing it here for emphasis. because you always want to check what type of value you are working with. You will realize the importance of this when you are working with a dataset where values might (intentionally or unintentionally) have unexpected types.
print(type(name)) # Check the type of a string variable
print(type(age)) # Check the type of an integer variable
print(type(height)) # Check the type of a float variable
print(type(is_student)) # Check the type of a boolean variable
print(type(grade)) # Check the type of a NoneType variable
Converting Between Data Types
Sometimes, you need to change or convert a variable’s data type. This is called type conversion.
1. Converting a String to an Integer
num_str = "100" # A string containing numeric characters
num_int = int(num_str) # Convert to an integer for numerical operations
print(num_int, type(num_int)) # Output converted integer and its type
2. Converting an Integer to a Float
num = 42 # A whole number (integer)
num_float = float(num) # Convert to a floating-point number
print(num_float, type(num_float)) # Output converted float and its type
3. Converting a Number to a String
age = 30 # An integer representing age
age_str = str(age) # Convert to a string for text display
print(age_str, type(age_str)) # Output converted string and its type
4. Converting Values to Boolean
print(bool(0)) # False
print(bool(1)) # True, any non-zero number is 'True'
print(bool("")) # False, an empty string is 'False'
print(bool("Hello")) # True, non-empty strings are 'True'
If you notice, I am not overwriting variables. Instead, i am creating a new variable each time. That helps to avoid confusion when you need the same values of different types for varying reasons.
Quick Practice
Try these out to test what you’ve learned.
1️. Check the type of a variable
value = "42" # A string containing numeric characters
print(value, type(value)) # Output value and its type
2️. Convert a string to a float and print it
num = "10.5" # A string representation of a decimal number
num = float(num) # Convert the string into a float
print(num, type(num)) # Output converted float and its type
3️. Try converting a boolean to an integer
boolean_value = True # A boolean variable
int_value = int(boolean_value) # Convert to integer
print(int_value, type(int_value)) # Output converted integer and its type
Summary
- Python has several built-in data types: strings, integers, floats, booleans, and NoneType.
- Use
type()
to check a variable’s data type/the kind of value you’re working with. - Convert data types using
int()
,float()
,str()
, andbool()
.
Next, we will introduce data structures in Python, starting with lists and dictionaries.
Recommended Python Books
- Fluent Python: Clear, Concise, and Effective Programming
- Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python
- Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter
- Learning Python: Powerful Object-Oriented Programming
- Python Crash Course
- Python Programming for Beginners
- Pandas Cookbook