UX DESIGN TRENDS 2019

UX design is a constantly evolving discipline which forces designers to take a multi-faceted approach to development. It’s no secret that the world of UX design has boomed since it’s conception. As…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Conditional Statements in Python

CONDITIONAL STATEMENTS IN PYTHON

In programming languages, most of the time in large projects we have to control the flow of execution of our program and we want to execute some set of statements only if the given condition is satisfied, and a different set of statements when it’s not satisfied.

Conditional statements begin with a hypothesis and end with a conclusion. Decision-making statements are also known as conditional statements. A password is an excellent illustration of a condition. Passwords are “if, then” logic statements: users can access the software if they enter the correct password.

Conditional statements are also known as decision-making statements. We need to use these conditional statements to execute the specific block of code if the given condition is true or false.

Indentation is a crucial aspect of Python programming, along with case sensitivity. It is worth noting that most of the keywords in the python program are in lower case.

TYPES OF CONDITONAL STATEMENTS

There are four conditional statements in Python.

1.Simple if-statement.

2.If-else statement.

3.If-elif-else statement.

4.Nested if statement.

1.Simple if-statement.

One of the most widely used conditional statements in computer languages is the if statement in Python. It determines whether or not particular assertions must be performed. It tests for a particular condition, and if it is true, the code within the ” if ” block will be run; otherwise, it will not.

The if condition evaluates a Boolean expression and executes the block of code only when the Boolean expression becomes TRUE.

SYNTAX:

IF

(STATEMENT EXPRESSION)

EXAMPLE:

num = 5

if (num < 10):

print(“Num is smaller than 10”)

print(“This statement will always be executed”)

2. If-else statement.

The statement itself indicates that if a certain condition is true, then the statements inside the “if block” should be executed, and if the condition is false, then the “else” block should be executed.

Only when the condition becomes false will the “else” block be executed. It’s the block in which you’ll take action if the condition isn’t true.

if-else statement evaluates the Boolean expression. If the condition is TRUE then, the code present in the “ if “ block will be executed otherwise the code of the “else“ block will be executed

SYNTAX:

If (EXPRESSION == TRUE):

Statement (Body of the block)

else:

Statement (Body of the block)

EXAMPLE:

num = 5

if(num > 10):

print(“number is greater than 10”)

else:

print(“number is less than 10”)

print (“This statement will always be executed” )

3.If-elif-else statement.

From the top down, the if statements are performed. The statement linked with that if is performed as soon as one of the criteria controlling the if is true, and the rest of the ladder is bypassed. The last else line will be performed if none of the criteria are true. Elif is a shortcut for else if conditionals. The elif statement in Python has one or more criteria.

SYNTAX:

if (condition):

#Set of statement to execute if condition is true

elif (condition):

#Set of statements to be executed when if condition is false and elif condition is true

else:

#Set of statement to be executed when both if and elif conditions are false

EXAMPLE:

num = 10

if (num == 0):

print(“Number is Zero”)

elif (num > 5):

print(“Number is greater than 5”)

else:

print(“Number is smaller than 5”)

4.Nested if statement.

Nested “if-else” statements mean that an “if” statement or “if-else” statement is present inside another if or if-else block.this in turn will help us to check multiple conditions in a given program.

An “if” statement is present inside another “if” statement which is present inside another “if” statements and so on.

SYNTAX

if(condition):

#Statements to execute if condition is true

if(condition):

#Statements to execute if condition is true

#end of nested if

#end of if

EXAMPLE:

num = -10

if (num != 0):

if (num > 0):

print(“Number is positive”)

else:

print(“Number is negative”)

else:

print(“Number is Zero”)

Python If Statement In One Line

We can write “if” statements, “if-else” statements and “elif” statements in one line without worrying about the indentation.

SYNTAX:

if (condition): statement 1; statement 2; statement 3;…;statement n

EXAMPLE:

num = 7

if (num > 0): print(“Number is greater than Zero”)

CONCLUSION

We learned about the relevance of conditional statements in programming in this post. The Python programming language’s conditional statements were carefully investigated. The notion of control structures was presented to you. These are compound statements that change the execution order of programme statements or the programme control flow.

Add a comment

Related posts:

How to Scrape Amazon for Book Information Using Python and BeautifulSoap?

To scrape Amazon for book information, you require to first install Beautiful Soup library. The finest way of installing BeautifulSoup is through pip, so ensure you have a pip module installed. It’s…

The Courage to Take Courage

Mum enveloped me with all the woolens she could possibly find in the closet. I was all dressed up to go, but the war in my head started to grow intense. “It isn’t always easy to ‘be bold and do what…

Deployment of iOS device with free developer account

When you wanted to run an iOS app in your device from xcode using a free personal developer account, ensure you have Xcode version > 7.0 and iOS running > 9.0. Login to the dev account in your Xcode…