Skip to content

Latest commit

 

History

History
218 lines (169 loc) · 5.47 KB

File metadata and controls

218 lines (169 loc) · 5.47 KB

🐍 Python Day 5 Cheat Sheet

Organizing Code with Functions


📝 Table of Contents

  1. Core Definitions
  2. Key Differences: print vs. return
  3. Defining and Calling Functions
  4. Parameters and Arguments
  5. Return Values
  6. Advanced Concepts
  7. Quick Reference
  8. Pro Tips
  9. Common Mistakes to Avoid

🧠 Core Definitions

  • Function: A named, reusable block of code that performs a specific action.
  • def: The keyword used to define a function.
  • Call: The act of executing a function by writing its name followed by parentheses ().
  • Parameter: A variable name listed in a function's definition. It's a placeholder for an input value.
  • Argument: The actual value that is passed to a function when it is called.
  • return: A keyword that exits a function and sends a value back to the caller.
  • Recursion: The process of a function calling itself to solve a problem.

📊 Key Differences: print vs. return

Feature print() return
Purpose Displays a value to the screen for a human to see. Sends a value back to the program for further use.
Output Visual output in the console. A value that can be stored in a variable.
Use Case Showing status, results, or messages. Providing a result for calculations or assignments.

🗂️ Defining and Calling Functions

Basic Function (No Inputs)

# 1. Define the function
def show_greeting():
    print("Hello from a function!")

# 2. Call the function to run its code
show_greeting()

📥 Parameters and Arguments

Function with One Parameter

# 'name' is the parameter (a placeholder)
def greet(name):
    print(f"Hello, {name}!")

# "Alice" and "Bob" are the arguments (the actual values)
greet("Alice")
greet("Bob")

📤 Return Values

Getting a Value Back

# This function calculates a value and sends it back
def square(number):
    return number * number

# You must store the returned value in a variable to use it later
result = square(5) # result is now 25
print(result)

⚙️ Advanced Concepts

Function Calling Another Function

Functions can be broken down into smaller "helper" functions.

def calculate_tax(price):
    """Calculates an 8% tax for a given price."""
    return price * 0.08

def get_final_price(price):
    """Calculates the final price by adding tax."""
    tax = calculate_tax(price) # Calling the helper function
    return price + tax

final_cost = get_final_price(100)
print(f"The final cost is ${final_cost:.2f}") # The final cost is $108.00

Recursion (A Function Calling Itself)

A function can call itself, but it must have a base case to stop.

def countdown(n):
    # Base Case: The condition to stop the recursion.
    if n <= 0:
        print("Blast off!")
        return

    # Recursive Step: The function calls itself with a smaller problem.
    print(n)
    countdown(n - 1)

countdown(3)

⚡ Quick Reference

Function Syntax

# Defining a function
def function_name(parameter1, parameter2):
    # Code to be executed
    result = parameter1 + parameter2
    return result

# Calling a function and storing its return value
my_result = function_name(argument1, argument2)

Docstrings

It's good practice to add a "docstring" to explain what your function does.

def add(a, b):
    """This function takes two numbers and returns their sum."""
    return a + b

💡 Pro Tips

  1. One Function, One Job: The best functions do one specific thing and do it well. This makes them easy to test, reuse, and understand.

  2. Use Helper Functions: Don't be afraid to break a large function into several smaller ones. This improves readability and organization.

  3. Default Parameter Values: You can provide a default value for a parameter, making it optional when the function is called.

    def greet(name="World"):
        print(f"Hello, {name}!")
    
    greet("Alice") # Prints "Hello, Alice!"
    greet()        # Prints "Hello, World!"

🎯 Common Mistakes to Avoid

Forgetting to Call the Function

# Wrong: This code defines the function but never runs it.
def say_hello():
    print("Hello!")
# Nothing will be printed.

Always Call the Function

# Correct
def say_hello():
    print("Hello!")

say_hello() # This line actually executes the code.

Forgetting to return a Value

# Wrong: This function calculates a sum but doesn't send it back.
def add(a, b):
    result = a + b
    # No return statement!

total = add(5, 3)
print(total) # This will print 'None' because the function returned nothing.

Always return the Result

# Correct
def add(a, b):
    return a + b

total = add(5, 3)
print(total) # This correctly prints 8.

Infinite Recursion

# Wrong: There is no base case to stop the function from calling itself.
def uh_oh(n):
    print("Oh no!")
    uh_oh(n) # This will run forever and crash.

Always Have a Base Case

# Correct: The `if` statement provides an exit.
def safe_recursion(n):
    if n <= 0:
        return
    safe_recursion(n - 1)

Happy coding! 🐍✨