×

One Liner If-Else Statements in Python

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.

Python statements

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

  1. The if statement
  2. The if else statement
  3. The if elif statement
  4. The nested if else

1) The if statement: It is used to check whether the given statement is True or not.

Syntax

If boolean expressions:
        Statements

Example

a = 20
b = 20
if a == b:
      print("yes")
else:
     print("no")

Output

yes

2) The if else statement: It is used to check whether the given statement is true or false.

Syntax

If boolean expressions1:
         Statement-A
Else:
      Statement B

Example

a = 20
b = 23
if a < b:
        print(" a is less than b")
else:
     print("a is greater than or equal to b")

Output

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.

Syntax  

If boolean expressions2:
           Statement A
Elif boolean expressions3:
           Statement B
Elif boolean expressions4:
           Statement C
Else:
     Statement D 

Example

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")

Output

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.

Syntax

If boolean expressions2:
        Statement A
Else:
If boolean expressions3:
       Statement B
Else:
     Statement C

Example

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")

Output

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")

Output

One Liner If-Else Statements in Python

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”)

Output

One Liner If-Else Statements in Python

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)

Output

One Liner If-Else Statements in Python

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.


Related Topics

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Create Table Using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's...

4 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

What is Ipython shell?

IPython is a command shell for computing in multiple programming languages. IPython was developed for python language by Fernando Perez in 2001 as a well-equipped python interpreter that offers shell...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.