- Home
- Free Samples
- Programming
- ISY104 Conditions In Python Program: ...
ISY104 Conditions in Python Program: Activity Sheet Week 3 Assessment Answer

ISY104 Foundations of Programming Week 3 – Condition
Objective:
This week we will work on selection control structure and how to use conditions in Python program.
In this activity sheet we discuss question 1 to 3 on pages 1 to 3. Then we work on solving the questions on page 4.
1. What is the meaning of the following statement?
20 == 20
Answer: It is checking “Is 20 equal to 20?”. Note, it does not state that 20 is equal to 20. Specifically, not that == is not an assignment operator, it is a comparison operator.
2. What is the data type of the value produced, if any, by each of the statements or expressions in following segments of code?
Code Segments | Answer | Reasoning | |
1 | age = 20#01 age == 20#02 | Data type of age is int age == 20 is Boolean age == 20 evaluates to True | |
2 | 20 == 20 | Boolean, True | |
3 | 20 < 30 | Boolean, True | 20 is less than 30 |
4 | 20 > 30 | Boolean, False | 20 is not greater than 30 |
5 | age = 30 age <= 40 | Data type of age is int Boolean, True | |
6 | 40 != 30 | Boolean, True | |
7 | True | Boolean, True | |
8 | not True | Boolean, False | Not True is False |
9 | not False | Boolean, True | Not False is True |
10 | True == True | Boolean, True | True is equal to True |
11 | True == False | Boolean, False | True is not equal to False |
12 | 2+3 == 2+4 | Boolean, False | 5 is not equal to 6 |
13 | true | Syntax error | true is not defined. True is not same as true. Python is case sensitive. |
14 | false | Syntax error | true is not defined. True is not same as true. Python is case sensitive. |
15 | age= 5 age + 10 < 20 | Int Boolean, True | Int 15 is less than 20 |
16 | myAge =15 10<myAge and myAge <100 | 10 is less than 15 and 15 is less than 100 |
17 | 10<15 or 15<100 | Boolean, True | 10 is less than 15. Note if the first expression is True, the second is not checked |
18 | 25<15 or 15<100 | Boolean, True | 15 is less than 100. If any one condition is True, the final value is True |
19 | 10<15 or 200<100 | Boolean, True | 10 is less than 15. If any one condition is True, the final value is True |
20 | 10 <15 and 200 <100 | Boolean, False | 200 is not less than 100. Both conditions have to be True |
21 | 15 < 10 and 100 < 200 | Boolean, False | 15 is not less than 10. Both conditions have to be True |
22 | age =6 age >5 and age <18 or age >100 | True | Age is between 5 and 18 though age>100 is False |
23 | age =6 age >5 and age<18 and age >100 | False | Age is between 5 and 18 but age>100 is False. The and operator makes it False this time |
3. What is the output produced by the following code? Explain.
Code Segments | Output | Reasoning | |
1 | age=5#01 if age > 5:#02 print(“School age”) #03 print(“done”)#04 | Statement 03 is not executed because age>5 on statement 02 evaluates to False. Statement 04 is executed because it is not in the body of the if statement | |
2 | age=6#01 if age > 5:#02 print(“School age”) #03 print(“done”)#04 | Statement 03 is executed because age>5 on statement 02 evaluates to True. Statement 04 is executed because it is not in the body of the if statement | |
3 | age=5#01 if age > 5:#02 print(“School age”) #03 print(“Lucky you”) #04 | Nothing will be displayed | Statement 03 and 04 are not executed because age>5 on statement 02 evaluates to False. Both statement 03 and 04 will be executed since Statement 02 evaluates to True |
4 | age= 6#01 if age > 5:#02 print(“School age”) #03 print(“Lucky you”) #04 print(“done”)#05 | done | The body of the if statement on line 02 has two statements, namely 03 and 04. Since the if condition is True, both are executed. Statement 05 is always executed. It is not in the body of the if statement |
5 | myAge =15#01 if 10<myAge and myAge <100: #02 myAge=myAge+10#03 print(myAge)#04 | Statement 02 evaluates to True. So myAge will be 25 which is displayed by statement 04 | |
6 | myAge =09#01 if 10<myAge and myAge <100: #02 myAge=myAge+10#03 print(myAge)#04 | Nothing will be displayed | Statement 02 evaluates to False. |
7 | myAge =09#01 if 10<myAge and myAge <100: #02 myAge=myAge+10#03 print(myAge)#04 | Statement 02 evaluates to False, so Statement 03 will not be executed. So myAge remains on 9. Statement 04 will be executed because it is outside the body of the if Statement on line 02. | |
8 | myAge =09#01 if 10<myAge or myAge <100: #02 myAge=myAge+10#03 print(myAge)#04 | Statement 02 evaluates to True this time because of the or operation. So myAge will be 19 which is displayed by statement 04 |
9 | age=5#01 if age > 5:#02 print(“School age”)#03 else: print(“Pre-school”)#04 print (“done”)#05 | ||
10 | age=6#01 if age > 5:#02 print(“School age”)#03 else: print(“Pre-school”)#04 print (“done”)#05 | School-age done | Statement 02 is True, so Statement 03 will be executed. Consequently, 04 will not be executed. . Statement 05 is not within the if- else blocks, so is executed too. |
11 | age=6#01 if age > 5:#02 age = age +5#03 print(age)#04 else:print(“Pre-school”)#05 print (“done”)#06 | Statement 02 is True, so Statement 03 & 04 will be executed. Consequently, 05 will not be executed. . Statement 06 is not within the if- else blocks, so is executed too. | |
12 | age=5#01 if age > 5:#02 print(“School age”)#03 else: age = age – 1#04 print(age)#05 print (“done”)#06 | done | The condition on statement 02 is False, so the else part is executed. That is Statement 04 and 05 will be executed. Statement 06 is not within the if-else blocks, so is executed too. |
13 | age=5#01 if age > 5:#02 print(“School age”)#03 elif age > 1: age = age + 1#04 print(age)#05 else: print(“ha ha”)#06 print (“done”)#07 | The condition on statement 02 is False, so the elif part is executed to evaluate to True. So Statement 04 and 05 will be executed. Statement 06 is not executed since the elif evaluated to True. Statement 07 is not within the if-else blocks, so is executed too. | |
14 | age=1#01 if age > 5:#02 print(“School age”)#03 elif age > 1: age = age + 1#04 print(age)#05 else: print(“ha ha”)#06 print (“done”)#07 | The condition on statement 02 is False, so the elif part is executed to evaluate to False. So Statement 04 and 05 are not executed. Statement 06 is executed since the elif evaluated to False. Statement 07 is not within the if-else blocks, so is executed too. |
Activity -1:
Write a python program that will ask the user their age and if the age is less than 18 it will print that you are a minor.
Activity -2:
Write a python program that will ask the user their age and if the age is less than 18, then if the child's age is less than 5 then no ticket price otherwise 1/2 the full price and if the user's age is greater than or equal to 18 then full price
Activity -3:
Write a python program that will ask the user the marks achieved in a subject and your program must display the grade according to the following rule:
a. 0-49 - "FAIL"
b. 50-64 - "PASS"
- 65 - 74 - "CREDIT"
- 75-84 - "DIST" and above and equal to 85 - "HD"
Activity -4:
Write a python program that accepts day, month and last two digits of the year of a date and your program must display if the date is magic or not. (A date is magic date when (day * month = last two digits of year)
For example: if the date is 10/6/60
Then 10 * 6 = 60 so the date is a magic date.
Answer
For solution, connect with online professionals.
