Python Full Roadmap Notes (Expanded
Data Types Section)
4. Data Types and Type Casting (Expanded)
Python has several built-in data types that are used to store values of different types:
**1. Integers (int)**:
Integers are whole numbers without a decimal point. They can be positive or negative.
Used for counting, indexing, loops, etc.
Example:
x = 10
y = -5
print(x + y) # Outputs 5
**2. Floating Point Numbers (float)**:
Float represents real numbers with a decimal point. Useful in scientific calculations.
Example:
pi = 3.14
radius = 2.0
area = pi * (radius ** 2)
print(area)
**3. Strings (str)**:
Strings are sequences of characters, used to store text.
Strings are immutable and can be enclosed in single or double quotes.
Example:
name = 'Alice'
greeting = "Hello, " + name
print(greeting)
**4. Booleans (bool)**:
Booleans represent one of two values: True or False.
Used in conditional statements and logical operations.
Example:
is_valid = True
print(is_valid)
**5. Lists**:
Lists are ordered, mutable collections of items.
They can contain elements of different types.
Example:
numbers = [1, 2, 3, 4]
numbers.append(5)
print(numbers)
**6. Tuples**:
Tuples are ordered, immutable collections.
They are faster than lists and used for fixed collections.
Example:
coordinates = (10.0, 20.0)
print(coordinates)
**7. Sets**:
Sets are unordered collections of unique elements.
Used to perform set operations like union, intersection.
Example:
unique_numbers = {1, 2, 2, 3}
print(unique_numbers) # Outputs {1, 2, 3}
**8. Dictionaries (dict)**:
Dictionaries store key-value pairs. Keys must be unique and immutable.
Used for mapping, configurations, storing JSON-like data.
Example:
person = {'name': 'Alice', 'age': 30}
print(person['name'])
**Type Casting**:
You can convert data from one type to another using type casting functions:
- int(): Converts to integer
- float(): Converts to float
- str(): Converts to string
- bool(): Converts to boolean
Example:
x = int('5') # Converts string to int
print(x + 2)