Skip to content

Instantly share code, notes, and snippets.

View mayankdawar's full-sized avatar

Mayank Dawar mayankdawar

  • Ludhiana , Chandigarh , Mohali
View GitHub Profile
@mayankdawar
mayankdawar / bankAccount.py
Created September 24, 2020 19:45
Define a class called BankAccount that accepts the name you want associated with your bank account in a string, and an integer that represents the amount of money in the account. The constructor should initialize two instance variables from those inputs: name and amt. Add a string method so that when you print an instance of BankAccount, you see…
class BankAccount:
def __init__(self, name, amt):
self.name = name
self.amt = amt
def __str__(self):
return 'Your account, {}, has {} dollars.'.format(self.name, self.amt)
t1 = BankAccount('Bob',100)
@mayankdawar
mayankdawar / appleBasket.py
Created September 24, 2020 19:44
Create a class called AppleBasket whose constructor accepts two inputs: a string representing a color, and a number representing a quantity of apples. The constructor should initialize two instance variables: apple_color and apple_quantity. Write a class method called increase that increases the quantity by 1 each time it is invoked. You should …
class AppleBasket:
def __init__(self, color, qty):
self.apple_color = color
self.apple_quantity = qty
def increase(self):
self.apple_quantity += 1
def __str__(self):
return 'A basket of {} {} apples.'.format(self.apple_quantity,self.apple_color)
@mayankdawar
mayankdawar / bike.py
Created September 24, 2020 19:40
Define a class called Bike that accepts a string and a float as input, and assigns those inputs respectively to two instance variables, color and price. Assign to the variable testOne an instance of Bike whose color is blue and whose price is 89.99. Assign to the variable testTwo an instance of Bike whose color is purple and whose price is 25.0.
class Bike:
def __init__(self,col,pr):
self.color = col
self.price = pr
testOne = Bike('blue',89.99)
testTwo = Bike('purple',25.0)
@mayankdawar
mayankdawar / endangered.py
Created August 15, 2020 16:11
Below, we have provided a species list and a population list. Use zip to combine these lists into one list of tuples called pop_info. From this list, create a new list called endangered that contains the names of species whose populations are below 2500.
species = ['golden retriever', 'white tailed deer', 'black rhino', 'brown squirrel', 'field mouse', 'orangutan', 'sumatran elephant', 'rainbow trout', 'black bear', 'blue whale', 'water moccasin', 'giant panda', 'green turtle', 'blue jay', 'japanese beetle']
population = [10000, 90000, 1000, 2000000, 500000, 500, 1200, 8000, 12000, 2300, 7500, 100, 1800, 9500, 125000]
pop_info = zip(species,population)
endangered = [x1 for x1,x2 in pop_info if x2< 2500]
@mayankdawar
mayankdawar / tName.py
Created August 7, 2020 20:55
Given below is a list of lists of athletes. Create a list, t, that saves only the athlete’s name if it contains the letter “t”. If it does not contain the letter “t”, save the athlete name into list other.
t = []
other = []
athletes = [['Phelps', 'Lochte', 'Schooling', 'Ledecky', 'Franklin'], ['Felix', 'Bolt', 'Gardner', 'Eaton'], ['Biles', 'Douglas', 'Hamm', 'Raisman', 'Mikulak', 'Dalton']]
for lst in athletes:
for name in lst:
if 't' in name:
t.append(name)
else:
other.append(name)
@mayankdawar
mayankdawar / nestedList.py
Created August 7, 2020 20:53
Iterate through the contents of l_of_l and assign the third element of sublist to a new list called third.
third = []
l_of_l = [['purple', 'mauve', 'blue'], ['red', 'maroon', 'blood orange', 'crimson'], ['sea green', 'cornflower', 'lavender', 'indigo'], ['yellow', 'amarillo', 'mac n cheese', 'golden rod']]
for i in l_of_l:
third.append(i[2])
@mayankdawar
mayankdawar / olympics2.py
Created August 7, 2020 20:52
Given the dictionary, nested_d, save the medal count for the USA from all three Olympics in the dictionary to the list US_count.
nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}
US_count = []
for i in nested_d:
temp = nested_d[i]['USA']
US_count.append(temp)
@mayankdawar
mayankdawar / sports.py
Created August 7, 2020 20:51
Below, we have provided a nested dictionary. Index into the dictionary to create variables that we have listed in the ActiveCode window.
sports = {'swimming': ['butterfly', 'breaststroke', 'backstroke', 'freestyle'], 'diving': ['springboard', 'platform', 'synchronized'], 'track': ['sprint', 'distance', 'jumps', 'throws'], 'gymnastics': {'women':['vault', 'floor', 'uneven bars', 'balance beam'], 'men': ['vault', 'parallel bars', 'floor', 'rings']}}
# Assign the string 'backstroke' to the name v1
v1 = sports['swimming'][2]
# Assign the string 'platform' to the name v2
v2 = sports['diving'][1]
# Assign the list ['vault', 'floor', 'uneven bars', 'balance beam'] to the name v3
v3 = sports['gymnastics']['women']
# Assign the string 'rings' to the name v4
@mayankdawar
mayankdawar / olympics1.py
Created August 7, 2020 20:49
The variable nested_d contains a nested dictionary with the gold medal counts for the top four countries in the past three Olympics. Assign the value of Great Britain’s gold medal count from the London Olympics to the variable london_gold. Use indexing. Do not hardcode.
nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}
london_gold = nested_d['London']['Great Britain']
@mayankdawar
mayankdawar / nested2.py
Created August 7, 2020 20:48
Provided is a nested data structure. Follow the instructions in the comments below. Do not hard code.
nested = {'data': ['finding', 23, ['exercises', 'hangout', 34]], 'window': ['part', 'whole', [], 'sum', ['math', 'calculus', 'algebra', 'geometry', 'statistics',['physics', 'chemistry', 'biology']]]}
# Check to see if the string data is a key in nested, if it is, assign True to the variable data, otherwise assign False.
if 'data' in nested:
data = True
else:
data = False
# Check to see if the integer 24 is in the value of the key data, if it is then assign to the variable twentyfour the value of True, otherwise False.
if 24 in nested['data']: