Skip to content

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 β†’

Released under the MIT License.