Python Conditions and If statements

JK JK 2038 
Created at
Updated at  
7,184 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,616
  2. Python Functions
    7,121
  3. Python Dictionaries
    7,159
  4. Python Sets
    7,164
  5. Python Tuples
    7,257
  6. Python Lambda
    7,170
  7. Python Arrays
    7,163
  8. Python Classes/Objects
    7,578
  9. Python Comparison Operators
    7,342
  10. Python Arithmetic Operators
    7,137
  11. Printing string n times
    7,116
  12. String concatenation by join()
    7,182
  13. Python Lists
    7,455
  14. Python String Operations
    7,535
  15. Python Data Types
    7,547
  16. Python Inheritance
    7,267
  17. Python Iterators
    7,174
  18. Python Variables
    7,513
  19. Python Comments
    7,670
  20. Python Syntax
    7,497
  21. Python Polymorphism
    7,155
  22. Python Scope
    7,126
  23. Python Modules
    7,177
  24. Python Getting Started
    7,568
  25. Python Introduction
    7,162
  26. What is Python?
    7,822
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,459
  28. Machine Learning Types and Programming Languages
    7,326
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    8,050
  30. Mastering Excel Data Manipulation with Python
    7,143
  31. Challenge: One Code Problem Per Day
    1,255
  32. Clean Python Environments: The Power of venv vs. Docker
    1,063
  1. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,031
  2. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,538
  3. RegExp example in Python to exclude javascript from HTML code
    7,115
  4. Python code to convert from Lunar to Solar
    7,888
  5. Python example to download webpage
    8,858
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    680
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    606
  3. Michael Jackson's Billie Jean
    357
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    280
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    274
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    310
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    326
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    352
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,117
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    506
  11. The Evolution and Production Reality of Agentic AI
    393
  12. How to Activate or Waive Your UIUC Student Health Insurance
    498
  13. Complete Guide to Building a Machine Learning Model
    516
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,583
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    534
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,302
  17. What is Docker? Why is Docker also useful in a development environment?
    781
  18. UIUC 2026-2027 Academic Calendar
    1,919
  19. Open-Source LLMs: The AI Revolution
    949
  20. Resume 2.0: Leveling Up for My First Software Gig
    2,840
  21. Not everyone will understand what this man just did
    2,339
  22. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,803
  23. Unpacking IU's Shopper
    950
  24. Jackie Chan's Police Story: The Action Masterpiece
    828
  25. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,154
  26. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,355
  27. Open Databases for Sex Crime Occurrences in the U.S.
    928
  28. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,250
  29. My First Day at University of Illinois-Urvana Champaign
    1,353
  30. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,372
  31. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,447
  32. Sipping the Stars: My Starbucks Adventure
    10,054
  33. Exciting explore at Sequoia National Park
    7,839
  34. My Life Shot at Death Valley
    1,995
  35. Ip Man fights with Muay Thai Master
    1,105
  36. Mad Clown - Don't Die
    1,224
  37. How to get Student Enrollment and Degree Verification at UIUC
    5,656
  38. LAX Thanksgiving Rush: A Joyful Reunion
    1,062
  39. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,319
  40. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,129
  41. 10CM - To Reach You (너에게 닿기를)
    1,312
  42. Feeling weak? Transform yourself at the UIUC ARC!
    1,718
  43. BOYNEXTDOOR - If I Say I Love You
    1,346
  44. The Future of Software Engineer - AI Engineering
    1,073
  45. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,077
  46. Lie - Legend song by BIGBANG
    7,972
  47. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,049
  48. Reimbursement after Vaccination at McKinley Health Center
    1,181
  49. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,123
  50. Common Questions from UIUC school life in terms of CS Program
    1,305