Python Basics for Data Science
Module 3: Python Programming Fundamentals
Operators/Keywords/Statements Description Example
==, !=, >, <, >=, <= Comparison operators. Return a a = 5
Boolean. #b is false
b=a==6
if <comparison>: Runs code based on true #expression that can be true or false
<code runs when comparison is evaluation of the if argument. if age > 18:
true> #within an indent, we have the
#expression that is run if the condition is #true
print("you can enter")
#The statements after the if statement will #run
regardless if the condition is true or #false
print("move on")
if <comparison>: Runs code based on evaluation # if=else statement example
<code that runs when of the if argument. If the weather = “sunny”
comparison is true> argument is false runs the # weather = “cloudy”
if weather == “sunny”:
else: code that follows the else
print("The sun is out today")
<code that runs when statement.
else:
comparison is false> print("Looks like it might rain")
elif Similar to else if but allows # If-elif statement example
for else if statements as age = 18
well. if age > 18:
print("you can enter")
elif age == 18:
print("go see Pink Floyd")
else:
print("go see Meat Loaf")
for loop Continue to do something while dates = [1982,1980,1973]
iterating through a list. for year in dates:
print(year)
while loop Continue to do something as # While Loop Example
long as while statement is dates = [1982, 1980, 1973, 2000]
i = 0
true.
year = dates[0]
while year != 1973:
print(year)
i = i + 1
year = dates[i]
print("It took ", i ,"repetitions to get out of
loop.")