×

Python if statement

Python if statement

Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as:

  • If statement
  • If-else statement
  • elif statement

if statement

The if statement is the easiest conditional statement. It is used to test a particular condition. If the condition is true, a block of code (if statement block) will be executed.

Syntax:

if expression:  
    statement 

Flow chart of if statement: The flowchart of if statement is following

Flow chart of if statement

In the above flowchart, First it checks the condition if the condition is true, then conditional code(if statement) will be executed. If the condition is false, then first set of code after the end of the 'if' statement will be executed

Python programming language supposes any non-zero and non-null values as TRUE. If it is zero, then it is assumed as FALSE values.

Indentation in Python

Indentation is used to declare the block of code. All the statements of one block are intended at the same level indentation. Python doesn't allow the use of curly braces or parentheses for block-level code. Indentation makes programming simple and easy in Python.

Example: There are following the example of if statement

1. Write a program to check number is even

n = int(input("Enter the number:"))

if(n%2==0):

    print("The number is even")

  print("The number is odd")

Output:

Enter the number: 10
The number is even

2. Write a program to check the weight of the passenger’s luggage

weight = float(input("How many Kilograms(kg) does your luggage?: ")) 
if (weight <30): 
    print("You have charged ?300 for heavy luggage .") 
print("Thank you for your business.") 

Output:

How many Kilograms does your suitcase weigh?: 35
There is a 250 charge for heavy luggage.
Thank you for your business 

3. Write a program to print the largest number of the three numbers

a = int(input("Enter the First number:"))
b = int(input("Enter the Second number:"))
c = int(input("Enter the Third number:"))
 if(a>b) and (a>c):

    print(a, 'is largest number' )

if(b>a) and (b>c):

    print(b, 'is largest number')

if (c>a) and (c>b):  

    print(c, "c is largest"); 

Output:

Enter the First number: 100
Enter the Second number: 145
Enter the Third number: 200
200 is largest 

4. Write a program to check the given number is positive or negative

num = 3

if num >0:

    print(num, "is a positive number.")

    print("This is always printed.")

num = -1

if num >0:

    print(num, "is a positive number.")

    print("This is also always printed.")

Output:

3 is a positive number.
This is always printed.
This is also always printed. 

Related Topics

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

Isodate Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is...

6 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Google Chrome API in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.