Checking your session…
Module 02: Variables, Data Types & Operators

Python Data Types

Level: 1 | Version: v1.0 | Author: Meptrasoft


What is a Data Type?

A data type tells Python what kind of value a name refers to and what operations can be performed on that value.

PYTHON
age = 21         # int (integer)
name = "Arun"    # str (string)
price = 99.5     # float (decimal number)

In Python, every value has a data type. Python detects the data type automatically when you assign a value to a name.


Data Types at a Glance

Python Data Types overview diagram


Why Do We Need Data Types?

Data types tell Python how to handle data. For example, the + operator behaves differently depending on data types:

PYTHON
# Adding two numbers
a = 10
b = 20
print(a + b)  # Output: 30
PYTHON
# Joining two text strings (concatenation)
a = "10"
b = "20"
print(a + b)  # Output: 1020

Overview of Python Data Types

Category Data Type Example Description
Numeric int 10, -5 Whole numbers
Numeric float 10.5, 99.9 Numbers with decimal points
Numeric complex 2 + 3j Complex numbers with real and imaginary parts
Text str "Python" Text enclosed in single or double quotes
Boolean bool True, False Logical truth values
Sequence list [1, 2, 3] Ordered, changeable list of values
Sequence tuple (1, 2, 3) Ordered, unchangeable tuple of values
Sequence range range(5) Generated sequence of numbers
Set set {1, 2, 3} Unordered collection of unique values
Mapping dict {"name": "Arun"} Key-value pairs
Special value NoneType None Represents the absence of a value

Detailed Data Types

1. Numeric Types

PYTHON
age = 21           # int
price = 45000.50   # float
c_num = 3 + 4j     # complex

2. Text Type (str)

Strings store text data enclosed in single ('...') or double ("...") quotes:

PYTHON
course = "Python Programming"

3. Boolean Type (bool)

Booleans store either True or False (always capitalized):

PYTHON
is_enrolled = True
is_passed = False

4. Collection Types Summary

Type Example Ordered? Changeable (Mutable)? Duplicates Allowed?
list ["Python", "Java", "SQL"] Yes Yes Yes
tuple ("Python", "Java") Yes No Yes
set {1, 2, 3} No Yes No
dict {"name": "Arun", "age": 21} Yes Yes Keys: No

Type Conversion (Type Casting)

Type conversion means changing a value from one data type to another. In Python, type conversion happens in two ways: Implicit Conversion and Explicit Conversion.

1. Implicit Conversion (Automatic)

Python can automatically convert compatible numeric types during calculations.

PYTHON
num_int = 10     # int
num_float = 2.5  # float

total = num_int + num_float

print(total)        # Output: 12.5
print(type(total))  # Output: <class 'float'>

Python treated 10 (int) as 10.0 (float) before adding, so the result became a float.

2. Explicit Conversion (Manual)

Explicit conversion is done by the programmer using built-in functions like int(), float(), and str().

Converting String to Integer / Float

PYTHON
age_text = "21"
age_num = int(age_text)

print(age_num + 1)  # Output: 22

Converting Number to String

PYTHON
marks = 95
marks_text = str(marks)

print("Your marks are: " + marks_text)

Warning: Converting non-numeric text to an integer causes a ValueError:

PYTHON
val = int("twenty")  # Raises ValueError: invalid literal for int()

Checking Data Types with type()

Use the built-in type() function to check the data type of a value:

PYTHON
x = 10
y = "Hello"

print(type(x))  # <class 'int'>
print(type(y))  # <class 'str'>

Placement Quick Points

  • Use type() to check the data type of a value.
  • int, float, and complex are numeric types.
  • str stores text inside quotes.
  • bool stores True or False.
  • Lists (list) are changeable; Tuples (tuple) are unchangeable.
  • Sets (set) store unique values and remove duplicates automatically.
  • Dictionaries (dict) store data as key: value pairs.
  • Implicit type conversion is done automatically by Python when compatible values are used together.
  • Explicit type conversion uses int(), float(), and str().
  • Converting invalid text like int("hello") raises a ValueError.

Interview Questions

What is a data type in Python?
A classification that tells Python what kind of value a name refers to and what operations can be performed on that value.
How do you check a value's data type?
By using the built-in type() function (e.g., type(value)).
What is the difference between implicit and explicit type conversion?
Implicit conversion is done automatically by Python when compatible types are used together. Explicit conversion is done manually by the programmer using functions like int(), float(), or str().
What is the difference between a list and a tuple?
A list is mutable (can be changed after creation), while a tuple is immutable (cannot be changed).
What happens if you try int("abc") in Python?
It raises a ValueError because "abc" is not a valid numeric string.
What output does "5" + "5" give in Python?
"55" because both operands are strings, so string concatenation takes place.

Practice

  1. Create variables for int, float, str, and bool, and print their data types using type().
  2. Convert the string "100" into an integer and add 50 to it.
  3. Convert the integer 450 to a string and combine it with "Total Marks: ".
  4. Create a list of 3 colors and change the first color to "Blue".
  5. Create a set {10, 20, 20, 30} and print it. Explain why 20 appears only once.
  6. What error occurs when running int("nineteen")? Fix it.

End of Python Data Types