We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

If Else In Python With Examples

If Else In Python With Examples

Introduction

Python is one of the most helpful programming coding languages across the world. It was first designed in the year 1991 by Guido van Rossum. Also, it was later developed by Python Software Foundation. Python is both a simple and powerful language. The greatest strength of Python is the coding language, which is extremely easy to read and can be codified in fewer lines. Python can be used to write function languages and bridges the gap between the developers and the business. It is cost-effective to bring a Python programming language to market as compared to Java or C++. There are numerous books and communities available to support the language.

Features of Python

Python is extremely easy to use language and quite popular. It can be easily codified and is available as an open-source language. Some of the basic features of Python are followed:

Easy to Operate

Python is a straightforward and minimal programming language. The pseudo-code of Python is easy to interpret and allows for higher concentration in solving critical issues in the programming language.

Learning is Breeze

Python is an accommodating language and quite easy to learn. The syntax of Python is more straightforward, and it is simple to get started with the basics of this programming language.

Open Source Platform

Python operates on a free and open-source software platform. So, it is easy to distribute copies and scan the source codes of this language. This makes it easier to make the necessary changes to the software. Python language is continuously going through upgrades, and so it makes it comfortable for the user to see the newer version regularly.

High-End Language

Python is a well rounded developed program, and so when you write programs in the language, you can avoid low-level details like managing your memory and writing codes, among others.

Easy to Carry Across platforms

Python is easily portable across all platforms due to its open-source nature. You can easily use Python to work on Macintosh, Solaris, OS/2, AROS, BeOS, Palm OS, VMS, Psion, Windows CE, PlayStation, and Pocket PCI, among others.

Easy to Interpret

All the coding languages need binary conversion because that’s the language your computer understands. When you run your program, the loader software copies the program and then starts running it.

Conditional Statements in Python

This performs various computations and depends on specific Boolean constraint performs true or false operations. The conditional statements in Python are accomplished through the use of IF comments.

Indentation in Python

Python doesn’t allow block-level codes, and indentation is used to declare a block. The indention in Python is used to achieve simplicity in programming structures. Indentation is the most used part of Python’s statement, and when the conditions are actual, it executes a condition known as if block.

What are IF ELSE statements in Python?

The IF & ELSE statement in Python is used for making decisions and evaluate test expressions and executes statements only if this expression is true. If the expression turns false, then the appearance won’t be completed — non-zero values as True. None and 0 are treated as invalid.

Let’s take a view at the various kinds of conditional statements:

StatementDescription
If statementThe if the state is used to test expression a specific condition and if the conditions are actual if block code is executed
If Else StatementIt is similar to if statement except for the fact that it provides the block code for the false case of the condition to be checked
Nested If statementThis statement also allows using this function inside an outer if statement

Simple IF Statement

The simplest example of a conditional statement is:

If (condition):

Indented Statement

The lines will be executed whenever the state is True.

If you are assigning marks to a student say for example out of 100, and 80 marks are kept for theory and 20 for the practical, the above statements would look like these:

Score_theory = 70
Score_practical = 15
If (score_theory + score_practical > 100):
Print (“Invalid score. Please check the input.”)
The colon separates the colon from the statements after evaluating the conditions.
Python IF…ELSE statement
Syntax of if…else
If test expression:
Body of if
Else:
Body of else
Example of if…else
# Program checks if the number is +ve or -ve
# and displays an appropriate message
Num = 3
# Try these two variations as well
# num = 5
# num = 0
If num >=0:
Print (“Positive or Zero”)
Else:
Print (“Negative number”)

In the above example, when the number is equal to 3, the expression holds, and if the statement is executed and else is skipped.

If the number is equal to -5, the expression is false and else is executed, and if it is skipped.

If the number or num is equal to 0, the expression is valid and if it is executed, leaving out else.

IF…ELIF…ELSE statements

# In this program
#we check if the number is positive or
#negative or zero and
#display an appropriate message
Num = 5.5
#Try these two variations as well;
#num=0
#num= 6.5
If num >0:
Print( “positive number”)
Elif num==0:
Print (“zero”)
Else:
Print (“Negative number”)

When the variable number is positive you will get a positive number.

If the number is equal to 0, the number zero is printed.

If the number is negative, you will get a negative number.

Nested IF statements

You can create an IF…ELIF…ELSE statement inside another one, and it is called nesting in computer programming. Other comments can be nested inside one another. You can use indentation to figure out the level of nesting.

Python Nested IF Example

# In this program, we can input a number
# check if the number is positive or
# negative or zero and display
# an appropriate message
# This time we use nested if
Num = float(input(“Enter a number:”))
If num >=0:
If num ==0:
Print (“zero”)
Else:
Print (“positive number”)
Else:
Print (“negative number”)

Output 1

Enter a number: 5
Positive number

Output 2

Enter a number: -1
Negative number

Output 3

Enter a number: 0
Zero

Conditional statements in Python are used to test various conditions and to arrive at various possibilities. You have worked with examples in this tutorial and learned about the numerous variations. This course will surely help you to proceed further in your studies.

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring

 

Besant Technologies WhatsApp