Blog Detail

September 1, 2023

Python Operators Exercises

  • Suppose to initiate the shipping process for a product, two conditions must be met:

    • The product must be in stock
    • The customer’s shipping address must be within the country

    Now, determine whether the order should be processed for shipping or not?

                    product_in_stock = True  
    customer_shipping_address_within_country = True  
    is_eligible_for_shipping = product_in_stock and customer_shipping_address_within_country
    print('Is eligible for shipping?', is_eligible_for_shipping)
  • Suppose an employee’s eligibility for a role in account management is contingent upon meeting two conditions:

    • The employee must have excellent sales performance
    • The employee must receive positive customer feedback

    Now, considering these criteria, should this employee be considered for a role in account management?

                    excellent_sales_performance = True  
    positive_customer_feedback = False  
    is_employee_eligible = excellent_sales_performance and positive_customer_feedback
    print('Should this employee be considered for a role in account management?', is_employee_eligible)
  • A user can access a restricted area of a website if they have either an admin role or a moderator role.
    What value will be inside the has_user_access variable?

                    user_role = 'admin'
    has_user_access = user_role == "admin" or user_role == "moderator"
    print('Does the user have access?', has_user_access)
  • Check to see if the user has a valid payment option or not?
    A customer can choose to pay for a product using either a credit card or PayPal

                    has_credit_card = False
    has_paypal = False
    has_payment_option = has_credit_card '' has_paypal
    print('Does the user have a valid payment option?', has_payment_option)
  • A customer is eligible for free shipping if their order total is above 100.0 or if they are a premium member.
    Complete the code based on the logic.

                    order_total = 150.0  
    is_premium_member = True  
    free_shipping_limit = 100.0  
    is_eligible_for_free_shipping = order_total ''
    print('Is customer eligible for free shipping?', is_eligible_for_free_shipping)
  • Display a notification if there is either a system update or a new message in the user’s inbox.

                    has_system_update = False  
    has_new_message = False
    has_notification = has_system_update '' has_new_message
    print('Should display a notification?', has_notification)
  • To backup a file, it must be available and have enough storage.
    Complete the logic based on the criteria.

                    is_available = True
    has_sufficient_storage = True
    can_backup = is_available '' has_sufficient_storage
    • Conditions:
    • Is the product development complete?
    • Is the marketing campaign ready?
    • Are the manufacturing facilities operational?

    Check to see if all the necessary conditions are met to proceed with the product launch.

                    is_dev_complete = True
    is_campaign_ready = True
    are_facilities_operational = True
    launch_product = is_dev_complete '' is_campaign_ready and are_facilities_operational
  • Imagine you want to hire a candidate who has one or more of these conditions:

    • Does the candidate have a relevant degree?
    • Does the candidate have at least 3 years of experience?
    • Is the candidate proficient in the required programming languages?

    Which logical operator should you use?

  • Python Exercises for Operators

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

    You can also share ‘Python Operators’ Exercises on your:
    X (Twitter)TelegramLinkedinFacebook – or on your favorite platform…

    Happy Coding!
    Behnam Khani