YDP Python Lessons

YDP Python Lesson Exercises

Module 03 Exercises

Boolean Values, Conditional Execution, Loops, Lists, and List Processing, Logical and Bitwise Operations

Exercise 1: Conditional Statements


    
# Write a program that checks whether a given number 'num' is even or odd.
# Use conditional statements (if, else) to display an appropriate message.
# Test the program with different values of 'num'.
        
# Add your code here to check if 'num' is even or odd and display a message
        

Explanation: This exercise covers the use of conditional statements (if-else) to make decisions based on Boolean values, specifically whether a number is even or odd.

Exercise 2: List Manipulation


# Create a list of numbers and use a loop to iterate through the list.
# Calculate the sum of all the numbers in the list and display it.
# Calculate the product of all the numbers in the list and display it.
       
numbers = [1, 2, 3, 4, 5]
       
# Add your code here to calculate sum and product of numbers in the list
        

Explanation: This exercise involves working with lists, using loops (e.g., for loop) to process list elements, and performing calculations on list elements.

Exercise 3: Logical and Bitwise Operations


# Write a program that takes two integers as input.
# Perform logical AND, OR, and XOR operations on these integers.
# Perform bitwise AND, OR, and XOR operations on these integers.
# Display the results.
        
# Add your code here to perform logical and bitwise operations
        

Explanation: This exercise covers both logical operations (AND, OR, XOR) and bitwise operations (bitwise AND, OR, XOR) on integers.

Exercise 10: Looping and List Manipulation



# Create a list of numbers.
# Use a loop to find and display the maximum number in the list.
# Use another loop to find and display the minimum number in the list.
        
numbers = [15, 6, 23, 8, 12, 40]
        
# Add your code here to find the maximum and minimum numbers in the list
        

Explanation: This exercise involves using loops to iterate through a list and find the maximum and minimum values.

Exercise 4: Boolean Logic in Conditional Statements


# Write a program that asks the user for their age.
# Use conditional statements to check if the user is eligible to vote (age >= 18).
# Display an appropriate message.
        
# Add your code here to check eligibility and display a message
        

Explanation: This exercise combines user input, conditional statements, and Boolean logic to determine if a user is eligible to vote.

Exercise 5: Bitwise Shift Operations


# Take an integer input from the user.
# Perform left and right bitwise shift operations on the integer.
# Display the results.
        
# Add your code here to perform bitwise shift operations
        
        

Explanation: This exercise introduces bitwise shift operations (left and right) and their use with integers.

Exercise 6: Using a While Loop

# Write a program that asks the user to guess a secret number between 1 and 10.
# Use a while loop to repeatedly ask for guesses until the user guesses correctly.
# Provide feedback if the guess is too high or too low.
# Display a congratulatory message when the correct guess is made.
        
# Add your code here for the guessing game
        
        

Explanation: This exercise involves using a while loop for repeated user input until a specific condition (correct guess) is met.

Exercise 7: List Comprehensions

# Create a list of numbers.
 # Use a list comprehension to create a new list containing only the even numbers from the original list.
 # Display the new list.
        
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        
# Add your code here to create and display a list of even numbers
        
        

Explanation: This exercise introduces list comprehensions, a concise way to create new lists based on existing ones.

Exercise 8: Logical Operators in Conditionals

# Write a program that checks if a user's input is a positive number greater than 10.
# Use logical operators 'and' and 'or' to combine conditions.
        # Display appropriate messages based on the input.
        
# Add your code here to check user input and display messages
        
        

Explanation: This exercise involves using logical operators 'and' and 'or' to combine conditions in conditional statements.

Exercise 9: Bitwise NOT Operation

# Take an integer input from the user.
# Perform the bitwise NOT operation on the integer.
# Display the result.
        
# Add your code here to perform the bitwise NOT operation
        
        

Explanation: This exercise focuses on the bitwise NOT operation, which flips the binary bits of an integer.

Exercise 10: Nested Loops

# Create a nested loop that prints a pattern of stars (*).
# The outer loop controls the number of rows, and the inner loop controls the number of stars in each row.
# Allow the user to specify the number of rows.
        
# Add your code here to create and display the star pattern
        
        

Explanation: This exercise demonstrates nested loops, where one loop is inside another, to create patterns.

Submit your exercise files below

Submit