COMP6010: Python Comparison Operators
7 Questions
4 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output of the code snippet where a = 5 and b = 1 if a > 2 else 2?

  • 0
  • 1 (correct)
  • 2
  • 5
  • Inline if-else statements can use 'elif' conditions.

    False

    What is the condition to check if a year is a leap year?

    (year%4==0 and year%100!=0) or year%400==0

    If a deposit is between $1,000 and $10,000, what is the interest rate?

    <p>1.5%</p> Signup and view all the answers

    A deposit of $50,001-$100,000 has an interest rate of ___%.

    <p>2</p> Signup and view all the answers

    What condition checks whether an income is over $180,000?

    <p>income &gt;= 180000</p> Signup and view all the answers

    How can you calculate the interest for a given bank deposit?

    <p>Use the provided interest rates based on the deposit amount.</p> Signup and view all the answers

    Study Notes

    Python Comparison Operators

    • Equal operator: x == y checks if x is equal to y.
    • Not equal operator: x != y checks if x is not equal to y.
    • Greater than operator: x > y checks if x is greater than y.
    • Less than operator: x < y checks if x is less than y.
    • Greater than or equal to operator: x >= y checks if x is greater than or equal to y.
    • Less than or equal to operator: x <= y checks if x is less than or equal to y.
    • Boolean expressions can be used for comparisons, e.g., x < 8 yields True or False based on x's value.

    Inline If-Else Statements

    • Syntax: statement1 if condition else statement2.
    • Executes statement1 if the condition is True; otherwise executes statement2.
    • Provides a concise one-line alternative to traditional if-else structures.
    • Cannot use elif within inline if-else statements.
    • Example:
      • For a = 5, b = 1 if a > 2 else 2 results in b being 1.

    Leap Year Calculation

    • A year is considered a leap year if:
      • It is divisible by 4 AND not divisible by 100, OR
      • It is divisible by 400.
    • Example code checks for leap year:
      • if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

    Bank Deposit Interest Calculation

    • Interest rates based on deposit amounts:
      • $1 - $1,000: 1%
      • $1,001 - $10,000: 1.5%
      • $10,001 - $50,000: 1.8%
      • $50,001 - $100,000: 2%
      • Over $100,001: 2.5%
    • Example code demonstrates how to calculate interest based on the deposit amount.

    Income Tax Calculation Framework

    • Different tax rates apply based on income levels.
    • Base framework involves checking income against various thresholds to determine incremental tax rates.
    • Example check:
      • If income is greater than $180,000, additional tax is calculated at 45% on the income exceeding $180,000.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Week3-conditions.pdf

    Description

    Test your understanding of Python comparison operators in this COMP6010 quiz. Topics include equality, inequality, and relational operators. Perfect for students looking to solidify their knowledge in fundamental programming concepts.

    More Like This

    Python Operators Overview
    5 questions

    Python Operators Overview

    UnconditionalTellurium avatar
    UnconditionalTellurium
    Python Boolean Data Types and Operators
    9 questions
    Python Arithmetic and Comparison Operators
    17 questions
    Use Quizgecko on...
    Browser
    Browser