0% found this document useful (0 votes)
13 views

Python Chapter 4.1

Here are 3 programs using if statements to check conditions and produce different outputs based on those conditions: [IF1] age = int(input("Enter your age: ")) if age < 14: print("Too young") else: print("Eligible") [IF2] name = input("Enter your name: ") year = int(input("Enter the year you were born: ")) if year > 1999: print(f"Welcome {name}, you are a 21st Century Child!") else: print(f"Welcome {name}, you are a 20th Century child!") [IF3] password = input("Enter a password: ")

Uploaded by

shahida
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Chapter 4.1

Here are 3 programs using if statements to check conditions and produce different outputs based on those conditions: [IF1] age = int(input("Enter your age: ")) if age < 14: print("Too young") else: print("Eligible") [IF2] name = input("Enter your name: ") year = int(input("Enter the year you were born: ")) if year > 1999: print(f"Welcome {name}, you are a 21st Century Child!") else: print(f"Welcome {name}, you are a 20th Century child!") [IF3] password = input("Enter a password: ")

Uploaded by

shahida
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Simple if

▪ Review conditional expression using relational operator and logical operator


▪ Explain type of control structure and their application
▪ Explain type of decision structure in Python
▪ Simple/single if
▪ Double if
▪ Muliple if
▪ Nested if

▪ Apply conditional expression in writing decision structure


▪ Expressions written using Relational Operators and/or logical operators
▪ The result of the expression is either true or false
▪ Convert this statement into Python codes. Given value X=5, Y=10, Z=80
a. X is greater than 2
b. Y is not 0
c. Z is between 20 and 100
d. X is strictly between 1 and 6 and Z is 50
f. Y is greater than 65 inclusively
g. Z is 80 and X is less than 65 inclusively
h. Y is between 5 and 20 inclusively but not 15
▪ Sequential
▪ Statements are executed one after another. No statements are skipped or repeated

▪ Selection/Decision
▪ Statements are executed conditionally. Executed when the condition is true and skipped
when the condition is false
▪ Looping/Iteration/Repetition
▪ Statements are repeated conditionally
S
E
…consecutive steps (or
Q
groups of steps) processed
U one after another in the order
that they arise.
A bit like reading through a cooking
E recipe, one line at a time.

N
C E
Tru
e

False
Tru
e

False
Tru
e
False
▪ Explain simple if structure and how it works
▪ Write simple if statement
▪ Evaluate simple if statement
False
if <condition>: <condition
>
Statement1
Statement2
:
Execute if the condition is
true True
StatementN

Uncondition Statement 1
:
Either <condition> True of
Statement False, execute this statement StatementN

Uncondition Statement
▪ Python codes

1)if Gender is 1 then increase Male by 1 Statement statement

<condition>

2)if Age is 65 and above then print “too


old”. In either case, print value of Age
True

3)if Gender is 1 and Age is 65 and above


then add 1 to variable Count, set Found to
value True. In either case read another
Gender and Age
>>> if x==5:
print("Hurray!")

>>>if x==3: ▪ Assume x=5,y=3,z=8. Write no output produce if no


print statement executed
print("Bye")

>>>if x+y==z:
print(“{}+{}={}”.format(x,y,
z))

>>>if not(x%2==0):
print(x,” is even”)

>>>if y%2==0:
print(y,” is even”)
Task 1
1) Create a program that prompts the user for their age
• If their age is less than 14 it should display message
“Too Young”
• If their age is greater than or equal to 14, it should produce a
A message “Eligible”
Create your program in SCRIPT mode and save it as IF1
Task 2
1) Create a program that prompts the user for their NAME and the YEAR
they were born.
• If their year is after 1999, the program should output
‘Welcome NAME, you are a 21st Century Child!’
• If their year is not after 1999, the program should output
‘Welcome NAME, you are a 20th Century child!’
Create your program in SCRIPT mode and save it as IF2
Task 3
1) Create a program that prompts the user for a PASSWORD
• If the entered password is less than 8 characters long, it should output
‘Password Rejected.’
• If the entered password is at least 8 characters long, it should output
‘Password Accepted.’
Create your program in SCRIPT mode and save it as IF3

You might also like