Python Lab 2
Python Lab 2
Python Lab 2
/usr/bin/env python
# coding: utf-8
# In[6]:
#Example: If Statement
""" 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"."""
a = 33
b = 200
if b > a:
print("b is greater than a")
# # Indentation
# Python relies on indentation (whitespace at the beginning of a line) to
define scope in the code. Other programming languages often use curly-brackets
for this purpose.
# In[13]:
# In[14]:
# In[15]:
#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".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
# # Else
# The else keyword catches anything which isn't caught by the preceding
conditions.
# In[18]:
# 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".
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[19]:
# # Short Hand If
# If you have only one statement to execute, you can put it on the same line
as the if statement.
# In[20]:
a = 200
b = 33
# In[21]:
a = 2
b = 330