Overview
Python supports the usual logical mathematical conditions which can be used in several ways, most commonly in various “if statement” types and loops.
- 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
Simple If Statement
Basically, “if” the statement is true, do something.
x = 88
y = 100
if y > x:
print("y is greater than x")
Shorthand version:
x = 88
y = 100
if y > x: print("y is greater than x")
Output:
y is greater than x
The elif Keyword
The elif keyword basically says if the previous conditions were not true, then try this condition.
x = 12
y = 12
if y > x:
print("y is greater than x")
elif x == y:
print("x and y are equal")
Output:
x and y are equal
The else Keyword
The else keyword catches anything which isn’t caught by the preceding conditions.
x = 99
y = 88
if y > x:
print("y is greater than x")
elif x == y:
print("x and y are equal")
else:
print("x is greater than y")
Output:
x is greater than y
The and Keyword
The and keyword is a logical operator, and is used to combine conditional statements.
x = 100
y = 88
z = 200
if x > y and y < z:
print("Both conditions are true")
Output:
Both conditions are true
The or Keyword
The or keyword is a logical operator, and is used to combine conditional statements.
x = 100
y = 88
z = 200
if x < y or y < z:
print("One of the conditions is true")
Output:
One of the conditions is true
The not Keyword
The not keyword is a logical operator, and is used to reverse the result of the conditional statement.
x = 8
y = 10
if not x > y:
print("a is NOT greater than b")
Output:
a is NOT greater than b
Using the pass Statement
if statements cannot be empty, but if you so have an if statement with no content, a pass statement can be placed to avoid getting an error.
x = 5
y = 12
if y > x:
pass
Output:
#there will be no output, and no error will be thrown
Nesting if Statements
Nesting if statements is when there are if statements inside other if statements.
x = 88
if x > 10:
print("x is more than 10")
if x > 50:
print("and also above 50")
else:
print("but not above 50")
Output:
x is more than 10
and also above 50
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.