Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
guestPK1986 / functions_classes.py
Last active December 13, 2024 10:22
python functions, classes
# Create a class Cat which will define name and age of cat
# Instantiate the Cat object with 3 cats
# Create a function that finds the oldest cat
# Print out: "The oldest cat is x years old.". x will be the oldest cat age by using the function
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
@guestPK1986
guestPK1986 / map_filter_zip_reduce.py
Last active August 30, 2020 19:44
map(), filter(), zip(), reduce(), lambda
#1 Capitalize first letter of all of the pet names and print the list
my_pets = ['sisi', 'bibi', 'titi', 'carla']
def first_upper(item):
for x in item:
up_char = item[0].upper()
item = up_char + item[1:]
return item
print(list(map(first_upper, my_pets)))
@guestPK1986
guestPK1986 / comprehensions.py
Created August 30, 2020 20:32
list, set, dictionary comprehension exercises
#create a LIST of characters in string 'hello'
my_list = [i for i in 'hello']
print("my_list "+ str(my_list))
#create a LIST of numbers in tuple (1,2,3,4)
new_tuple = [i for i in (1,2,3,4)]
print("new_tuple " + str(new_tuple))
#create a SET of unique items from string
a_set = {i for i in 'hello'}
@guestPK1986
guestPK1986 / Counter_defaultdict_OrderedDict.py
Last active September 15, 2020 21:06
Counter_defaultdict_OrderedDict.py
from collections import Counter, defaultdict, OrderedDict
li = [1,2,3,4,5,6,7,7,7]
sentance = "blah blah blah thinking about python"
print(Counter(li)) # will return Counter({7: 3, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1})
print(Counter(sentance)) # will return Counter({'h': 5, ' ': 5, 'b': 4, 'a': 4, 'l': 3, 't': 3, 'n': 3, 'i': 2, 'o': 2, 'k': 1, 'g': 1, 'u': 1, 'p': 1, 'y': 1})
dictionary = defaultdict(lambda: "does not exist", {"a":1, "b": 2})
print(dictionary['c']) # will return 'does not exist'