Full Transcript

COMP6010 Fundamentals of Computer Science Conditions Yan Wang [email protected] 1 Python Comparison Operators ComparisonOperators.py == Equal x == y != Not equal...

COMP6010 Fundamentals of Computer Science Conditions Yan Wang [email protected] 1 Python Comparison Operators ComparisonOperators.py == Equal x == y != Not equal x != y > Greater than x > y < Less than x= Greater than or equal to x >= y 4 is True x < 8 is True, x < 5 is False x >= 5 is True, x >= 4 is False x , >=, = 75: grade = "Distinction" elif mark >= 65: grade = "Credit" elif mark >= 50: grade = "Pass" else: grade = "Fail" print("Your mark is " + str(mark)) print("Your grade is " + grade) 21 Inline If-else statements inline-if-else.py statement1 if condition else statement2 execute statement1 if condition is True otherwise execute statement2 if condition evaluates to False a concise version of if-else statement written in just one line elif condition cannot be used in inline if-else statement Example: # alternatively a = 5 a=5 b = 1 if a>2 else 2 if a>2: b=1 print(b) else: # result is 1 b=2 print(b) 22 Examples and Exercises How to know if a year is a leap year? https://www.mathsisfun.com/leap-years.html 23 Examples and Exercises How to write the conditions for a If structure? https://www.mathsisfun.com/leap-years.html 24 Examples and Exercises year = int(input()) if (year%4==0 and year%100!=0) or year%400==0: print((str)(year) + " is a leap year") else: print((str)(year) + " is NOT a leap year") 25 Examples and Exercises Assume the following interest rates for bank deposit. Write a program to calculate the interest of a given deposit. 1) deposit $1-$1,000, interest rate: 1% 2) deposit $1,001-$10,000, interest rate: 1.5% 3) deposit $10,001-$50,000, interest rate: 1.8% 4) deposit $50,001-$100,000, interest rate: 2% 5) deposit $100,001 and above, interest rate: 2.5% 26 Examples and Exercises bankinterest.py deposit = int(input("Enter your deposit $")) interest = 0 rate = 0 if deposit >= 1 and deposit = 1001 and deposit = 10001 and deposit = 50001 and deposit =0 and income = 18201: if income = 45001: if income = 120001: if income = 180001: tax = tax + (income - 180000) * 0.45 print(tax) 29

Use Quizgecko on...
Browser
Browser