Data Types and Variables β
Master Python's fundamental data types including numbers, strings, booleans, and complex data structures
π’ Numbers β
Integers (int) β
Whole numbers without decimal points:
python
age = 25
count = 0
negative = -10
print(f"Age: {age}")
print(f"Type: {type(age)}")
# Expected output:
# Age: 25
# Type: <class 'int'>Floats (float) β
Numbers with decimal points:
python
price = 19.99
temperature = -5.5
pi = 3.14159
print(f"Price: ${price}")
print(f"Temperature: {temperature}Β°C")
print(f"Type: {type(price)}")
# Expected output:
# Price: $19.99
# Temperature: -5.5Β°C
# Type: <class 'float'>print(type(price)) # <class 'float'>
### **Complex Numbers**
```python
complex_num = 3 + 4j
print(type(complex_num)) # <class 'complex'>π Strings β
Creating Strings β
python
name = "Alice"
message = 'Hello, World!'
multiline = """This is a
multiline string"""
print(type(name)) # <class 'str'>String Operations β
python
first_name = "John"
last_name = "Doe"
# Concatenation
full_name = first_name + " " + last_name
print(full_name) # John Doe
# String formatting
age = 25
message = f"My name is {first_name} and I am {age} years old"
print(message)β Booleans β
python
is_student = True
is_graduated = False
is_active = True
print(type(is_student)) # <class 'bool'>
# Boolean operations
print(is_student and is_graduated) # False
print(is_student or is_graduated) # True
print(not is_student) # Falseπ Type Conversion β
Converting Between Types β
python
# String to integer
age_str = "25"
age_int = int(age_str)
print(age_int) # 25
# Integer to string
count = 100
count_str = str(count)
print(count_str) # "100"
# String to float
price_str = "19.99"
price_float = float(price_str)
print(price_float) # 19.99
# Float to integer (truncates decimal)
price = 19.99
price_int = int(price)
print(price_int) # 19π Useful Functions β
type() Function β
python
name = "Alice"
age = 25
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>id() Function β
python
x = 10
y = 10
print(id(x)) # Memory address
print(id(y)) # Same memory address (Python optimization)isinstance() Function β
python
age = 25
print(isinstance(age, int)) # True
print(isinstance(age, str)) # Falseπ― Practice Examples β
Example 1: User Information β
python
# Collect user information
name = "Alice"
age = 30
height = 5.6
is_student = False
# Display information
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height} feet")
print(f"Is student: {is_student}")Example 2: Simple Calculator β
python
# Numbers
num1 = 10
num2 = 3
# Calculations
sum_result = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2
print(f"Sum: {sum_result}")
print(f"Difference: {difference}")
print(f"Product: {product}")
print(f"Quotient: {quotient}")π Key Takeaways β
- int: Whole numbers (positive, negative, zero)
- float: Decimal numbers
- str: Text data in quotes
- bool: True or False values
- Use
type()to check data types - Use
int(),float(),str()for type conversion - Python automatically determines variable types
Continue to: Operators β