0% found this document useful (0 votes)
4 views5 pages

Python Data Types Complete Reference

The document provides a comprehensive reference on Python data types, including strings, integers, floats, booleans, lists, tuples, sets, and dictionaries. Each data type is defined with key characteristics, examples, and exercises for practice. It emphasizes the immutability and mutability of different types, along with their unique properties and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Python Data Types Complete Reference

The document provides a comprehensive reference on Python data types, including strings, integers, floats, booleans, lists, tuples, sets, and dictionaries. Each data type is defined with key characteristics, examples, and exercises for practice. It emphasizes the immutability and mutability of different types, along with their unique properties and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Data Types - Complete Reference

String (str)

Definition: Sequence of characters enclosed in single (' '), double (" "), or triple
quotes (''' ''' or """ """).

Key Characteristics:
- Immutable (cannot be changed after creation).
- Indexed and ordered.
- Supports slicing and concatenation.

Examples:
text = "Python"
print(text[0]) # P
print(text[-1]) # n
print(text[0:3]) # Pyt
print(len(text)) # 6
print(text.upper()) # PYTHON

Exercises:
1. Create a string and print its first and last characters.
2. Check if the word "Python" exists in a given sentence.
3. Reverse a string using slicing.

Integer (int)

Definition: Whole numbers without a decimal point.

Key Characteristics:
- Immutable.
- Can be positive, negative, or zero.
- Supports all arithmetic operations.

Examples:
a = 10
b = 3
print(a + b) # 13
print(a // b) # 3
print(a % b) # 1
print(abs(-5)) # 5

Page 1
Python Data Types - Complete Reference

Exercises:
1. Write a program to swap two integers without using a temporary variable.
2. Calculate the factorial of a number using integers.
3. Check if a given integer is even or odd.

Float (float)

Definition: Numbers with a decimal point.

Key Characteristics:
- Immutable.
- Supports arithmetic operations.
- Subject to floating-point precision limitations.

Examples:
x = 5.75
y = 2.0
print(x / y) # 2.875
print(round(x)) # 6
print(round(3.14159, 2)) # 3.14

Exercises:
1. Convert an integer to a float and print both values.
2. Write a program to calculate the Body Mass Index (BMI) using floats.
3. Round a float to 3 decimal places.

Boolean (bool)

Definition: Represents truth values True or False.

Key Characteristics:
- Subclass of integers (True = 1, False = 0).
- Immutable.
- Used for conditional logic.

Examples:
print(True + True) # 2
print(False * 5) # 0

Page 2
Python Data Types - Complete Reference

print(5 > 3) # True


print(bool(0)) # False

Exercises:
1. Check if a number is within a specific range using Boolean logic.
2. Convert various data types to Boolean using bool().
3. Use 'and', 'or', 'not' to combine conditions.

List (list)

Definition: Ordered, mutable collection of items.

Key Characteristics:
- Can store mixed data types.
- Supports indexing and slicing.
- Mutable (can add, remove, and modify elements).

Examples:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.remove("banana")
print(fruits[0]) # apple

Exercises:
1. Create a list of numbers and print only the even ones.
2. Reverse a list without using reverse() method.
3. Merge two lists without using '+' operator.

Tuple (tuple)

Definition: Ordered, immutable collection of items.

Key Characteristics:
- Can store mixed data types.
- Supports indexing and slicing.
- Faster than lists due to immutability.

Examples:
colors = ("red", "green", "blue")

Page 3
Python Data Types - Complete Reference

print(colors[1]) # green
print(len(colors)) # 3

Exercises:
1. Create a tuple with single element and check its type.
2. Unpack a tuple into separate variables.
3. Concatenate two tuples.

Set (set)

Definition: Unordered, mutable collection of unique elements.

Key Characteristics:
- No duplicate items allowed.
- Elements must be immutable.
- Supports mathematical set operations.

Examples:
nums = {1, 2, 3}
nums.add(4)
nums.update([3, 5, 6])
nums.remove(2)
print(nums)

Exercises:
1. Create two sets and find their intersection.
2. Remove duplicates from a list using a set.
3. Check if one set is a subset of another.

Dictionary (dict)

Definition: Unordered, mutable collection of key-value pairs.

Key Characteristics:
- Keys are unique and immutable.
- Values can be of any type.
- Fast lookups using keys.

Examples:

Page 4
Python Data Types - Complete Reference

person = {"name": "Alice", "age": 25}


person["city"] = "London"
print(person.get("age"))
print(person.keys())

Exercises:
1. Create a dictionary to store student names and grades, then print each student with
their grade.
2. Merge two dictionaries into one.
3. Count the frequency of each word in a given sentence.

Page 5

You might also like