Python Data Types Explained with Examples – A Beginner’s Guide

🐍 1. Introduction

💻 Every Python program works with data. This data may be a number, a piece of text, a collection of values, or even a simple True or False value.

🔎 Python uses data types to tell us what kind of value we are working with.

💡 In simple words: A data type describes the kind of data stored in a variable.

🌍 A Simple Real-World Example

Think about the information we use in everyday life. A person’s age is a whole number, height may contain a decimal value, and a person’s name is text.

📋 Example Data 🧩 Python Data Type
🎂 Age → 20 int
📏 Height → 5.8 float
👤 Name → “Rahul” str
✅ Passed → True bool
📚 Subjects → [“Python”, “C”, “Java”] list

🐍 How Does This Look in Python?

✏️ We can store these values in variables:

age = 20
height = 5.8
name = “Rahul”
passed = True
👀 Notice: The values are different kinds of data. Python can determine the appropriate data type from the value assigned to the variable.

⭐ Why Should We Learn Data Types?

Data types are important because different types of data behave differently in a Python program.

➕ For example, adding two numbers performs arithmetic, while joining two strings combines text.

🔢 Numbers: 10 + 20 → 30    |    🔤 Text: “10” + “20” → “1020”

🚀 In the following sections, we will explore the most commonly used Python data types with simple explanations, examples, and programs.

🧩 2. What is a Data Type?

Before learning the different Python data types, let’s understand what a data type actually means.

A data type tells Python what kind of value is stored in a variable. For example, 20 is a whole number, 5.8 is a decimal number, and “Rahul” is text.

💡 Simple Definition: A data type identifies the kind of data a value represents.

🐍 Let’s Look at an Example

Consider the following Python variables:

age = 20
name = “Rahul”
height = 5.8
passed = True

🔍 Each variable contains a different kind of value:

  • 20 → an integer (int)
  • “Rahul” → a string (str)
  • 5.8 → a floating-point number (float)
  • True → a Boolean value (bool)
👀 Notice: The values are not all of the same kind. Python keeps track of the type of each value.

🔎 How Can We Find the Data Type?

Python provides a built-in function called type(). It tells us the data type of a value or variable.

print ( type ( age ))

📤 Output:

<class ‘int’>

🎯 The output <class ‘int’> tells us that the variable age contains an integer value.

Key Takeaway: Use type() whenever you want to check what type of data a variable contains.

🚀 Now that we understand what a data type is, let’s explore the different built-in data types available in Python.

🧩 3. Main Built-in Data Types in Python

🐍 Python provides several built-in data types for storing and working with different kinds of information.

Before learning each type in detail, let’s get a quick overview of the most commonly used ones.

💡 Quick idea: Different data types are designed for different kinds of data— numbers, text, collections, logical values, and more.
📂 Category 🧩 Data Type ✏️ Example 🎯 Mainly Used For
🔢 Numeric int 25 Whole numbers
🔢 Numeric float 25.5 Decimal numbers
🔢 Numeric complex 2 + 3j Complex numbers
✅ Boolean bool True True / False
🔤 Text str "Python" Text and characters
📚 Sequence list [10, 20, 30] Ordered collection
📚 Sequence tuple (10, 20, 30) Ordered, fixed collection
🎯 Set set {10, 20, 30} Unique values
🗂️ Mapping dict {"name": "John"} Key-value data
⚪ Special NoneType None Represents no value
👀 Don’t try to memorize everything at once! We will study each important data type step by step with simple Python programs and examples.

🗺️ What We Will Learn

We will begin with simple data types such as int, float, bool, and str, and then move on to collections such as list, tuple, set, and dict.

Quick Reference: Numbersint, float, complex   •   Textstr   •   Collectionslist, tuple, set, dict

🚀 Let’s start with the simplest numeric data type: integers (int).

🔎 4. The type() Function

When learning Python, you may sometimes wonder: “What type of data is stored in this variable?”

Python provides a simple built-in function called type() to answer this question.

💡 Remember: The type() function tells you the data type of a value or variable.

🐍 Example

