python-control-flow-and-functions

Certainly! Here’s a cheat sheet for Lesson 2: Control Flow and Functions:

Control Flow:

  • Conditional Statements:
    • if statement: Executes a block of code if a condition is true.
    • elif statement: Additional condition(s) to check if the previous condition(s) are false.
    • else statement: Executes a block of code if none of the previous conditions are true.
  • Example:
x = 10
if x > 5:
    print("x is greater than 5")
elif x == 5:
    print("x is equal to 5")
else:
    print("x is less than 5")
  • Looping:
    • for loop: Iterates over a sequence (e.g., list, tuple, string).
    • while loop: Executes a block of code repeatedly as long as a condition is true.
  • Example:
# for loop
for i in range(5):
    print(i)

# while loop
x = 0
while x < 5:
    print(x)
    x += 1

Functions:

  • Definition: A block of reusable code that performs a specific task.
  • Syntax:
def function_name(parameters):
    # function body
    return value
  • Example:
def greet(name):
    return "Hello, " + name + "!"

print(greet("Alice"))
  • Parameters: Variables passed into a function.
  • Return Value: Data returned by a function.

Notes:

  • Use indentation to define blocks of code within if statements, loops, and functions.
  • Functions improve code reusability and organization.
  • Functions can have parameters and return values, but they are not mandatory.