Python Unit III: Conditional Execution and Boolean Expressions

python n.w
1 / 12
Embed
Share

Dive into the world of conditional execution and Boolean expressions in Python, including simple if statements, if/else statements, compound boolean expressions, nested conditionals, multi-way decision statements, and errors in conditional statements. Learn about boolean expressions and how to compare numeric expressions for equality or inequality. Explore examples of using if statements, if/else statements, and the elif statement to control the flow of your Python code.

  • Python
  • Conditional Execution
  • Boolean Expressions
  • If Statements
  • Elif Statement

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. PYTHON UNIT III

  2. Conditional Execution Boolean Expressions The Simple if Statement The if/else Statement Compound Boolean Expressions Nested Conditionals Multi-way Decision Statements Conditional Expressions Errors in Conditional Statements

  3. Boolean Expressions An expression comparing numeric expressions for equality or inequality is also a Boolean expression. These comparisons are done using relational operators Expression x == y equality, x < y x <= y x > y x <= y x != y Meaning True if x = y (mathematical not assignment); otherwise, false True if x < y; otherwise, false True if x y; otherwise, false True if x > y; otherwise, false True if x y; otherwise, false True if x 6= y; otherwise, false

  4. Simple if Statement The general form of the if statement is: if condition : statement The reserved word if begins a if statement. The condition is a Boolean expression that determines whether or not the body will be executed. A colon (:) must follow the condition. The statements to be executed if the condition is true.

  5. Example var = input('Enter a value') if ( var == 100 ) : print('Value of expression is 100') print ("Good bye!")

  6. if..else.. Statement An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE. Syntax if expression: statement(s) else: statement(s)

  7. Example var = input('Enter a value (0 or 1):') if (var == 1): print("It is positive value") print(var) else : print("It is a negative value") print(var)

  8. 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. syntax if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s)

  9. var =eval(input('Enter a value :')) if(var>0) : print("Positive number") elif(var == 0) : print("Zero") else: print("Negative number")

  10. Nested Condition The statements in the block of the if or the else may be any statements, including other if/else statements. Example value = eval(input("Please enter an integer value in the range 0...10: ") if value >= 0: if value <= 10: print("In range") print("Done")

  11. Multi-way Decision Statements A simple if/else statement can select from between two execution paths

Related


More Related Content