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
| # 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 |
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
| #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))) |
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
| #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'} |
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
| 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' |
OlderNewer