This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Classes and Methods Cheat Sheet (Optional) | |
Classes and Methods Cheat Sheet | |
In the past few videos, we’ve seen how to define classes and methods in Python. Here, you’ll find a run-down of everything we’ve covered, so you can refer to it whenever you need a refresher. | |
Defining classes and methods | |
############################################ | |
class ClassName: | |
def method_name(self, other_parameters): | |
body_of_method | |
Classes and Instances |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def counter(start, stop): | |
x = start | |
if x > stop: | |
return_string = "Counting down: " | |
while x >= stop: | |
return_string += str(x)+"," | |
x -= 1 | |
else: | |
return_string = "Counting up: " | |
while x < stop: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_prices(basket): | |
total = 0 | |
for price in basket.values(): | |
total += price | |
return round(total, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def groups_per_user(group_dictionary): | |
user_groups = {} | |
for group, users in group_dictionary.items(): | |
for user in users: | |
if user not in user_groups: | |
user_groups[user] = [] | |
user_groups[user].append(group) | |
return user_groups | |
print(groups_per_user({"local": ["admin", "userA"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def email_list(domains): | |
emails = [] | |
for domain, users in domains.items(): | |
for user in users: | |
emails.append(user+'@'+domain) | |
return(emails) | |
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], \ | |
"yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Definition | |
x = {key1:value1, key2:value2} | |
Operations | |
len(dictionary) - Returns the number of items in the dictionary | |
for key in dictionary - Iterates over each key in the dictionary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_letters(text): | |
result = {} | |
for char in text: | |
if char not in result: | |
result[char] = 0 | |
result[char] += 1 | |
return result | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def guest_list(guests): | |
for guest in guests: | |
name, age, job = guest | |
print("{} is {} years old and works as {}".format(name, age, job)) | |
guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")]) | |
#Click Run to submit code | |
""" | |
Output should match: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"] | |
# Generate newfilenames as a list containing the new filenames | |
# using as many lines of code as your chosen method requires. | |
newfilenames = [x.replace('.hpp','.h') for x in filenames] | |
print(newfilenames) | |
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pig_latin(text): | |
words = text.split() | |
pigged_text = [] | |
for word in words: | |
word = word[1:] + word[0] + 'ay' | |
pigged_text.append(word) | |
return ' '.join(pigged_text) | |