Python Conditions and If statements

JK JK 2038 
Created at
Updated at  
7,234 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 Facebook X
Comments 0
Similar posts
  1. Python While Loops/For Loops
    7,684
  2. Python Functions
    7,162
  3. Python Dictionaries
    7,209
  4. Python Sets
    7,223
  5. Python Tuples
    7,316
  6. Python Lambda
    7,209
  7. Python Arrays
    7,207
  8. Python Classes/Objects
    7,650
  9. Python Comparison Operators
    7,409
  10. Python Arithmetic Operators
    7,174
  11. Printing string n times
    7,162
  12. String concatenation by join()
    7,226
  13. Python Lists
    7,518
  14. Python String Operations
    7,599
  15. Python Data Types
    7,615
  16. Python Inheritance
    7,308
  17. Python Iterators
    7,213
  18. Python Variables
    7,584
  19. Python Comments
    7,748
  20. Python Syntax
    7,561
  21. Python Polymorphism
    7,198
  22. Python Scope
    7,174
  23. Python Modules
    7,220
  24. Python Getting Started
    7,619
  25. Python Introduction
    7,198
  26. What is Python?
    7,860
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,508
  28. Machine Learning Types and Programming Languages
    7,376
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    8,144
  30. Mastering Excel Data Manipulation with Python
    7,183
  31. Challenge: One Code Problem Per Day
    1,337
  32. Clean Python Environments: The Power of venv vs. Docker
    1,164
  1. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,142
  2. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,793
  3. RegExp example in Python to exclude javascript from HTML code
    7,155
  4. Python code to convert from Lunar to Solar
    7,987
  5. Python example to download webpage
    9,065
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    1,182
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    1,052
  3. Michael Jackson's Billie Jean
    465
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    449
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    385
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    425
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    421
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    456
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,570
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    609
  11. The Evolution and Production Reality of Agentic AI
    485
  12. How to Activate or Waive Your UIUC Student Health Insurance
    625
  13. Complete Guide to Building a Machine Learning Model
    625
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,674
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    638
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,423
  17. What is Docker? Why is Docker also useful in a development environment?
    864
  18. UIUC 2026-2027 Academic Calendar
    2,172
  19. Open-Source LLMs: The AI Revolution
    1,035
  20. Resume 2.0: Leveling Up for My First Software Gig
    3,199
  21. Not everyone will understand what this man just did
    2,624
  22. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,922
  23. Unpacking IU's Shopper
    1,020
  24. Jackie Chan's Police Story: The Action Masterpiece
    919
  25. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,270
  26. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,450
  27. Open Databases for Sex Crime Occurrences in the U.S.
    1,037
  28. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,607
  29. My First Day at University of Illinois-Urvana Champaign
    1,416
  30. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,501
  31. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,553
  32. Sipping the Stars: My Starbucks Adventure
    10,304
  33. Exciting explore at Sequoia National Park
    7,910
  34. My Life Shot at Death Valley
    2,153
  35. Ip Man fights with Muay Thai Master
    1,185
  36. Mad Clown - Don't Die
    1,312
  37. How to get Student Enrollment and Degree Verification at UIUC
    6,184
  38. LAX Thanksgiving Rush: A Joyful Reunion
    1,137
  39. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,409
  40. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,187
  41. 10CM - To Reach You (너에게 닿기를)
    1,378
  42. Feeling weak? Transform yourself at the UIUC ARC!
    1,784
  43. BOYNEXTDOOR - If I Say I Love You
    1,430
  44. The Future of Software Engineer - AI Engineering
    1,149
  45. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,154
  46. Lie - Legend song by BIGBANG
    8,060
  47. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,147
  48. Reimbursement after Vaccination at McKinley Health Center
    1,267
  49. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,201
  50. Common Questions from UIUC school life in terms of CS Program
    1,392