Skip to main content

One Liner If-Else Statements in Python

 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 Python 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.

 

Comments

Popular posts from this blog

Explain Block Diagram of Computer and Its Components

History of Computer In ancient times, mankind is unaware of nothing. They eat what they get, grasp knowledge what they saw and do whatever they like. As generation passes habits of mankind also changed. ”Necessity is the mother of Invention”, this proverb suits here.  Little by little they civilized themselves by a proper manner. Education in older days is traditional one. They learn mathematics and writing by visual materials like stone, writing in sand and counting’s are made by different way.  Later, modernized and globalized world takes us to next level. In early century people face a tough time where they have to solve complex problem without any machine. So, they founded a machine which was technically called “COMPUTER” Generations of Computer People design computer which meets their necessity. It is divided into five types such as 1. First Generation Computer( Vacuum tube) 2. Second Generation Computer(Transistor) 3. Third Generation Computer(Integrated Circuit) 4. Fourt

Learning Objectives of Digital Marketing Course in 2022

Introduction to Digital Marketing Course  The digital certificate of marketing   training is a way of instructing students on the basics and nuances of marketing online. At Javatpoint have created quite an image for ourselves because we provide quality-assured classes in our  Digital Marketing Courses that have the placement   that follows a meticulously researched and thorough course outline. The popularity and reach of digital marketing has been at their highest in courtiers such as the UK as well as countries like the US, Italy and in important European countries.   Asian nations like China and Japan have also followed this trend to try to establish a position for their own in the world of digital. Over the last 10 years, Indian businesses from all kinds of industries have realized they will require an online presence in order to be successful over the long haul. So what does all this mean to you? In a nutshell you could make a decent amount of money and enjoy an impressive job when

Different between Traditional Marketing and Digital Marketing

  Different between Traditional Marketing and Digital Marketing This tutorial will cover traditional marketing, digital marketing and the what are the various similarities and differences between them. What is marketing? Everyone knows everything through Advertisement. Advertisement is technically called Marketing. Corporate and other developing sectors design and launch their product through marketing strategies. Marketing is used to make the product reach people and make it familiar. Marketing is the process of launching a new product or service on the computer. It follows some strategies and techniques to make the product reach people. It promotes the product to the people to meet the business target or requirement. Digital Marketing As the name itself indicates that launching or introducing the product digitally, like through social media, email, website, and search engines. If people are experts in digital marketing , it is easy to launch any product. Traditional Marke