Conditional Statement Python Python Arithmetic Operators Operator Name
















- Slides: 16

Conditional Statement Python

Python Arithmetic Operators Operator + * / % ** // Name Example Addition Subtraction Multiplication Division Modulus Exponentiation Floor division Try it x+y x-y x*y x/y x%y x ** y x // y

Python Comparison Operators (True or false) Operator == != > < >= <= Name Example Equal Not equal Greater than Less than Greater than or equal to Less than or equal to Try it x == y x != y x>y x<y x >= y x <= y

Output x = 25 y = 35 z = "25" print(x == z) print(x != y) print(x != z) print(x < y) print(x > y) print(x <= y) print(x >= y) False True False

If statement • If statement is one of the most commonly used conditional statement in most of the programming languages. It decides whether certain statements need to be executed or not. If statement checks for a given condition, if the condition is true, then the set of code present inside the if block will be executed.

Introduction to the if Statement if <expr>: <statement> • <expr> is an expression evaluated in Boolean context, as discussed in the section on Logical Operators in the Operators and Expressions in Python tutorial. • <statement> is a valid Python statement, which must be indented. • If <expr> is true (evaluates to a value that is “truthy”), then <statement> is executed. If <expr> is false, then <statement> is skipped over and not executed. • Note that the colon (: ) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not.

Grouping Statements: Indentation and Blocks If the weather is nice, then I will: • Mow the lawn • Weed the garden • Take the dog for a walk (If the weather isn’t nice, then I won’t do any of these things. ) Here is a syntactic device that groups multiple statements into one compound statement or block. A block is regarded syntactically as a single entity. When it is the target of an if statement, and <expr> is true, then all the statements in the block are executed. If <expr> is false, then none of them are.

Indentation • Indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block. • Here, all the statements at the matching indentation level (lines 2 to 5) are considered part of the same block. The entire block is executed if <expr> is true, or skipped over if <expr> is false. Either way, execution proceeds with <following_statement> (line 6) afterward.

A few important things to note about if statements: • The colon (: ) is significant and required. It separates the header of the compound statement from the body. • The line after the colon must be indented. It is standard in Python to use four spaces for indenting.

Examples 1. person = input("Nationality? ") if person == "french": print("Préférez-vous parler français? ") 2. a = 33 b = 200 if b > a: print(b, “is greater than”, a) 3. saving. Amt = 1000 withdraw. Amt = int(input("Amount to Withdraw: ")); if withdraw. Amt > saving. Amt: print ("Insufficient balance");

Assignment Q 1. You have written an exam for a total score of 100 and if your score is above 60 then you will be considered as PASS in the exam. Q 2. Enter the weight of the suitcase in float. Check whether the weight is greater than 25 kg then print - There is a Rs. 250 charge for luggage that heavy. Q 3. Enter temperature(in Celsius) in float. Check whether the temperature is more than 50 then print – It is unbearably hot weather. Q 4. Enter a (int) number. Check whether the number is even or odd. (use % for remainder and comparison Equal to operator ==

if-else condition • The if-else condition adds an additional step in the decision-making process compared to the simple if statement. The beginning of an ifelse statement operates similar to a simple if statement; however, if the condition is false, instead of printing nothing, the indented expression under else will be printed.

if-else condition • The most complex of these conditions is the if-else condition. When you run into a situation where you have several conditions, you can place as many elif conditions as necessary between the if condition and the else condition. • An arbitrary number of elif clauses can be specified. The else clause is optional. If it is present, there can be only one, and it must be specified last.

Examples x, y =8, 4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print (st)

Example • Write code to assign value ‘Joe’ to name variable. • Check whether the name variable is equal to – john, smith, fred then print- Hello name else print – I do not know you!!!! name = 'Joe' if name == 'Fred': print('Hello Fred') elif name == 'Xander': print('Hello Xander') elif name == 'Joe': print('Hello Joe') elif name == 'Arnold': print('Hello Arnold') else: print("I don't know who you are!")

Assignment Body mass index (BMI) is a value derived from the mass (weight) and height of a person. The BMI is defined as the body mass divided by the square of the body height, and is universally expressed in units of kg/m 2, resulting from mass in kilograms and height in metres. BMI=ml 2 Write a program, which asks for the length and the weight of a person and returns an evaluation string according to the following table: BMI Evaluation BMI = weight / height power 2 For calculating power we use **