0% found this document useful (0 votes)
77 views

Python Cheatsheet 1

This document provides an overview of common Python programming fundamentals including operators, if/else statements, for and while loops. It includes examples of using comparison operators like == and !=, if/elif/else conditional statements to run different code blocks based on expression evaluations, and for and while loops to iterate through lists and continue executing code as long as a condition is true.

Uploaded by

Anushka Chib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Python Cheatsheet 1

This document provides an overview of common Python programming fundamentals including operators, if/else statements, for and while loops. It includes examples of using comparison operators like == and !=, if/elif/else conditional statements to run different code blocks based on expression evaluations, and for and while loops to iterate through lists and continue executing code as long as a condition is true.

Uploaded by

Anushka Chib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.")

You might also like