• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Python Basics
    • Comments
    • Functions
    • Lists
    • Loops
    • Strings
    • Syntax Basics
  • Code Examples
  • Categories
    • Basics
    • Code Snippets
    • Python On The Web
    • Scripts
    • System & OS
    • Dictionary
    • Modules In Python
    • Lists
    • Modules
    • OS


You are here: Home / Basics / Conditional statements in Python

Conditional statements in Python

Last Updated: August 27, 2020

Conditional statements

In programming, very often we want to check the conditions and change the
behavior of the program.

How to use Conditional Statements

We can write programs that has more than one choice of actions depending on
a variable’s value.

Perhaps the most well-known statement type is the if statement.

You use the if statement to perform one action if one thing is true,
or any number of other actions, if something else is true.

We must use indentation to define that code that is executed, based on whether
a condition is met.

To compare data in Python we can use the comparison operators, find in
this Booleans, True or False post.

If Statement

The syntax of the if statement is:

if expression:
statement(s)

Elif Statement

Sometimes there are more than two possibilities, in that case we can use the
elif statement

It stands for “else if,” which means that if the original if statement is
false and the elif statement is true, execute the block of code following
the elif statement.

The syntax of the if…elif statement is:

if expression1:
   statement(s)
elif expression2:
   statement(s)
elif expression3:
   statement(s)
else:
   statement(s)

Else Statement

An else statement can be combined with an if statement.

An else statement contains the block of code that executes if the conditional
expression in the if statement resolves to 0 or a false value.

The else statement is an optional statement and there could be at most only one
else statement following if.

The syntax of if..else is:

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial
if expression:
   statement(s)
else:
   statement(s)

Examples

This script will compare two strings based on the input from the use

# This program compares two strings.

# Get a password from the user.
password = raw_input('Enter the password: ')

# Determine whether the correct password
# was entered.

if password == 'hello':
    print'Password Accepted'

else:
    print'Sorry, that is the wrong password.'

Another Example

Let’s show one more examples, in which will also make use of the elif statement.

#!/usr/bin/python

number = 20

guess = int(input('Enter an integer : '))

if guess == number:
    print('Congratulations, you guessed it.')

elif guess < number:
    print('No, it is a little higher than that')

else:
    print('No, it is a little lower than that')

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Basics, Conditionals Date Originally Published: September 25, 2012

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Control Flow crawler Data Types Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Fabric Files ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python On The Web Python Strings Requests Scraping Scripts sh Strings System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Iterating over dictionary in Python
  • Difference between comments and docstrings in Python
  • Shortcut to comment out multiple lines in Python
  • Convert a list containing float numbers to string in Python
  • Single Line and Multi Line Comments in Python

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us