Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

Python Data Types Explained: int, float, complex, str, bool and None

When you start learning Python, you will work with different kinds of values. Sometimes you will store a person's name, sometimes an age, and sometimes a percentage or a logical value such as True or False.

But how does Python distinguish between these values?

The answer is data types.

Data types tell us what kind of value we are working with. Understanding them is important because different types of data support different operations.

For example, adding two numbers is different from joining two strings.

In this article, we will explore the six fundamental types listed in our syllabus: int, float, complex, str, bool, and None. We will also learn how to identify data types using Python's built-in type() function.

1. What Is a Data Type in Python?

A data type describes the kind of value represented by an object in a Python program.

Consider the following example:

age = 20
price = 99.50
name = "Rahul"
is_student = True

Here, the variables contain different types of values:

  • age contains an integer.

  • price contains a floating-point number.

  • name contains a string.

  • is_student contains a Boolean value.

Python automatically determines the type of each value when you assign it to a variable.

This means you generally do not need to declare a variable's type before using it.

2. Why Are Data Types Important?

Data types help Python determine which operations are appropriate for a value.

For example:

a = 10
b = 20

print(a + b)

Output:

30

Both variables contain integers, so Python adds their numeric values.

Now consider this example:

first_name = "Rahul"
last_name = "Sharma"

print(first_name + last_name)

Output:

RahulSharma

Here, the + operator joins the two strings instead of performing numerical addition.

If you want a space between the names, you can write:

first_name = "Rahul"
last_name = "Sharma"

print(first_name + " " + last_name)

Output:

Rahul Sharma

These examples demonstrate why understanding data types is essential.

3. Integer Data Type (int)

An integer is a whole number without a fractional or decimal part.

Python uses the int data type for integers.

Examples include:

age = 20
marks = 85
temperature = -5
number = 0

All these variables contain integer values.

Example: Student Age

age = 19

print(age)
print(type(age))

Output:

19
<class 'int'>

The type() function tells us that the value belongs to the int type.

Example: Adding Two Integers

a = 15
b = 25

total = a + b

print("Total =", total)

Output:

Total = 40

Important Points About Integers

  • Integers can be positive, negative, or zero.

  • They do not contain a decimal point.

  • Python integers can represent very large whole numbers, subject to available memory.

4. Floating-Point Data Type (float)

A floating-point number represents a number that can have a fractional part.

Python uses the float data type for these values.

Examples:

price = 99.50
percentage = 87.5
temperature = -2.5
pi = 3.14159

Example: Student Percentage

percentage = 85.75

print(percentage)
print(type(percentage))

Output:

85.75
<class 'float'>

Example: Calculating an Average

Suppose a student scores 80 and 90 in two subjects.

marks1 = 80
marks2 = 90

average = (marks1 + marks2) / 2

print("Average =", average)

Output:

Average = 85.0

Notice that the result is 85.0, not 85.

In Python, the / operator performs true division and returns a floating-point result for ordinary finite numeric inputs, even when the division is exact.

Integer vs Float

Consider:

a = 10
b = 10.0

print(type(a))
print(type(b))

Output:

<class 'int'>
<class 'float'>

Although both values represent the number ten, their data types are different.

Remember: 10 is an integer literal, while 10.0 is a floating-point literal.

5. Complex Data Type (complex)

Python supports complex numbers through the complex data type.

A complex number has a real part and an imaginary part.

For example:

z = 3 + 4j

Here:

  • 3 is the real part.

  • 4 is the imaginary part.

  • j represents the imaginary unit in Python.

Complex numbers are used in mathematics, engineering, signal processing, and scientific computing.

Example: Creating a Complex Number

number = 3 + 4j

print(number)
print(type(number))

Output:

(3+4j)
<class 'complex'>

Example: Accessing Real and Imaginary Parts

number = 3 + 4j

print("Real part:", number.real)
print("Imaginary part:", number.imag)

Output:

Real part: 3.0
Imaginary part: 4.0

Python provides the .real and .imag attributes to access these parts.

Another Example

a = 2 + 3j
b = 4 + 5j

print(a + b)

Output:

(6+8j)

Python supports arithmetic operations on complex numbers.

6. String Data Type (str)

A string is a sequence of characters used to represent text.

Python uses the str data type for strings.

Examples:

name = "Rahul"
city = "Kolkata"
message = "Welcome to Python"

Strings can contain letters, numbers, spaces, and special characters.

For example:

student_id = "12345"

Although the value contains digits, it is a string because it is enclosed in quotation marks.

Creating Strings

Python allows you to create strings using single or double quotation marks.

name = 'Rahul'
city = "Kolkata"

Both are valid.

You can also use triple quotation marks for strings that span multiple lines.

message = """Welcome to Python.
This is a beginner-friendly course."""

print(message)

Output:

Welcome to Python.
This is a beginner-friendly course.

Example: Joining Strings

first_name = "Rahul"
last_name = "Sharma"

full_name = first_name + " " + last_name

print(full_name)

Output:

Rahul Sharma

Example: Finding the Length of a String

Python provides the built-in len() function to find the number of characters in a string.

message = "Python"

print(len(message))

Output:

6

Important Point

Compare these two statements:

a = 100
b = "100"

print(type(a))
print(type(b))

Output:

<class 'int'>
<class 'str'>

The first value is an integer, while the second is a string.

Quotation marks make a significant difference.

7. Boolean Data Type (bool)

The Boolean data type represents one of two logical values:

True
False

Python uses the bool data type for these values.

Boolean values are commonly used in conditions, comparisons, and decision-making statements.

Example: Boolean Variable

is_student = True

print(is_student)
print(type(is_student))

Output:

True
<class 'bool'>

Example: Comparing Two Numbers

a = 10
b = 20

result = a < b

print(result)

Output:

True

The expression a < b is true because ten is less than twenty.

Now try:

a = 10
b = 20

result = a > b

print(result)

Output:

False

The expression is false because ten is not greater than twenty.

Common Comparison Operators

OperatorMeaningExampleResult
==Equal to10 == 10True
!=Not equal to10 != 20True
>Greater than20 > 10True
<Less than10 < 20True
>=Greater than or equal to10 >= 10True
<=Less than or equal to10 <= 5False

These operators are useful when you begin learning conditional statements.

Important: In Python, True and False must be written with their first letters capitalized.

8. The Special Value None

Python provides a special value called None.

It represents the absence of a value.

For example:

result = None

print(result)
print(type(result))

Output:

None
<class 'NoneType'>

The type of None is called NoneType.

Why Is None Useful?

Sometimes a program has not yet calculated a result or does not currently have a meaningful value to store.

In such situations, None can be useful.

Example:

student_name = None

print(student_name)

Output:

None

Later, the variable can be assigned a meaningful value:

student_name = None

student_name = "Rahul"

print(student_name)

Output:

Rahul

None Is Different from Zero

Consider:

a = None
b = 0

print(a)
print(b)

Output:

None
0

These values are not the same.

  • None represents the absence of a value.

  • 0 is an integer representing the number zero.

An empty string, "", is also different from None.

9. How to Check the Data Type Using type()

Python provides a built-in function called type().

It allows you to determine the type of a value or object.

Example

a = 10
b = 10.5
c = 3 + 4j
d = "Python"
e = True
f = None

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

Output:

<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

This example demonstrates the six types covered in this article.

Why Should You Learn type()?

The type() function is especially helpful when you are learning Python because it allows you to check what kind of value a variable contains.

It can also help you identify mistakes when working with numbers, strings, and user input.

10. Python Automatically Determines Data Types

Python is a dynamically typed language.

This means that you do not generally need to declare the data type before assigning a value to a variable.

For example:

value = 10

print(value)
print(type(value))

Output:

10
<class 'int'>

You can then assign a string to the same variable name:

value = "Python"

print(value)
print(type(value))

