Menu iconMenu iconAlgorithms and Data Structures with Python
Algorithms and Data Structures with Python

Chapter 2: Diving into Python

Chapter 2: Practical Exercises of Diving into Python

Exercise 1: Conditional Greetings

Given a person's name and the time of the day (morning, afternoon, evening), craft a function named time_greeting that returns an appropriate greeting.

def time_greeting(name, time_of_day):
    # Your code here

# Example:
# time_greeting("Alice", "morning") should return "Good morning, Alice!"

Exercise 2: Loop through Colors

You are given a list of colors. Write a for loop that prints out each color with its respective index.

colors = ["red", "green", "blue", "yellow"]

# Your code here

Exercise 3: Function Calculator

Design a function named simple_calculator that takes in three parameters: two numbers and an operator (either "+", "-", "*", or "/"). The function should return the result of the operation.

def simple_calculator(num1, num2, operator):
    # Your code here

# Example:
# simple_calculator(5, 3, "+") should return 8

Exercise 4: Is It a Leap Year?

Create a function named is_leap_year that determines if a given year is a leap year. Remember, a leap year is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

def is_leap_year(year):
    # Your code here

# Example:
# is_leap_year(2000) should return True
# is_leap_year(1900) should return False

Exercise 5: Lambda Square

Using a lambda function, craft a function named get_square that returns the square of a number.

# Your lambda function here

# Example:
# get_square(6) should return 36

Exercise 6: Factorial Using Recursion

Using recursion, design a function named recursive_factorial to compute the factorial of a number.

def recursive_factorial(n):
    # Your code here

# Example:
# recursive_factorial(4) should return 24

Exercise 7: Countdown Generator

Utilizing the concept of generators, design a generator named countdown_gen that yields numbers from a given number down to 1.

def countdown_gen(num):
    # Your code here

# Example:
# for i in countdown_gen(3):
#     print(i)
# Should output:
# 3
# 2
# 1

After you've tried your hand at these exercises, cross-check your solutions with peers or mentors. The true essence of coding is not just knowing the syntax or logic, but practicing until patterns become second nature. Have fun coding, and don't forget to relish every "Eureka!" moment you encounter.

Chapter 2: Practical Exercises of Diving into Python

Exercise 1: Conditional Greetings

Given a person's name and the time of the day (morning, afternoon, evening), craft a function named time_greeting that returns an appropriate greeting.

def time_greeting(name, time_of_day):
    # Your code here

# Example:
# time_greeting("Alice", "morning") should return "Good morning, Alice!"

Exercise 2: Loop through Colors

You are given a list of colors. Write a for loop that prints out each color with its respective index.

colors = ["red", "green", "blue", "yellow"]

# Your code here

Exercise 3: Function Calculator

Design a function named simple_calculator that takes in three parameters: two numbers and an operator (either "+", "-", "*", or "/"). The function should return the result of the operation.

def simple_calculator(num1, num2, operator):
    # Your code here

# Example:
# simple_calculator(5, 3, "+") should return 8

Exercise 4: Is It a Leap Year?

Create a function named is_leap_year that determines if a given year is a leap year. Remember, a leap year is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

def is_leap_year(year):
    # Your code here

# Example:
# is_leap_year(2000) should return True
# is_leap_year(1900) should return False

Exercise 5: Lambda Square

Using a lambda function, craft a function named get_square that returns the square of a number.

# Your lambda function here

# Example:
# get_square(6) should return 36

Exercise 6: Factorial Using Recursion

Using recursion, design a function named recursive_factorial to compute the factorial of a number.

def recursive_factorial(n):
    # Your code here

# Example:
# recursive_factorial(4) should return 24

Exercise 7: Countdown Generator

Utilizing the concept of generators, design a generator named countdown_gen that yields numbers from a given number down to 1.

def countdown_gen(num):
    # Your code here

# Example:
# for i in countdown_gen(3):
#     print(i)
# Should output:
# 3
# 2
# 1

After you've tried your hand at these exercises, cross-check your solutions with peers or mentors. The true essence of coding is not just knowing the syntax or logic, but practicing until patterns become second nature. Have fun coding, and don't forget to relish every "Eureka!" moment you encounter.

