×

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

Working with files 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 an...

5 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Sort a dataframe based on a column in Python

Sorting the dataframe based on a column requires pandas which is An open-source library called Python Pandas is described as offering high-performance data processing in Python. For both professionals and...

4 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.