Python Conditions and If statements

JK 2010 
Created at
Updated at  
7,118 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,474
  2. Python Functions
    7,051
  3. Python Dictionaries
    7,041
  4. Python Sets
    7,072
  5. Python Tuples
    7,152
  6. Python Lambda
    7,092
  7. Python Arrays
    7,074
  8. Python Classes/Objects
    7,441
  9. Python Comparison Operators
    7,229
  10. Python Arithmetic Operators
    7,061
  11. Printing string n times
    7,051
  12. String concatenation by join()
    7,117
  13. Python Lists
    7,316
  14. Python String Operations
    7,388
  15. Python Data Types
    7,415
  16. Python Inheritance
    7,178
  17. Python Iterators
    7,092
  18. Python Variables
    7,374
  19. Python Comments
    7,510
  20. Python Syntax
    7,388
  21. Python Polymorphism
    7,071
  22. Python Scope
    7,052
  23. Python Modules
    7,076
  24. Python Getting Started
    7,432
  25. Python Introduction
    7,087
  26. What is Python?
    7,697
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,344
  28. Machine Learning Types and Programming Languages
    7,218
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,855
  30. Mastering Excel Data Manipulation with Python
    7,050
  31. Challenge: One Code Problem Per Day
    1,062
  32. Clean Python Environments: The Power of venv vs. Docker
    802
  1. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,135
  2. RegExp example in Python to exclude javascript from HTML code
    7,040
  3. Python code to convert from Lunar to Solar
    7,692
  4. Python example to download webpage
    8,548
  5. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,855
  6. Python Modules
    7,076
  7. Python Scope
    7,052
  8. Python Polymorphism
    7,071
  9. Python Iterators
    7,092
  10. Python Inheritance
    7,178
  11. Python Classes/Objects
    7,441
  12. Python Arrays
    7,074
  13. Python Lambda
    7,092
  14. Python Functions
    7,051
  15. Python While Loops/For Loops
    7,474
  16. Python Dictionaries
    7,041
  17. Python Sets
    7,072
  18. Python Tuples
    7,152
  19. Python Comparison Operators
    7,229
  20. Python Arithmetic Operators
    7,061
  21. Printing string n times
    7,051
  22. String concatenation by join()
    7,117
  23. Python Lists
    7,316
  24. Python String Operations
    7,388
  25. Python Data Types
    7,415
  26. Python Variables
    7,374
  27. Python Comments
    7,510
  28. Python Syntax
    7,388
  29. Python Getting Started
    7,432
  30. Python Introduction
    7,087
Recently updated
  1. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    104
  2. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    275
  3. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    194
  4. The Evolution and Production Reality of Agentic AI
    185
  5. How to Activate or Waive Your UIUC Student Health Insurance
    263
  6. Complete Guide to Building a Machine Learning Model
    305
  7. My life cuts at Las Vegas during Thanksgiving day holiday
    7,388
  8. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    329
  9. Harness vs. OpenClaw: Two Very Different "Agents"
    926
  10. Clean Python Environments: The Power of venv vs. Docker
    802
  11. What is Docker? Why is Docker also useful in a development environment?
    602
  12. UIUC 2026-2027 Academic Calendar
    1,539
  13. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    786
  14. Open-Source LLMs: The AI Revolution
    734
  15. Resume 2.0: Leveling Up for My First Software Gig
    2,141
  16. Not everyone will understand what this man just did
    1,770
  17. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,566
  18. Unpacking IU's Shopper
    736
  19. Jackie Chan's Police Story: The Action Masterpiece
    627
  20. The IVE Story: Identity, 'I AM' Charts, and Influence
    932
  21. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,171
  22. Open Databases for Sex Crime Occurrences in the U.S.
    699
  23. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,564
  24. My First Day at University of Illinois-Urvana Champaign
    1,173
  25. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,106
  26. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,186
  27. Sipping the Stars: My Starbucks Adventure
    9,610
  28. Exciting explore at Sequoia National Park
    7,635
  29. My Life Shot at Death Valley
    1,662
  30. Ip Man fights with Muay Thai Master
    919
  31. Mad Clown - Don't Die
    997
  32. How to get Student Enrollment and Degree Verification at UIUC
    4,729
  33. LAX Thanksgiving Rush: A Joyful Reunion
    912
  34. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,101
  35. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    964
  36. 10CM - To Reach You (너에게 닿기를)
    1,143
  37. Feeling weak? Transform yourself at the UIUC ARC!
    1,560
  38. BOYNEXTDOOR - If I Say I Love You
    1,174
  39. The Future of Software Engineer - AI Engineering
    922
  40. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    894
  41. Lie - Legend song by BIGBANG
    7,802
  42. Why ROLLBACK is useful when you work with Google Gemini CLI?
    818
  43. Reimbursement after Vaccination at McKinley Health Center
    1,005
  44. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    951
  45. Common Questions from UIUC school life in terms of CS Program
    1,096
  46. UIUC Immunization Compliance
    1,189
  47. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,081
  48. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,080
  49. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    859
  50. Common methods to improve coding skills
    979