HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Python / Python Flow Control / Python if…else

Python if…else

Learn to control the flow of a Python program using if, if...else and if...elseif statements. This process is also called decision making.

A if statement is executed only if a certain condition is True. The the condition is False, then else statement is executed, if provided in the program.

1. Python if...else Statement

Python if…else statement is written with if keyword and else keyword.

1.1. Example

In this Python program, we are comparing the values of x to y using the conditional expression 'x < y'.

x = 100
y = 200

if x < y:
	print('x is less than y')
else:
	print('x is greater than y')

1.2. Condition Expressions

We can write the condition expressions in following ways:

  • x is ‘equals to’ y: x == y
  • x is ‘not equals to’ y: x != y
  • x is ‘less than’ y: x < y
  • x is ‘less than or equal to’ y: x <= y
  • x is ‘greater than’ y: x > y
  • x is ‘greater than or equal to’ y: x >= y

Also note that Python evaluates non-zero values as True. None and 0 are interpreted as False.

1.3. Combining Expressions

To combine the two expressions, Python provides to logical operators:

  • and : Logical AND
  • or : Logical OR
x = 100
y = 200
z = 300

if x < y and y < z:
	print('y lies between x and z')
else:
	print('y lies outside x and z')

2. Elif Statements

The elif statement is short form of else if statements in other programming languages such as Java. A Elif statement contain a conditional expression which will be executed if it’s previous conditional expression is evaluated to False.

In this Python example, x is greater than y, so the if statement will be False. So, the Python will execute the elif statement which is True.

x = 400
y = 200
z = 300

if x < y:
	print('x is less than y')
elif y < z:
	print('y is less than z')

Program output.

y is less than z

3. Indentation

Python uses indentation to define the scope in the code. (Similar to curly brackets in Java). So be mindful of indentation while writing if else statements in Python.

x = 400
y = 200
z = 300

if x < y:
	print('x is less than y')
elif y < z:
	print('y is less than z')
else:
	print('Something else')

4. Nested if…else

The if...else statement written inside another if...else statement is called

nested if...else

statement.

Be very carefull about indentation while writing nested if statements.

x = 100
y = 200
z = 300

if x < y:
	if x < z:
		print('x is less than y and z')
else:
	print('Something else')

5. Empty if Statement

A Python program cannot have an empty if statement. If for any reason, you must provide an empty if statement then use the pass keyword.

x = 100
y = 200

if x < y:
	pass

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python References
  • Python Examples

Python Flow Control

  • Python if…else
  • Python for Loop
  • Python while Loop
  • Python break
  • Python continue
  • Python pass

Python Datatypes

  • Python Integer
  • Python String
  • Python List
  • Python Tuple
  • Python Set
  • Python Dictionary
  • Python OrderedDict
  • Python MultiDict
  • Python Priority Queue

Python Advanced Topics

  • Python Lists vs Tuples
  • Python Comparing Tuples
  • Python Unpacking Tuples
  • Python CSV Files

Python Libraries

  • Python Httplib2

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces