×

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

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Comment starts with the symbol in Python

Python programming language: 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...

3 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python Typing Module

An Introduction to the Typing Module The typing module is introduced in Python version 3.5 and used to provide hinting method types in order to support static type checkers and linters...

10 minutes read.

GUI Calendar using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

__init__ in python

Every programmer will enclose to object-oriented programming (OOP) these days. As we know, that python is the latest and most modern programming language; python supports to implementation of object-oriented philosophy....

5 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

PyShark 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....

4 minutes read.