Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from web improvement and numerical processing to work area graphical UIs. It is an interpreted language which means it checks codes line by line and provides output.
The syntax of Python language is clean, and the length of code relatively shorter than any other programming language. It permits us to think about the problem rather than focusing on syntax only. It has a wide range of applications such as web applications, desktop GUI applications, scientific and numeric calculations, image processing applications, etc. It is an open-source language and is easy to understand. It is free and portable.
Statements are instructions which python interpreters will execute. There are many types of Python statements, and one of them is conditional statements. Let us learn why conditional statements. A conditional statement is utilized to decide if a specific condition exists or not before the code is executed. Conditional statements are otherwise called dynamic statements. It performs various calculations or activities relying upon whether the given condition is true or false.
The types of conditional statements are as follow
- The if statement
- The if else statement
- The if elif statement
- The nested if else
1) The if statement: It is used to check whether the given statement is True or not.
If boolean expressions: Statements
a = 20 b = 20 if a == b: print("yes") else: print("no")
2) The if else statement: It is used to check whether the given statement is true or false.
If boolean expressions1: Statement-A Else: Statement B
a = 20 b = 23 if a < b: print(" a is less than b") else: print("a is greater than or equal to b")
a is less than b
3) The if elif statements: The if elif statements allow you to check multiple expressions for True or False and execute a block of code as soon as one of the conditions, evaluates to True.
If boolean expressions2: Statement A Elif boolean expressions3: Statement B Elif boolean expressions4: Statement C Else: Statement D
a = int(input("Enter an integer")) if a < 0: print(" a is negative") elif a==0: print(" a is null") else: print("a is positive")
Enter an integer 34 a is less than b
4) The nested if else statement: When we write an if else statement in another if else statement, then that statement is known as a nested if else statement.
If boolean expressions2: Statement A Else: If boolean expressions3: Statement B Else: Statement C
num = 21 if num < 25: if num%2==0: print("num is even number less than 25") else: print("num is an odd number less than 25") else: print("num is greter than 25")
num is an odd number less than 25
Boolean expression: It is a Python inbuilt data type used to represent an expression's True value.
Note: The colon is vast and required. It isolates the header of the compound statement from the body. The line after the colon should be indented.
One liner if else Python
It is a type of if else statement in which we write if else statements in one line. It means that if statements and other statements are in the same line. It is otherwise called the ternary administrator. The ternary operator is available in some programming languages, which allows us to shorten the If else block. The one-line statement syntax is
<instruction A> If (conditions) else <instruction B> And If (conditions A): Statements Else: Statements
Let us understand by an example.
val = int(input("Enter an integer")) if val > 45: print(" Value entered by the user is greater than 45") else: print("Value is less than or equal to 45")
We have implemented both if and else in the above example. We have taken inputs from the user; if the user types a number greater than 45, it will give output as the value is more significant than val, and the “if” statement will execute. If the user type number is less than 45, then the “else” statement will run and give output as the value entered by the user is less than 45. Also, the else statement will execute when the user types a number equal to 45 and will give an output value equal to 45. So this is how we can use a one-liner if else statement.
Let us take more examples to understand it better.
val = int (input (“Enter an integer”)) If val < 90: print (“Entered value is less than 90”) Else: print (“Entered value is greater than 90”)
In this example, the input () command has been used to get input from the user. If the data is under 90, the “if” statement will execute, and if the value is more than 90, then the “else” statement will execute. Its value relies on the input given by the user.
Note: - The execution of a one-liner if-else statement starts from left to right.
Let us take one other example
def oddeven(a): if a % 2 == 0: print(a, " even number ") else: print(a, "odd number ") oddeven(27)
In the above example, we have executed a simple function to understand it in a better way. If the number is not distinct by 2, it will return an odd number and the “if” statement will execute. If the number is distinct by 2, it will return an even number, and the else statement will execute. We have passed 27, a number which will be returned as an odd number.