File: /home/sangeetha/downloads/python - Cheatsheet - Py Page 1 of 8
File: /home/sangeetha/downloads/python - Cheatsheet - Py Page 1 of 8
File: /home/sangeetha/downloads/python - Cheatsheet - Py Page 1 of 8
py Page 1 of 8
'''
This is a multi-line comment
'''
name = 15
print(name)
print("1 + 2 - 3 * 2 =", 1 + 2 - 3 * 2)
print("(1 + 2 - 3) * 2 =", (1 + 2 - 3) * 2)
# A multi-line quote
multi_line_quote = ''' just
like everyone else" '''
print(quote + multi_line_quote)
# LISTS -------------
# A list allows you to create a list of values and manipulate them
File: /home/sangeetha/Downloads/python_cheatsheet.py Page 2 of 8
# You can get a subset of the list with [min:up to but not including max]
print(grocery_list[1:3])
print(to_do_list)
# Get the second item in the second list (Boxes inside of boxes)
print(to_do_list[1][1])
# TUPLES -------------
# Values in a tuple can't change like lists
pi_tuple = (3, 1, 4, 1, 5, 9)
print(super_villains['Captain Cold'])
# Delete an entry
del super_villains['Fiddler']
print(super_villains)
# Replace a value
super_villains['Pied Piper'] = 'Hartley Rathaway'
# CONDITIONALS -------------
# The if, else and elif statements are used to perform different
# actions based off of conditions
# Comparison Operators : ==, !=, >, <, >=, <=
age = 30
if age > 16 :
print('You are old enough to drive')
if age > 16 :
print('You are old enough to drive')
else :
print('You are not old enough to drive')
if age >= 21 :
print('You are old enough to drive a tractor trailer')
elif age >= 16:
print('You are old enough to drive a car')
else :
print('You are not old enough to drive')
print('\n')
for y in grocery_list:
print(y)
for x in range(0,3):
for y in range(0,3):
print(num_list[x][y])
i += 1
# FUNCTIONS -------------
# Functions allow you to reuse and write readable code
# Type def (define), function name and parameters it receives
# return is used to return something to the caller of the function
def addNumbers(fNum, sNum):
sumNum = fNum + sNum
File: /home/sangeetha/Downloads/python_cheatsheet.py Page 5 of 8
return sumNum
print(addNumbers(1, 4))
print(subNumbers(1, 4))
print('Hello', name)
# STRINGS -------------
# A string is a series of characters surrounded by ' or "
long_string = "I'll catch you if you fall - The Floor"
# String formatting
print("%c is my %s letter and my number %d number is %.5f" % ('X', 'favorite',
1, .14))
# Replace the first word with the second (Add a number to replace more)
print(long_string.replace("Floor", "Ground"))
print(text_in_file)
class Animal:
# None signifies the lack of a value
# You can make a variable private by starting it with __
__name = None
__height = None
__weight = None
__sound = None
def get_name(self):
File: /home/sangeetha/Downloads/python_cheatsheet.py Page 7 of 8
return self.__name
def get_height(self):
return str(self.__height)
def get_weight(self):
return str(self.__weight)
def get_sound(self):
return self.__sound
def get_type(self):
print("Animal")
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
self.__height, self.__weight, self.__sound)
print(cat.toString())
# INHERITANCE -------------
# You can inherit all of the variables and methods from another class
class Dog(Animal):
__owner = None
def get_owner(self):
return self.__owner
def get_type(self):
print ("Dog")
print(spot.toString())
File: /home/sangeetha/Downloads/python_cheatsheet.py Page 8 of 8
class AnimalTesting:
def get_type(self, animal):
animal.get_type()
test_animals = AnimalTesting()
test_animals.get_type(cat)
test_animals.get_type(spot)
spot.multiple_sounds(4)