Output:

Python
<class 'str'>

The variable name value now refers to a string object rather than the integer object it referred to previously.

This flexibility is one of the features that makes Python convenient for beginners.

However, changing the type of a value does not mean that every operation will work with every type. You must still understand the data you are using.

11. Difference Between the Six Data Types

Let's compare the six types covered in our syllabus.

Data TypePurposeExampleOutput of type()
intWhole numbers25<class 'int'>
floatFloating-point numbers25.5<class 'float'>
complexComplex numbers3 + 4j<class 'complex'>
strText"Python"<class 'str'>
boolLogical valuesTrue<class 'bool'>
NoneTypeType of NoneNone<class 'NoneType'>

Understanding these differences will make it easier to write calculations, process text, take user input, and create conditions.

12. Common Beginner Mistakes

Mistake 1: Confusing an integer with a string

Incorrect assumption:

age = "20"

A beginner might assume that age contains an integer.

However, "20" is a string because it is enclosed in quotation marks.

Check it:

age = "20"

print(type(age))

Output:

<class 'str'>

If you need an integer, write:

age = 20

Or convert the string when appropriate:

age = int("20")

print(type(age))

Output:

<class 'int'>

Mistake 2: Using lowercase true instead of True

Incorrect:

is_student = true

Python does not recognize lowercase true as its Boolean literal. Unless you have defined a name called true, this will raise a NameError.

Correct:

is_student = True

Mistake 3: Expecting None to behave like zero

Consider:

result = None

print(result + 10)

This produces a TypeError because Python does not support adding None to an integer.

If you intend to perform a calculation, assign an appropriate numeric value first.

Mistake 4: Forgetting that / returns a floating-point result

Example:

a = 10
b = 2

print(a / b)

Output:

5.0

The result is a float.

If you need floor division, Python provides the // operator:

print(10 // 2)

Output:

5

13. Practice Programs

Try these small programs to test your understanding.

Program 1: Identify the Data Types

a = 50
b = 25.75
c = 2 + 3j
d = "Python"
e = False
f = None

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

Task: Predict the output before running the program.

Program 2: Student Information

student_name = "Rahul"
age = 20
percentage = 85.5
is_passed = True

print("Name:", student_name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_passed)

Task: Identify the data type of each variable.

Program 3: Add Two Numbers

a = 15
b = 25

total = a + b

print("Total:", total)
print("Data type:", type(total))

Task: Change the values of a and b and observe the output.

Program 4: Compare Two Numbers

a = 50
b = 30

result = a > b

print("Result:", result)
print("Data type:", type(result))

Task: Change the values so that the result becomes False.

Program 5: Compare an Integer and a String

a = 100
b = "100"

print(type(a))
print(type(b))

Task: Explain why the two data types are different even though their contents look similar.

14. Frequently Asked Questions

Q1. What are data types in Python?

Data types describe the kind of value represented by an object, such as a number, text, Boolean value, or None.

Q2. What is the difference between int and float?

int represents integers such as 10 and -5. float represents floating-point values such as 10.5 and 3.14.

Q3. What is the data type of "123"?

It is str because the value is enclosed in quotation marks.

Q4. What is the data type of True?

Its data type is bool.

Q5. What is the data type of None?

Its data type is NoneType.

Q6. How can I check the data type of a variable?

Use the built-in type() function.

Example:

number = 25

print(type(number))

Output:

<class 'int'>

Q7. Does Python require us to declare variable types?

No. Python generally determines the type automatically from the value assigned to a variable.

Conclusion

Data types are one of the most important foundations of Python programming.

In this article, we explored integers, floating-point numbers, complex numbers, strings, Boolean values, and None. We also learned how to check data types using type() and saw how Python determines types automatically.

As you continue learning Python, you will use these concepts in almost every program, from simple calculators to database applications and AI projects.

The best way to remember data types is to practise creating variables with different values and checking their types yourself.

Keep practising, and build your Python knowledge one concept at a time!

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...