Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 4 Selection Statement ( if ) Eng. Ibraheem Lubbad October 10, 2016
In this lab we will constructs program that allow statements to be optionally executed, depending on the selection execution. Boolean Data Type and Operations: The python values True and False are special inbuilt objects which is Boolean type. You can give variable the value directly or via Boolean expressions. Boolean expression: an expression that computes to True or false. It may contain relational (Comparison) operators and logical operators, the simplest Boolean expressions in Python are True and False. Comparison Operators: Operator Description == Equal to!= Not equal to <> Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Logical Operators: We combined more complex Boolean expressions using the logical operators and, or, and not. Operator and or not Description If both the operands are true then condition becomes true If any of the two operands are non-zero then condition becomes true Used to reverse the logical state Logical operators e1 and e2 are Boolean expressions e1 e2 e1 and e2 e1 or e2 not e1 True True True True False True False False True False False True False True True False False False False True Membership Operators: Python s membership operators test for membership in a sequence, such as strings, lists, Tuples or dictionary. For example list= [2, 6, 9, 7] Operator Description Example in Check element belong to sequence 5 in list------ False not in Check element not belong to sequence 5 not in list---true
Decision structures evaluate multiple expressions which produce True or False as outcome. You need to determine which action to take and which statements to execute if outcome is true or false. Following is the general form of a typical decision making structure: If Statement: if Boolean Expression: statements Syntax of if statement Condition False True Statements Example 1 a = 24 if a > 0: print "a is positive "
If else Statement: if Boolean Expression: statements(1) statements(2) Syntax of if / else statement Condition False True Statements(1) Statements(2) Example 2 a = 24 if a > 0: print "a is positive " print "a is non - positive"
The elif Statement: The elif statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True. if expression1 : Syntax of elif statement statements(1) elif expression2: statements(2) statements(3) Example 3: a = 24 if a == 0: print "a is zero " elif a < 0: print "a is negative " print "a is positive " If a=24 a=0 a=-3
Single Statement: If the suite of an if statement consists only of a single line, it may go on the same line. Here is an example of a one-line if statement: Example 4: Single statement var = 100 if var == 100 : print "Value of expression is 100" print "Done!" Nested Conditionals: The statements in the block of the if or the else may be any Python statements, including other if/else statements. Determines if a number is between 0 and 10, inclusive value = input("please enter an integer value in the range 0...10:") if value >= 0: # First check if value <= 10: # Second check print "In range" print "Out of range" print("done") The program can be rewritten to behave the same way with only one if statement: Another way value = input("please enter an integer value in the range 0...10:") if value >= 0 and value <= 10: # Only one print("in range") else: print "Out of range" print("done")
Conditional Expressions: Python provides an alternative to the if/else construct called a conditional expression. A conditional expression evaluates to one of two values depending on a Boolean condition The general form of the conditional expression is: expression1 if condition else expression2 For example here variable c is assigned one of two possible values: if a!= b: c = d c = e If/else example Rewritten as c = d if a!= b else e The form of the conditional expression For Example: write a program to compute the absolute value of entered number from user. Example 5: n = input ("Enter a number: ") absvalue=(-n if n < 0 else n) print ' ', n, ' = ', absvalue Output:
Lab work: Example 6: check number is even or odd n = input ("Enter a number: ") if n%2==0 : print "Even number" print "Odd number" print "Done" Output: Example 7: Check entered number in the range value = input("please enter an integer in the range 0...5: ") if value < 0: print "Too small" elif value == 0: print "zero" elif value == 1: print "one" elif value == 2: print "two" elif value == 3: print "three" elif value == 4: print "four" elif value == 5: print "five" else: print "Too large" print "Done" Output:
Q) Write a program that requests from user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. For example, here are some sample runs for inputs 10, 30, and 23: 10 is divisible by 5 or 6, but not both 30 is divisible by both 5 and 6 23 is not divisible by either 5 or 6 Sol: Solution num=input("enter number : ") if num%5==0 and num%6==0: print num,"is divisible by both 5 and 6" elif num%5==0 or num%6==0: print num, "is divisible by 5 or 6, but not both " else: print num,"is not divisible by either 5 or 6" Output:
Exercises: 1) Write a Python program that requests three integer values from the user. It then prints one of two things: if any of the values entered are duplicates, it prints "DUPLICATES"; otherwise, it prints "ALL UNIQUE". 2) Write a Python program that use an if statement to find the smallest of three given integers without using the min function 3) Write a Python program that sorts three integers. The integers are entered from user using input function. For example if the user enters: 7, 3, 5 then the output should be 3<=5 <=7. 6, 2, 6 then the output should be 2<=6 <=6