Let’s create variables containing different types of data:

age = 20
height = 5.8
name = “Python”
passed = True

print ( type ( age ))
print ( type ( height ))
print ( type ( name ))
print ( type ( passed ))

📤 Output:

<class ‘int’>
<class ‘float’>
<class ‘str’>
<class ‘bool’>

🧠 Understanding the Output

Each line of the output tells us the data type of the corresponding variable:

🔢 age → int    |    📏 height → float
🔤 name → str    |    ✅ passed → bool
Useful Tip: If you are ever unsure about the type of a value, use type(). It is one of the simplest and most useful functions for beginners.

✏️ Basic Syntax

type ( value )
🎯 Key Takeaway: type() helps you identify the type of data stored in a Python value or variable.

🔄 5. Python Automatically Determines the Data Type

🐍 One of the useful features of Python is that you do not have to specify the data type when creating a variable.

Python looks at the value assigned to the variable and automatically determines its data type.

💡 For example: When you write x = 10, Python recognizes x as an integer variable.

💻 See It in Action

Look at what happens when we assign different values to the same variable:

x = 10
print ( type ( x ))

x = “Python”
print ( type ( x ))

x = 10.5
print ( type ( x ))

📤 Output:

<class ‘int’>
<class ‘str’>
<class ‘float’>

🧠 What Happened Here?

The variable x was used three times, but each time it referred to a different type of value.

🔢 x = 10 → x refers to an int
🔤 x = “Python” → x refers to a str
🔢 x = 10.5 → x refers to a float
👀 Important: Python variables do not need a fixed data type declaration. The type is determined from the value assigned to the variable.

🚀 This Feature is Called Dynamic Typing

Python is known as a dynamically typed language. This means a variable can refer to values of different data types during the execution of a program.

Beginner Tip: You don’t need to write something like int x = 10 in Python. Simply write x = 10.

🎯 Key idea: Python determines the type from the value, and type() lets you check that type.

🔄 6. Type Conversion

🔁 Sometimes we need to change a value from one data type to another. This process is called type conversion.

Python provides built-in functions such as int(), float(), and str() to perform common conversions.

💡 Simple idea: Type conversion means changing a value from one data type to another.

🔢 1. Convert to Integer — int()

The int() function can convert a suitable value into an integer.

age = “20”

age = int ( age )

print ( age )
print ( type ( age ))

📤 Output:

20
<class ‘int’>

🔢 2. Convert to Float — float()

The float() function converts a suitable value into a floating-point number.

x = 10
y = float ( x )

print ( y )
print ( type ( y ))

📤 Output:

10.0
<class ‘float’>

🔤 3. Convert to String — str()

The str() function converts a value into a string.

number = 100
text = str ( number )

print ( text )
print ( type ( text ))

📤 Output:

100
<class ‘str’>
Quick Reference:
int() → converts to an integer
float() → converts to a floating-point number
str() → converts to a string

🎯 A Practical Example with input()

One of the most common situations where type conversion is needed is when taking input from the user.

age = int ( input ( “Enter your age: “ ))

print ( age )
print ( type ( age ))
👀 Why use int() here? The input() function normally gives the user’s input as a string. Using int() converts that input into an integer so it can be used in calculations.
🧠 Key Takeaway: Type conversion allows Python programs to work with values in the data type required for a particular operation.

📊 7. Quick Comparison of Python Data Types

🔍 We have now explored the main Python data types. The table below brings them together in one convenient reference.

💡 Quick Reference: Use this table whenever you need a quick reminder of a Python data type, its example, or its common purpose.
📂 Category 🧩 Data Type ✏️ Example 🎯 Commonly Used For
🔢 Numeric int 25 Whole numbers
🔢 Numeric float 25.5 Decimal numbers
🔢 Numeric complex 2 + 3j Complex numbers
✅ Boolean bool True True / False decisions
🔤 Text str "Hello" Text
📚 Sequence list [1, 2, 3] Collection of items
📚 Sequence tuple (1, 2, 3) Fixed collection
🎯 Set set {1, 2, 3} Unique items
🗂️ Mapping dict {"a": 1} Key-value data
⚪ Special NoneType None Represents no value
🧠 Memory Tip: Think of the types in simple groups: numbers for calculations, str for text, and collections for storing multiple values.
📌 Keep this table handy! It can serve as a quick reference while practicing Python programs.

