portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/cs1201_week2_1_201… · web...

13
CSX3001 Fundamentals of Computer Programming, Semester 1/2019 Week 2 Conditional Tests At the heart of every if statement is an expression that can be evaluated as True or False, and is called a conditional test. Python uses the values True and False to decide whether the code in an if statement should be executed. If a conditional test evaluates to True, Python executes the code following the if statement. If the test evaluates to False, Python ignores the code following if statement. For example, The equality operator (==) returns True if

Upload: others

Post on 09-Oct-2019

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

Week 2

Conditional Tests

At the heart of every if statement is an expression that can be evaluated as True or False, and is called a conditional test. Python uses the values True and False to decide whether the code in an if statement should be executed.

If a conditional test evaluates to True, Python executes the code following the if statement. If the test evaluates to False, Python ignores the code following if statement. For example,

The equality operator (==) returns True if the values on the left and right size of the operator match.

Note that checking for equality in Python is case sensitive. For example, if you type

Page 2: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

car = ‘Audi’if car == ‘audi’ # <-- this returns False since Audi is not the same as audi print(car)

Expression

Meaning

x == y True if x = y (mathematical equality, not assignment; otherwise, false

x < y True if x < y; otherwise, falsex <= y True if x ≤ y; otherwise, falsex > y True if x > y; otherwise, falsex >= y True if x ≥ y; otherwise, falsex != y True if x y; otherwise, false

Examples10 < 20 True10 >= 20 Falsex < 100 True if x Is less than 100; otherwise,

Falsex != y True unless x and y are equal

1)Write a Python code that takes two integers, x and y. Then print out only the lowest value.

Hint: You need to define a simple if-else condition to check if x is greater than y (then y is the smallest value), then print out the value of y. Otherwise (or else), print out the value of x.

Page 3: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

2)Write a Python code that takes three integer x, y and z. Then print out the integer that has the value between the lowest and highest values.

Checking for Inequality in Python

In Python, when you want to determine whether two values are not equal, you can combine an exclamation point and an equal sign (!=). The exclamation point represents not, as it does in many programming languages. For example,

Checking Multiple Conditions

You may want to check multiple conditions at the same time. For example, you may want two conditions to be True to take action. You need and and or for this situation.

Operator not has higher precedence than both and and or. and has higher precedence than or.

Page 4: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

and, or have lower precedence than any other binary operator except assignment.

For example, the following Python code prints the first and the second True since the conditions return True. However, the third if-statement returns False, so it ignores all statements following if.

The if-elif-else Chain

Often, you will need to test more than two possible situations, and to evaluate these you can use Python’s if-elif-else syntax. Python executes only one block in an if-elif-else chain. It runs each conditional test in order until one passes. When a test passes, the code following that test is executed, and Python skips the rest of the tests. For example,

Rather than typing ‘Your admission cost …’ with different fees in every if-statement. You can format

Page 5: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

your output by calling print() function just one time after if-elif-else statement, For example,

Note that you can also omit (ignore) the else block in Python.

3)Write a Python code to take a height as an input. Print the message according to the following table. Note that you must use f-strings for your output format.

Lower than 80

You are too small for this ride.

80 – 180 You are ok for this ride.Higher than 180

You are too tall for this ride.

4) Imagine an alien was just shot down in a game. Create a variable called alien_color and assign a value of ‘green’, ‘yellow’, or ‘red’.

Page 6: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

Green You have earned 5 points.Yellow You have earned 10 points.Red You have earned 100 points.

5)Stages of life: write an if-elif-else chain that determines a person’s stage of life. Take a value of age as an input, and print the message according the following ranges:

Age (years old) MessageLess than 2 The person is a baby.At least 2 but less than 4

The person is a toddler.

At least 4 but less than 13

The person is a kid.

At least 13 but less than 20

The person is a teenager.

At least 20 but less than 62

The person is an adult.

65 or older The person is an elder.

Working with Lists#1 Looping Through an Entire List

You will often want to run through all entries (elements) in a list, performing the same task with each item. When you want to do that, in Python, you can use Python’s for loop. For example,

Page 7: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

for w in words – this line tells Python to pull animal names from the list, and associate it with the variable w. Then we tell Python to print the w’s value together with the length of that word using len() function. Then we insert a new animal at the front of the list.

6)Change ‘Tuger’ to ‘Tiger’ and print all the words with at least five characters. After the loop, print the following message “These animals look great.”.

7)Define a list with 5 heroes (Hulk, Thor, Ironman, Spiderman, Vision). Go through all entries (elements) in the list and print a message according to the following table

Number of characters4 or lower Weak Hero5 Fine HeroMore than 5 Awesome Hero

Avoiding Indentation Errors

Python uses indentation to determine how a line, or a group of lines, is related to the rest of the program.

Page 8: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

The use of indentation makes code very easy to read. In longer Python programs, you will notice blocks of code indented at a few different levels. These indentation levels help you gain a general sense of overall program organization.

Sometimes you may come across what we called indentation errors message, when you forget to indent. For example,

Keep that in mind and do not repeat this mistake.8) Try the following Python code.

- What does function break for? ______________________________________

Output Formatting Alternatives

Previously, f-string is used to format your output. In some scenario, you may want to add few more whitespaces, or limit a number of decimal points. Consider the following example

Page 9: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

This syntax tells Python that, for example, at %.2f, the value to be printed is going to be a floating point value with 2 decimal points. %d is for an integer.

You can define a space for your output by adding a number of whitespaces you want to appear. For example, %10.2f means for that output, Python will reserve 10 spaces.

9)Write a Python code that takes two integers, x and y, which are real numbers. Then print x with 3 digits after decimal point and print y with 2 digits after decimal point. 

10) Write a Python code that takes two integers, x and y, which are real numbers. Then print both with 8 characters (including spaces) with 2 digits after decimal point for x and 1 digit after decimal point for y. 

11) Write a Python code that computes a total balance in the account after depositing the money for n years with the first deposit of x Baht, and the interest rate is fixed at y% per year. The interest is calculated at each completion of one year and the interest earned is deposited into the account. 

Page 10: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019

Show the result in the following format. (Assume that the first deposit is 20000 and the money has been deposited for 3 years)Year Interest Rate Interest Earned Balance 1 10 2000.00 22000.002 10 2200.00 24200.003 10 2420.00 26620.00

Extra Exercises

12) Try the following Python code

- At which loop does a function break execute? ___________________________

- Modify the code to produce the following outputs.

Page 11: portal.scitech.au.eduportal.scitech.au.edu/.../uploads/2019/07/CS1201_Week2_1_201… · Web viewExamples. 10 < 20True. 10 >= 20False. x < 100True if x Is less than 100; otherwise,

CSX3001 Fundamentals of Computer Programming, Semester 1/2019