Blog Detail

September 1, 2023

Python Conditionals Exercises

  • Write a condition that checks if the item is affordable.

                    item_price = 1900.25
    wallet = 2100.50
    
    if '':
        print('The item is affordable!')
  • Hold the result of the comparison in a variable and then use the variable as the condition.

                    item_price = 1900.25
    wallet = 2100.50
    
    is_affordable = item_price <= wallet
    
    if '':
        print('The item is affordable!')
  • If the user wants to rent a car for more than 30 days, announce to the user that they are eligible for a 15% discount.

                    rent_days = 34
    
    if ''
        print('You are eligible for a discount of 15% on your order.')
  • Check if the file size exceeds the maximum allowable size for uploading.

                    file_size = 10     # 10 MB
    max_file_size = 5  # 5 MB
    
    '' file_size > max_file_size:
        print('File size exceeds the maximum allowable limit.')
        print('Please choose a smaller file.')
  • Check if the file size exceeds the maximum allowable size for uploading.
    Otherwise upload the file.

                    file_size = 2      # 2 MB
    max_file_size = 5  # 5 MB
    
    if file_size > max_file_size:
        print('File size exceeds the maximum allowable limit.')
        print('Please choose a smaller file.')
    '':
        print("File uploaded successfully!")
  • This code verifies the password and confirm password entered by the user.
    What will be the output of this program?

                    password = "SecurePassword123"
    confirm_password = "SecurPassword123"
    
    if password == confirm_password:
        print('Password and confirm password match. Registration successful!')
    else:
        print('Password and confirm password are not the same!')
  • Based on the app logic, a valid product code contains only digits.
    Write an if condition that checks if the given product code is valid or not.

                    product_code = '1001'
    
    if ''
        print("Valid product code.")
    else:
        print("Invalid product code. Please enter a numeric value.")
  • This code verifies the password and confirm password entered by the user.
    What will be the output of this program?

                    password = "SecurePassword123"
    confirm_password = "Securepassword123"
    
    if password == confirm_password:
        print('Password and confirm password match. Registration successful!')
    else:
        print('Password and confirm password are not the same!')
    
  • Python Exercises for Conditionals

    • If you've faced difficulties with these exercises, take a look at Conditionals Module on my Python course.


    You can also share 'Python Conditionals' Exercises on your:
    X (Twitter) - Telegram - Linkedin - Facebook - or on your favorite platform...




    Happy Coding!
    Behnam Khani