Chapter 2: Practical Exercises of Diving into Python

Exercise 1: Conditional Greetings

Given a person's name and the time of the day (morning, afternoon, evening), craft a function named time_greeting that returns an appropriate greeting.

def time_greeting(name, time_of_day):
    # Your code here

# Example:
# time_greeting("Alice", "morning") should return "Good morning, Alice!"

Exercise 2: Loop through Colors

You are given a list of colors. Write a for loop that prints out each color with its respective index.

colors = ["red", "green", "blue", "yellow"]

# Your code here

Exercise 3: Function Calculator

Design a function named simple_calculator that takes in three parameters: two numbers and an operator (either "+", "-", "*", or "/"). The function should return the result of the operation.

def simple_calculator(num1, num2, operator):
    # Your code here

# Example:
# simple_calculator(5, 3, "+") should return 8

Exercise 4: Is It a Leap Year?

Create a function named is_leap_year that determines if a given year is a leap year. Remember, a leap year is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

def is_leap_year(year):
    # Your code here

# Example:
# is_leap_year(2000) should return True
# is_leap_year(1900) should return False

Exercise 5: Lambda Square

Using a lambda function, craft a function named get_square that returns the square of a number.

# Your lambda function here

# Example:
# get_square(6) should return 36

Exercise 6: Factorial Using Recursion

Using recursion, design a function named recursive_factorial to compute the factorial of a number.

def recursive_factorial(n):
    # Your code here

# Example:
# recursive_factorial(4) should return 24

Exercise 7: Countdown Generator

Utilizing the concept of generators, design a generator named countdown_gen that yields numbers from a given number down to 1.

def countdown_gen(num):
    # Your code here

# Example:
# for i in countdown_gen(3):
#     print(i)
# Should output:
# 3
# 2
# 1

After you've tried your hand at these exercises, cross-check your solutions with peers or mentors. The true essence of coding is not just knowing the syntax or logic, but practicing until patterns become second nature. Have fun coding, and don't forget to relish every "Eureka!" moment you encounter.

Chapter 2: Practical Exercises of Diving into Python

Exercise 1: Conditional Greetings

Given a person's name and the time of the day (morning, afternoon, evening), craft a function named time_greeting that returns an appropriate greeting.

def time_greeting(name, time_of_day):
    # Your code here

# Example:
# time_greeting("Alice", "morning") should return "Good morning, Alice!"

Exercise 2: Loop through Colors

You are given a list of colors. Write a for loop that prints out each color with its respective index.

colors = ["red", "green", "blue", "yellow"]

# Your code here

Exercise 3: Function Calculator

Design a function named simple_calculator that takes in three parameters: two numbers and an operator (either "+", "-", "*", or "/"). The function should return the result of the operation.

def simple_calculator(num1, num2, operator):
    # Your code here

# Example:
# simple_calculator(5, 3, "+") should return 8

Exercise 4: Is It a Leap Year?

Create a function named is_leap_year that determines if a given year is a leap year. Remember, a leap year is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

def is_leap_year(year):
    # Your code here

# Example:
# is_leap_year(2000) should return True
# is_leap_year(1900) should return False

Exercise 5: Lambda Square

Using a lambda function, craft a function named get_square that returns the square of a number.

# Your lambda function here

# Example:
# get_square(6) should return 36

Exercise 6: Factorial Using Recursion

Using recursion, design a function named recursive_factorial to compute the factorial of a number.

def recursive_factorial(n):
    # Your code here

# Example:
# recursive_factorial(4) should return 24

Exercise 7: Countdown Generator

Utilizing the concept of generators, design a generator named countdown_gen that yields numbers from a given number down to 1.

def countdown_gen(num):
    # Your code here

# Example:
# for i in countdown_gen(3):
#     print(i)
# Should output:
# 3
# 2
# 1

After you've tried your hand at these exercises, cross-check your solutions with peers or mentors. The true essence of coding is not just knowing the syntax or logic, but practicing until patterns become second nature. Have fun coding, and don't forget to relish every "Eureka!" moment you encounter.