← Back to Python Basics
Module 1Lesson 32 hours

Variables and Data Types (Deep Dive)

Variables and Data Types - Deep Dive

Now that you understand the basics, let's explore variables and data types in much more depth.

Multiple Assignment

Python allows you to assign values to multiple variables in one line:

# Multiple assignment x, y, z = 10, 20, 30 print(x, y, z) # Output: 10 20 30 # Assign same value to multiple variables a = b = c = 100 print(a, b, c) # Output: 100 100 100 # Swap values (Python's elegant way!) x, y = 5, 10 x, y = y, x print(x, y) # Output: 10 5

Advanced Numeric Types

Integers with Underscores

For readability with large numbers:

# Use underscores for readability population = 7_800_000_000 print(population) # Output: 7800000000 # Python integers can be arbitrarily large huge = 123456789012345678901234567890 print(huge)

Floating-Point Numbers

price = 19.99 temperature = -5.5 pi = 3.14159 # Scientific notation speed_of_light = 3.0e8 # 3.0 × 10^8 print(speed_of_light) # Output: 300000000.0 # Very small numbers planck = 6.626e-34 print(planck)

Complex Numbers

z = 3 + 4j print(z) # Output: (3+4j) print(z.real) # Output: 3.0 print(z.imag) # Output: 4.0

Advanced String Operations

String Indexing

word = "Python" # Positive indexing (starts at 0) print(word[0]) # Output: P print(word[-1]) # Output: n (last character)

String Slicing

word = "Python" print(word[0:3]) # Output: Pyt print(word[2:]) # Output: thon print(word[:4]) # Output: Pyth print(word[::2]) # Output: Pto (every 2nd character) print(word[::-1]) # Output: nohtyP (reverse)

String Methods

text = "Hello, World!" # Case conversion print(text.upper()) # HELLO, WORLD! print(text.lower()) # hello, world! print(text.title()) # Hello, World! # Searching print(text.find("World")) # 7 print(text.count("l")) # 3 # Replacing print(text.replace("World", "Python")) # Hello, Python!

Type Conversion in Depth

Converting Between Types

# String to Integer age_str = "25" age_int = int(age_str) print(age_int + 5) # 30 # String to Float price_str = "19.99" price_float = float(price_str) print(price_float * 2) # 39.98 # Number to String score = 100 score_str = str(score) print("Your score is " + score_str) # Integer to Float x = 5 y = float(x) print(y) # 5.0 # Float to Integer (truncates decimal) pi = 3.14159 pi_int = int(pi) print(pi_int) # 3

Handling Conversion Errors

# This will cause an error try: num = int("hello") # ValueError except ValueError: print("Cannot convert 'hello' to integer!") # Check before converting text = "123" if text.isdigit(): number = int(text) print(number)

The None Type

Represents the absence of a value:

result = None print(result) # Output: None print(type(result)) # <class 'NoneType'> # Checking for None if result is None: print("No value assigned")

Boolean Operations

# Boolean values is_raining = True is_sunny = False # Boolean from comparisons age = 18 is_adult = age >= 18 print(is_adult) # Output: True # Boolean operations print(True and False) # False print(True or False) # True print(not True) # False

Truthy and Falsy Values

# Falsy values (evaluate to False) print(bool(0)) # False print(bool("")) # False (empty string) print(bool([])) # False (empty list) print(bool(None)) # False # Truthy values (evaluate to True) print(bool(1)) # True print(bool("Hello")) # True print(bool([1, 2])) # True

Best Practices

Variable Naming

# Good - descriptive names student_count = 30 max_temperature = 75 user_email = "alice@example.com" # Bad - unclear x = 30 temp = 75 ue = "alice@example.com"

Constants

# Use UPPERCASE for constants PI = 3.14159 MAX_ATTEMPTS = 3 DEFAULT_COLOR = "blue"

Practice Exercises

  1. Create variables with different data types and swap their values
  2. Convert between different numeric types and observe the results
  3. Use string slicing to extract parts of a sentence
  4. Check if values are truthy or falsy
  5. Practice type conversion with error handling

Next Lesson: Operators and Expressions