🚀 Now let’s put what we have learned into practice with a small Python program.

📝 8. Small Practice Exercises

💻 Now it’s your turn! Try these small exercises to check how well you understand Python data types.

💡 Practice Tip: Try solving each exercise yourself before looking at any solution. Writing the code is the best way to remember the concept.

🎯 Exercise 1 — Create Variables

Create variables to store the following information:

  • 👤 Your name
  • 🎂 Your age
  • 📏 Your height
  • 🎓 Whether you are a student

🔎 Print each value and use type() to display its data type.

📚 Exercise 2 — Identify a List

Create the following list:

numbers = [ 10 , 20 , 30 , 40 ]

🔎 Print the list and use type() to verify its data type.

🗂️ Exercise 3 — Create a Dictionary

Create a dictionary containing these three pieces of information:

name
age
course

🔎 Print the dictionary and check its data type using type().

🔄 Exercise 4 — Input and Type Conversion

Ask the user to enter two numbers. Convert both inputs into integers and calculate their sum.

💡 Hint: Remember that input() returns text. You will need int() to convert the input before performing the addition.

⭐ Bonus: Print the result and its data type.

🚀 Mini Challenge: Can you create one program that uses int, float, str, bool, list, and dict and prints the type of each one?

🎉 Don’t worry if your first attempt doesn’t work perfectly. Read the error, make a small change, and try again!

✅ Solutions to the Practice Exercises

🔍 Finished the exercises? Let’s check your solutions. The following examples show one possible way to solve each problem.

💡 Remember: There can be more than one correct way to write a Python program. Your solution does not have to look exactly like these examples.

🎯 Exercise 1 — Solution

Create variables for your name, age, height, and student status, then display their values and data types.

name = “Rahul”
age = 20
height = 5.8
is_student = True

print ( name , type ( name ))
print ( age , type ( age ))
print ( height , type ( height ))
print ( is_student , type ( is_student ))
🧠 Result: name → str, age → int, height → float, is_student → bool

📚 Exercise 2 — Solution

Create a list and check its data type.

numbers = [ 10 , 20 , 30 , 40 ]

print ( numbers )
print ( type ( numbers ))
[10, 20, 30, 40]
<class ‘list’>

🗂️ Exercise 3 — Solution

Create a dictionary containing a name, age, and course.

student = {
    “name” : “Rahul” ,
    “age” : 20 ,
    “course” : “Python”
}

print ( student )
print ( type ( student ))
{‘name’: ‘Rahul’, ‘age’: 20, ‘course’: ‘Python’}
<class ‘dict’>

🔄 Exercise 4 — Solution

Ask the user for two numbers, convert them to integers, and calculate their sum.

num1 = int ( input ( “Enter first number: “ ))
num2 = int ( input ( “Enter second number: “ ))

total = num1 + num2

print ( “Sum =” , total )
print ( type ( total ))

📤 Sample Output:

Enter first number: 10
Enter second number: 20
Sum = 30
<class ‘int’>

💡 The actual result will depend on the numbers entered by the user.

🚀 Mini Challenge — One Possible Solution

Here’s one program that uses several different Python data types and displays the type of each value.

age = 20
height = 5.8
name = “Rahul”
passed = True
subjects = [ “Python” , “C” , “Java” ]
student = { “name” : “Rahul” }

print ( type ( age ))
print ( type ( height ))
print ( type ( name ))
print ( type ( passed ))
print ( type ( subjects ))
print ( type ( student ))
<class ‘int’>
<class ‘float’>
<class ‘str’>
<class ‘bool’>
<class ‘list’>
<class ‘dict’>
🎉 Well done! By completing these exercises, you have practiced variables, type(), lists, dictionaries, user input, and type conversion.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »