• Home
  • Ubuntu 16.04
    • Whats New ?
    • Install JAVA 8
    • Setup LAMP Stack
    • Install LEMP Stack
    • Install Node.js
    • Install Git
    • Move Unity Launcher to Bottom
  • FeedBack
  • Funny Tools
  • Tutorials
    • Linux Distributions
      • CentOS
      • Debian
      • Fedora
      • LinuxMint
    • Monitoring Tools
      • Monit
      • Nagios
      • NRPE
    • Network Services
      • DHCP
      • DNS
      • FTP
    • Databases
      • MySQL
      • MariaDB
      • MongoDB
      • PostgreSQL
      • SQL Server
    • Amazon Web Services
  • Submit Article
  • About Us
TecAdmin.net
  • Home
  • Ubuntu 16.04
    • Whats New ?
    • Install JAVA 8
    • Setup LAMP Stack
    • Install LEMP Stack
    • Install Node.js
    • Install Git
    • Move Unity Launcher to Bottom
  • FeedBack
  • Funny Tools
  • Tutorials
    • Linux Distributions
      • CentOS
      • Debian
      • Fedora
      • LinuxMint
    • Monitoring Tools
      • Monit
      • Nagios
      • NRPE
    • Network Services
      • DHCP
      • DNS
      • FTP
    • Databases
      • MySQL
      • MariaDB
      • MongoDB
      • PostgreSQL
      • SQL Server
    • Amazon Web Services
  • Submit Article
  • About Us
15 July 2017

Working with Python IF, ELSE and ELIF Statements

Written by Rahul K. | July 15, 2017
Python else, if, python

IF, ELSE or ELIF (known as else if in some programming) are conditional statements which are used for execution of different code depends on condition. The if statements can be written without else or elif statements, But else and elif can’t be used without else. In this tutorial, you will learn if, else and elif in Python programming language.

1. Python if Statement

The single if statement is used to execute the specific block of code if the condition evaluates to true. In the case of false output, nothing will execute.

Syntax:

1
2
if ( condition ):
    statements

Example 1:

1
2
3
4
5
#!/usr/bin/python
 
var = 101
if ( var ):
    print "true"

Example 2:

1
2
3
4
5
#!/usr/bin/python
 
var = 101
if ( var==101 ):
    print "true"

2. Python if-else Statement

The if and else statement is used to execute the specific block of code for true condition and another block of code on false condition.

Syntax:

1
2
3
4
if ( condition ):
    statements
else:
    statements

Example: Assinged a value to var variable, Now test if the assigned value it greated than 100. As per below code the result will be “Assigned value is greater than 100”.

1
2
3
4
5
6
7
8
#!/usr/bin/python
 
var = 101
 
if ( var > 100 ):
    print "Assigned value is greater than 100"
else:
    print "Assigned value is less than or equals to 100"

3. Python if-elif Statement

The if and elif (known as else-if) statement is used to execute the specific block of codes with multiple conditions. In this, if the main if the condition goes false then another elif condition is checked. You can define a number of elif conditions as per your requirements.

Syntax:

1
2
3
4
5
6
if ( condition ):
    statements
elif ( condition ):
    statements
else:
    statements

Example: Taken a avalue as input in total variable. Now compare the value with multiple levels and print the appropriate output.

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
 
total = 90
 
if ( total > 500 ):
    print "total is greater than 500"
elif ( total > 100 ):
    print "total is greater than 100"
elif ( total > 50 ):
    print "total is greater than 50"
else:
    print "total is less than or equal to 50"

4. Python Nested if Statement

The nested if statements is use as if inside if. In this if any if condition evalutes to true then it goes to inner if condition.

Syntax:

1
2
3
4
5
if ( condition ):
    if ( condition ):
        statements
    else:
        statements

Example: taken 3 numeric inputs and find the greatest value. Like if var1 is greater than var2 then it check if var1 is also greater than var3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
 
var1 = 100
var2 = 350
var3 = 80
 
if ( var1 > var2 ):
    if ( var1 > var3 ):
        print "var1 is greatest"
    else
        print "var3 is greatest"
elif ( var2 > var3 ):
    print "var2 is greatest"
else:
    print "var3 is greatest"

Share it!
Share on Facebook
Share on Twitter
Share on Google+
Share on Reddit
Share on Tumblr
Rahul K.
Rahul K.
Connect on Facebook Connect on Twitter Connect on Google+

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Related Posts

  • How To Install PIP in Linux

  • How to Install PyCharm IDE on Ubuntu 16.04 and 14.04

  • How to Get Current Date & Time in Python

  • How to Install Python 3.6.4 on CentOS/RHEL 7/6 & Fedora 27/26/25

  • How to Install Python 3.6 on Ubuntu & LinuxMint

Leave a Reply

Cancel reply

Popular Posts

  • How to Install JAVA 8 on Ubuntu 16.04/14.04, LinuxMint 18/17
  • How to Install s3cmd in Linux and Manage Amazon s3 Buckets
  • How to Install AnyDesk on Ubuntu, Debian and LinuxMint (Alternative of TeamViewer)
  • How to Setup Selenium with ChromeDriver on Ubuntu 16.04
  • How to Install MySQL on macOS Sierra and High Sierra
All rights reserved. © 2013-2018 TecAdmin.net. This site uses cookies. By using this website you agree our term and services