Python Conditions and If statements

JK1184 
Created at Jun 16, 2023 09:57:37
Updated at May 09, 2024 16:05:54 
86   0   0   0  

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword.

a = 33
b = 200
if b > a:
  print("b is greater than a")

In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".


Elif

The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".


Else

The else keyword catches anything which isn't caught by the preceding conditions.

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".

You can also have an else without the elif:

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

Short Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

if a > b: print("a is greater than b")

Short Hand If ... Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

a = 2
b = 330
print("A") if a > b else print("B")

You can also have multiple else statements on the same line:

a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")

And

The and keyword is a logical operator, and is used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

Or

The or keyword is a logical operator, and is used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

a = 33
b = 200
if not a > b:
  print("a is NOT greater than b")

Nested If

You can have if statements inside if statements, this is called nested if statements.

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

a = 33
b = 200

if b > a:
  pass

 

Below YouTube content will be helpful for better understanding:

 



Tags: Python Python If Python Nested If Python Short Hand If Share on Facebook Share on X

◀ PREVIOUS
Python Dictionaries
▶ NEXT
Python While Loops/For Loops
  Comments 0
Login for comment
SIMILAR POSTS

Python While Loops/For Loops (updated at May 09, 2024)

Python Functions (updated at May 09, 2024)

Python Dictionaries (updated at May 09, 2024)

Python Sets (updated at May 09, 2024)

Python Tuples (updated at May 09, 2024)

Python Lambda (updated at May 09, 2024)

Python Arrays (updated at May 09, 2024)

Python Classes/Objects (updated at May 09, 2024)

Python Comparison Operators (updated at May 10, 2024)

Python Arithmetic Operators (created at Jun 14, 2023)

Python Lists (updated at May 10, 2024)

Python String Operations (updated at May 10, 2024)

Python Data Types (updated at May 10, 2024)

Python Inheritance (updated at May 09, 2024)

Python Iterators (updated at May 09, 2024)

Python Variables (updated at May 10, 2024)

Python Comments (updated at May 10, 2024)

Python Syntax (updated at May 10, 2024)

Python Polymorphism (updated at May 09, 2024)

Python Scope (updated at May 09, 2024)

Python Modules (updated at May 09, 2024)

Python Getting Started (updated at May 10, 2024)

Python Introduction (created at Jun 12, 2023)

What is Python? (updated at Jan 04, 2024)

Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering (updated at May 12, 2024)

Machine Learning Types and Programming Languages (updated at Nov 29, 2023)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship (updated at May 10, 2024)

Mastering Excel Data Manipulation with Python (updated at Apr 26, 2024)

OTHER POSTS IN THE SAME CATEGORY

Try...Catch Helps Ignoring Data Type Miss-Match Error in Python (updated at Mar 26, 2024)

RegExp example in Python to exclude javascript from HTML code (created at Mar 22, 2024)

Python code to convert from Lunar to Solar (created at Mar 22, 2024)

Python example to download webpage (updated at May 15, 2024)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship (updated at May 10, 2024)

Python Modules (updated at May 09, 2024)

Python Scope (updated at May 09, 2024)

Python Polymorphism (updated at May 09, 2024)

Python Iterators (updated at May 09, 2024)

Python Inheritance (updated at May 09, 2024)

Python Classes/Objects (updated at May 09, 2024)

Python Arrays (updated at May 09, 2024)

Python Lambda (updated at May 09, 2024)

Python Functions (updated at May 09, 2024)

Python While Loops/For Loops (updated at May 09, 2024)

Python Dictionaries (updated at May 09, 2024)

Python Sets (updated at May 09, 2024)

Python Tuples (updated at May 09, 2024)

Python Comparison Operators (updated at May 10, 2024)

Python Arithmetic Operators (created at Jun 14, 2023)

Python Lists (updated at May 10, 2024)

Python String Operations (updated at May 10, 2024)

Python Data Types (updated at May 10, 2024)

Python Variables (updated at May 10, 2024)

Python Comments (updated at May 10, 2024)

Python Syntax (updated at May 10, 2024)

