Python Statements: A statement is a command that the programmers give to the compiler. There is a different type of statements in python. They are as follows: Python If Else, Elif, Else, Short Hand If, Short Hand If Else, And, Or, Break Statement, Continue statement.
- 1 Python Statements
Python If else Statement
In python statements, If Statement is used for decision making. It will run the body of code only when IF statement is true. When you want to justify one condition while the other condition is not true, then you use “if statement”
if expression: statement
num = 4 if num >= 0: print("Positive or Zero") else: print ("Negative number")
Output: Positive or Zero
Python Elif Statement
It is used in python and used if the previous conditions were not true, then try this condition.
if expression1: statement
a = 55 b = 55 if b > a: print("b is greater than a") elif a == b: print("a and b are equal")
Output: a and b are equal
Python Else Statement
Python else statement is an optional statement that is normally used in an “if-else” or “if-else” construction.
a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print ("a is greater than b")
Output: a is greater than b
Python Short Hand If Statement
If you are having only one statement to execute, you can put it on the same line as the if statement.
if a > b:print("a is greater than b")
Output: “a is greater than b”
Python Short Hand If …Else Statement
If you are having only one statement to execute, one for if, and one for else, you can put it all on the same line:
print("A") if a > b else print("B")
If you want, we can have multiple else statements on the same line.
print("A") if a > b else print ("=") if a == b else print("B")
Python And Statement
It is a logical operator and is used to combine conditional statements.
if a > b and c > a: print("Both conditions are True")
Output: Both conditions are true.
Python Or Statement
It is used for combining conditional statements.
if a > b or a > c: print ("At least one of the conditions is True")
Python Break Statement
You can stop the loop, even if while the condition is true. The most common use for the break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.
i = 1 while i < 6: print(i) if i == 3: breaki += 1
Python Continue statement
It returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
#!/usr/bin/python for letter in 'Python':#First Example if letter == 'h': continue print 'Current Letter :', letter var = 10 #Second Example while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var print "Good bye!"
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0