Introduction
Python is a powerful, versatile programming language that’s perfect for data science. This module covers the essential building blocks you’ll need to start writing Python code.Variables and Data Types
Understanding Variables
Variables in Python are used to store data. Unlike some languages, Python uses dynamic typing, meaning you don’t need to declare the type explicitly.Best Practice: Use UPPERCASE for constants (values that shouldn’t change) and lowercase for regular variables.
Common Data Types
Numeric Types
Numeric Types
- int: Integer numbers (e.g., 30, -5, 1000)
- float: Decimal numbers (e.g., 4.0, 3.14, -2.5)
- bool: Boolean values (True or False)
String Type
String Type
Strings are sequences of characters enclosed in quotes.
Collection Types
Collection Types
- list: Ordered, mutable collection
- dict: Key-value pairs
- tuple: Ordered, immutable collection
Control Flow
Conditional Statements
Control the flow of your program based on conditions usingif, elif, and else.
- Basic If-Else
- Multiple Conditions
- Complex Logic
Logical Operators: Use
and, or, and not to combine multiple conditions. Use parentheses for clarity.Input Validation
Always validate user input to prevent errors:Functions
Defining Functions
Functions help organize code into reusable blocks:The
if __name__ == "__main__": pattern ensures code only runs when the file is executed directly, not when imported as a module.Functional Programming Concepts
Lambda Functions and Higher-Order Functions
Python supports functional programming with lambda functions,map(), filter(), and reduce():
Understanding Lambda Functions
Understanding Lambda Functions
Lambda functions are anonymous functions defined in a single line:Use lambda for simple operations, especially with
map(), filter(), and sorted().Best Practices
Use Descriptive Names
Choose variable and function names that clearly describe their purpose.
Add Docstrings
Document your functions with docstrings.
Handle Errors
Use try-except blocks to handle potential errors gracefully.
Validate Input
Always validate user input before processing.
Practice Exercises
Exercise 1: Grade Calculator
Exercise 1: Grade Calculator
Create a program that:
- Takes three grades as input
- Calculates the average
- Determines if the student passes (average >= 4.0)
- Prints a formatted result
float(input()) for input and conditional statements for evaluation.Exercise 2: Age Classifier
Exercise 2: Age Classifier
Write a function that takes an age and returns the appropriate category:
- 0-12: “niño/a”
- 13-17: “adolescente”
- 18-59: “adulto/a”
- 60+: “adulto/a mayor”
if-elif-else statements.Exercise 3: Inventory Filter
Exercise 3: Inventory Filter
Given a list of product dictionaries, write functions to:
- Filter products with stock > 0
- Convert product names to uppercase
- Calculate total inventory value
filter(), map(), and reduce().Next Steps
Now that you understand Python basics, you’re ready to explore:Object-Oriented Programming
Learn about classes, objects, and inheritance
Python Projects
Build real-world applications with Python