Python Getting Started (updated at May 10, 2024)

Python Introduction (created at Jun 12, 2023)

What is Python? (updated at Jan 04, 2024)

UPDATES

Aiming for Congressional Award Gold? Your Registration Guide! (created at May 19, 2024)

Digital SAT what you should know (updated at May 17, 2024)

Reflections on a Year of Volunteering at Torrance Wilson Park: Season in 2023-2024 (updated at May 16, 2024)

ChatGPT Reset command and Ignore the Previous Response feature to have a Solid Result (updated at May 16, 2024)

Python example to download webpage (updated at May 15, 2024)

Impact in the theird party cookie on web browser (updated at May 15, 2024)

Why Nike Card Wallets Are a Smart Buy for the Stylishly Practical (updated at May 15, 2024)

How to Rip a Leg that hundreds of people succeeded in (updated at May 15, 2024)

Little Johnny Kang's Taekwondo Journey at the Kukkiwon in 2016 (created at May 15, 2024)

IU's Life Snapshots (updated at May 15, 2024)

Java Tutorials associated with AP Computer Science A (updated at May 15, 2024)

Java Getting Started (updated at May 15, 2024)

Java Syntax (updated at May 15, 2024)

Java Comments (updated at May 15, 2024)

Java Variables (updated at May 15, 2024)

Java Math (updated at May 15, 2024)

Java If ... Else (updated at May 15, 2024)

Java Short Hand If...Else (Ternary Operator) (updated at May 15, 2024)

Java Switch Statements (updated at May 15, 2024)

Loading XML Data with JavaScript (created at May 15, 2024)

Finding Comfort in the Chaos: My LOL Journey on US Servers (updated at May 14, 2024)

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue (updated at May 13, 2024)

Java Arrays (updated at May 13, 2024)

Meet NewJeans Danielle: The Rising Star with Killer Dance Moves! (updated at May 13, 2024)

IU's 5.16MHz with UAENA (updated at May 12, 2024)

Microsoft Windows commands frquently used (updated at May 12, 2024)

Key Features of the Apache License (updated at May 12, 2024)

Naver Papago - multilingual machine translation service (updated at May 12, 2024)

What is Sitemap? Why do we need it? (updated at May 12, 2024)

What is a smart TV? (updated at May 12, 2024)

Semantic Network - a method of expressing knowledge based on a mesh structure (updated at May 12, 2024)

What is the role of README.md at GitHub/GitLab Repository? (updated at May 12, 2024)

Google Map Link Logic (updated at May 12, 2024)

Flying side kick - Run → Jump → Kick (updated at May 12, 2024)

Las Vegas KÀ by Cirque du Soleil that showed us an exciting fantasy (updated at May 12, 2024)

Physics Areas and Formulas (updated at May 12, 2024)

Statistics Formulas (updated at May 12, 2024)

PreCalculus Formulas for Trigonometry and Math Analysis (updated at May 12, 2024)

Differential/Integral Calculus Formulas (updated at May 12, 2024)

Mean(Averange) and Median based on Global Wealth per Person (updated at May 12, 2024)

Sneaker Style: Perfect Shoes to Wear with Jeans (updated at May 12, 2024)

YouTube Multiview Video Games Dataset from UC Irvine Machine Learning Repository (updated at May 12, 2024)

How to tie your Taekwondo Belt? (updated at May 12, 2024)

Taekwondo Tornado Kick Tutorial - one of my favorite kicks (updated at May 12, 2024)

Exploring the Heartfelt History and Traditions of Valentine's Day (updated at May 12, 2024)

Resell Price $9,400 - Apple Vision Pro Goes Off Right After Launch (updated at May 12, 2024)

Clack Attack - Embracing the Joyful Chaos of Mechanical Keyboards (updated at May 12, 2024)

Indulging in Delight: Waffles and Long Drinks in Seoul, Korea (updated at May 12, 2024)

A House Tour Adventure in the California, US (updated at May 12, 2024)

Base camp for house hunting in Anaheim near Disney Land (updated at May 12, 2024)