alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Decision control and looping statement - if-else Tutorial

Python if…else is a decision-making statement that is used to check whether the condition is True Or False.

If the first condition in if block is false it will check the next condition in elif block. If the elif block condition is also false then it will finally execute else block.

  • If statement

It executes if block only when if condition is true.

 

a = 2
b = 7
if a > b:
  print("a is greater than b")
if b > a:
  print("b is greater than a")

 

Run Program

Output-

b is greater than a

 

In shorthand, it can also be written as

a = 9
b = 7
if a > b: print("a is greater than b")

Run Program

 Output-

a is greater than b

 

  • If-else statement

It checks first for if condition, if the condition is true, it will execute if block, otherwise it will execute else block.

a = 2
b = 7
if b > a:
  print("b is greater than a")
else:
  print("a is greater than b")

Run Program

Output-

b is greater than a

 

In shorthand, it can also be written as

a = 9
b = 7
print("a is greater than b") if(a>b) else print("b is greater than a")

Run Program

 Output-

a is greater than b

It is also known as the ternary operator.

  • If-elif-else ladder

It checks first for if condition, if the condition is true, it will execute if block, otherwise it will go to next condition i.e elif.

If the condition went false, it will again move toward the next elif condition, still, the condition becomes true.

At last, if no condition is satisfied, finally it will execute else block.

a = 9
b = 7
if b > a:
  print("b is greater than a")
elif a == b:
  print("a is equal to b")
else:
  print("a is greater than b")

 

Run Program

Output-

a is greater than b

 

  • Nested If else

One if-else condition nested under another if-else condition is known as nested if else. If the outer condition is true, then it will execute the inner condition only when it become true.

a = 9
b = 7
c = 3
if(a>b):
    if(a>c):
        print("a is greater")
    else:
        print("c is greater")
else:
    if(b>c):
        print("b is greater")
    else:
        print("c is greater")

Run Program

 Output-

a is greater

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.