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

Python Chapter 4.2

The document describes how to use if/else conditional statements in Python by providing examples of code snippets that: 1) Take user input, evaluate if/else conditions based on the input, and print corresponding output messages. 2) Compare values, assign new values based on conditional logic, and print the results. 3) Calculate costs based on input values and conditional pricing structures, and print the calculated costs. It explains the basic syntax of if/else statements in Python, shows how to write conditional logic to evaluate tests and compute outputs, and provides step-by-step examples to demonstrate solving problems using if/else statements.

Uploaded by

shahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Python Chapter 4.2

The document describes how to use if/else conditional statements in Python by providing examples of code snippets that: 1) Take user input, evaluate if/else conditions based on the input, and print corresponding output messages. 2) Compare values, assign new values based on conditional logic, and print the results. 3) Calculate costs based on input values and conditional pricing structures, and print the calculated costs. It explains the basic syntax of if/else statements in Python, shows how to write conditional logic to evaluate tests and compute outputs, and provides step-by-step examples to demonstrate solving problems using if/else statements.

Uploaded by

shahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

▪ Describe how if else work

▪ Compare if else and if statement


▪ Write if else statement in python
▪ Evaluate if else codes for output
▪ Solve computing problem using if else
▪ Statements are executed conditionally. A block of statements is executed when the
condition is true and while another block of statements is executed when the condition
is false
if <condition>:
statement1 ▪ Indent the statements to be executed

else:
Statement 2
a=4
b=4
if a<b:
print("boo !!")
print("end of conditional code")
print("end of program")
a=input("enter a word")
b="z"
c=a[0]
if c==b:
print("the word begins with ",b)
else:
print("the word does not begin with ",b)
Example 1 Example 2

x=input("type a number") x=input("type a number")

if x==10: if x==10:
print("x is 10")
print("x is 10")
print("this is ok too")
else:
else:
print("x is not 10")
print("x is not 10")

Try input value 5 and 10


▪ Requests the value of Costs and Revenue for a company and displays the message “Break even “ if the Costs and
Revenue are equal or otherwise displays the difference.

▪ Read 3 tests. Compute the Average of three tests. If the Average is 60 and above display a passing message, otherwise
display the failing message.

▪ Reads a package ID and its Weight, print the package ID followed by “CLASS1” if the package weighs less than 32
ounces and “CLASS 2” if otherwise.
if (length <1):
cost=0.46
else:
cost=0.46+(length-1)*0.3
print("The cost is ",cost)

** if length is 3
Assume input is 20, 15

a, b= map(int, input("Enter the Numbers : ").split())


if (a>b):
a=a+1
else:
b=b+1
print(a,"\n",b)
▪ Request a number
▪ If the number entered is less than 0, display “zero or negative value”
▪ Otherwise, display “a positive number”
▪ Request a total sale
▪ Determine whether a salesperson is eligible for a bonus a not. Given that, a bonus
30% of sale is given to any total sale above 2000
▪ Read hour. If the hour is less than 18, create a "Good day" greeting, otherwise
"Good evening":
▪ Read two numbers. Display the maximum between two numbers
▪ Write a Python codes that prompt the user to input tree integer values and find the
greatest value of the three values.
▪ Write a program to input any alphabet and check whether it is vowel or consonant.

You might also like