Python Conditions and If statements

JK JK 2030 
Created at
Updated at  
7,152 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,543
  2. Python Functions
    7,081
  3. Python Dictionaries
    7,084
  4. Python Sets
    7,129
  5. Python Tuples
    7,199
  6. Python Lambda
    7,125
  7. Python Arrays
    7,109
  8. Python Classes/Objects
    7,508
  9. Python Comparison Operators
    7,284
  10. Python Arithmetic Operators
    7,101
  11. Printing string n times
    7,088
  12. String concatenation by join()
    7,153
  13. Python Lists
    7,383
  14. Python String Operations
    7,464
  15. Python Data Types
    7,476
  16. Python Inheritance
    7,217
  17. Python Iterators
    7,128
  18. Python Variables
    7,446
  19. Python Comments
    7,600
  20. Python Syntax
    7,437
  21. Python Polymorphism
    7,105
  22. Python Scope
    7,080
  23. Python Modules
    7,130
  24. Python Getting Started
    7,496
  25. Python Introduction
    7,125
  26. What is Python?
    7,771
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,406
  28. Machine Learning Types and Programming Languages
    7,281
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,959
  30. Mastering Excel Data Manipulation with Python
    7,101
  31. Challenge: One Code Problem Per Day
    1,167
  32. Clean Python Environments: The Power of venv vs. Docker
    933
  1. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    896
  2. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,317
  3. RegExp example in Python to exclude javascript from HTML code
    7,076
  4. Python code to convert from Lunar to Solar
    7,781
  5. Python example to download webpage
    8,688
Recently updated
  1. Michael Jackson's Billie Jean
    254
  2. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    42
  3. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    144
  4. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    163
  5. Watch This Dazzling Dancer’s Mind-Blowing Moves
    186
  6. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    225
  7. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    728
  8. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    358
  9. The Evolution and Production Reality of Agentic AI
    294
  10. How to Activate or Waive Your UIUC Student Health Insurance
    380
  11. Complete Guide to Building a Machine Learning Model
    413
  12. My life cuts at Las Vegas during Thanksgiving day holiday
    7,506
  13. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    437
  14. Harness vs. OpenClaw: Two Very Different "Agents"
    1,105
  15. What is Docker? Why is Docker also useful in a development environment?
    690
  16. UIUC 2026-2027 Academic Calendar
    1,754
  17. Open-Source LLMs: The AI Revolution
    849
  18. Resume 2.0: Leveling Up for My First Software Gig
    2,487
  19. Not everyone will understand what this man just did
    2,068
  20. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,686
  21. Unpacking IU's Shopper
    855
  22. Jackie Chan's Police Story: The Action Masterpiece
    732
  23. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,040
  24. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,260
  25. Open Databases for Sex Crime Occurrences in the U.S.
    796
  26. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,895
  27. My First Day at University of Illinois-Urvana Champaign
    1,269
  28. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,243
  29. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,338
  30. Sipping the Stars: My Starbucks Adventure
    9,812
  31. Exciting explore at Sequoia National Park
    7,757
  32. My Life Shot at Death Valley
    1,830
  33. Ip Man fights with Muay Thai Master
    1,016
  34. Mad Clown - Don't Die
    1,117
  35. How to get Student Enrollment and Degree Verification at UIUC
    5,182
  36. LAX Thanksgiving Rush: A Joyful Reunion
    983
  37. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,214
  38. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,057
  39. 10CM - To Reach You (너에게 닿기를)
    1,228
  40. Feeling weak? Transform yourself at the UIUC ARC!
    1,637
  41. BOYNEXTDOOR - If I Say I Love You
    1,254
  42. The Future of Software Engineer - AI Engineering
    988
  43. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    989
  44. Lie - Legend song by BIGBANG
    7,893
  45. Why ROLLBACK is useful when you work with Google Gemini CLI?
    929
  46. Reimbursement after Vaccination at McKinley Health Center
    1,092
  47. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,030
  48. Common Questions from UIUC school life in terms of CS Program
    1,204
  49. UIUC Immunization Compliance
    1,292
  50. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,165