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

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Conditional statements in Python

Conditional statements in Python

Author: PFB Staff Writer
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:

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')

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics, Conditionals Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

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