Questions and Answers
What is the output of the code snippet where a = 5 and b = 1 if a > 2 else 2?
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?
Signup and view all the answers
A deposit of $50,001-$100,000 has an interest rate of ___%.
Signup and view all the answers
What condition checks whether an income is over $180,000?
Signup and view all the answers
How can you calculate the interest for a given bank deposit?
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 executesstatement2
. - 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 inb
being 1.
- For
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.
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.