This file contains 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' |
This file contains 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 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 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 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
#!/bin/bash | |
##################################################### | |
# Name: Bash CheatSheet for Mac OSX | |
# | |
# A little overlook of the Bash basics | |
# | |
# Usage: | |
# | |
# Author: J. Le Coupanec | |
# Date: 2014/11/04 |
This file contains 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
#Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple. | |
lst_tups = [('Articuno', 'Moltres', 'Zaptos'), ('Beedrill', 'Metapod', 'Charizard', 'Venasaur', 'Squirtle'), ('Oddish', 'Poliwag', 'Diglett', 'Bellsprout'), ('Ponyta', "Farfetch'd", "Tauros", 'Dragonite'), ('Hoothoot', 'Chikorita', 'Lanturn', 'Flaaffy', 'Unown', 'Teddiursa', 'Phanpy'), ('Loudred', 'Volbeat', 'Wailord', 'Seviper', 'Sealeo')] | |
t_check = [] | |
for x in lst_tups: | |
t_check.append(x[2]) | |
print (t_check) | |
#Below, we have provided a list of tuples. |
This file contains 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
#Write a function named total that takes a list of integers as input, | |
#and returns the total value of all those integers added together. | |
def total(lst): | |
accum=0 | |
for x in lst: | |
accum=accum+x | |
return accum | |
lst=[1,2,3,4] | |
total(lst) |
This file contains 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. Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports. | |
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey'] | |
sports.insert(2, 'horseback riding') | |
# 2. Write code to take ‘London’ out of the list trav_dest. | |
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne'] | |
trav_dest.pop(7) |
This file contains 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 one conditional so that if “Friendly” is in w, then “Friendly is here!” should be assigned to the variable wrd. | |
#If it’s not, check if “Friend” is in w. If so, the string “Friend is here!” should be assigned to the variable wrd, | |
#otherwise “No variation of friend is in here.” should be assigned to the variable wrd | |
w = "Friendship is a wonderful human experience!" | |
for x in w: | |
if "Friendly" in w: | |
wrd = str("Friendly is here!") | |
elif "Friend" in w: | |
wrd = str("Friend is here!") |
This file contains 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
#rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) | |
#with every month separated by a comma. | |
#Write code to compute the number of months that have more than 3 inches of rainfall. | |
#Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0 | |
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85" | |
file = rainfall_mi.split(",") | |
print(file) | |
num_rainy_months = 0 | |
for x in file: |
NewerOlder