Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 55 additions & 27 deletions Python_Exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def power(a,b):

# ** What is 7 to the power of 4?**
print(7**4)

return None

Expand All @@ -16,33 +17,36 @@ def split_str(s):

# ** Split this string:**
#
# s = "Hi there Sam!"
s = "Hi there Sam!"
#
# **into a list. **

return None
x=s.split()
print(x)

return None


def format(planet,diameter):

# ** Given the variables:**
#
# planet = "Earth"
# diameter = 12742
planet = "Earth"
diameter = 12742
#
# ** Use .format() to print the following string: **
#
# The diameter of Earth is 12742 kilometers.
print("The diameter of {} is {} kilometers.".format(planet,diameter))

return None
return None



def indexing(lst):

# ** Given this nested list, use indexing to grab the word "hello" **

#lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]
print(lst[3][1][2][0])

return None

Expand All @@ -51,7 +55,8 @@ def dictionary(d):

# ** Given this nested dictionary grab the word "hello". Be prepared, this will be annoying/tricky **

# d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}
print(d['k1'][3]['tricky'][3]['target'][3])


return None
Expand All @@ -60,7 +65,7 @@ def dictionary(d):
def subjective():

# ** What is the main difference between a tuple and a list? **
# Tuple is _______
print("Tuple is immutable")

return None

Expand All @@ -74,21 +79,26 @@ def domainGet(email):
# user@domain.com
#
# **So for example, passing "user@domain.com" would return: domain.com**

return None
return email.split('@')[1]


def findDog(st):

# ** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **

return None
t=st.split()
return 'dog' in t


def countDog(st):

# ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **

t=st.split()
count=0
for item in t:
if item=='dog':
count=count+1
print(count)
return None


Expand All @@ -97,13 +107,16 @@ def lambdafunc(seq):

# ** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**
#
# seq = ['soup','dog','salad','cat','great']
# seq = ['soup','dog','salad','cat','great']
#
# **should be filtered down to:**
#
# ['soup','salad']

return None
t=list(filter(lambda item: 's' in item, seq))
print(t)

return None


def caught_speeding(speed, is_birthday):
Expand All @@ -114,8 +127,13 @@ def caught_speeding(speed, is_birthday):
# If your speed is 60 or less, the result is "No Ticket". If speed is between 61
# and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all
# cases. **

return None
if speed<=60:
return "No Ticket"
elif speed>60 and speed<=80:
return "Small Ticket"
elif speed>80:
return "Big Ticket"



## Numpy Exercises
Expand All @@ -126,50 +144,56 @@ def caught_speeding(speed, is_birthday):
def create_arr_of_fives():

#### Create an array of 10 fives
l=np.full((10,1),5)
#### Convert your output into list
#### e.g return list(arr)

return None
return list(l)



def even_num():

### Create an array of all the even integers from 10 to 50
l=np.arange(10,50,2)
### Convert your output into list
### e.g return list(arr)

return None
return list(l)



def create_matrix():

### Create a 3x3 matrix with values ranging from 0 to 8
t=np.arange(0,8+1)
l=t.reshape(3,3)
### Convert your output into list
### e.g return (arr).tolist()

return None
return l.tolist()



def linear_space():

### Create an array of 20 linearly spaced points between 0 and 1
l=np.linspace(0,1,20)
### Convert your output into list
### e.g return list(arr)

return None
return list(l)



def decimal_mat():

### Create an array of size 10*10 consisting of numbers from 0.01 to 1
l=np.linspace(0.01,1,100).reshape(10,10)
### Convert your output into list
### e.g return (arr).tolist()

return None
return l.tolist()



Expand All @@ -190,7 +214,9 @@ def slices_1():
# [17, 18, 19, 20],
# [22, 23, 24, 25]])

return None
l=arr[2:,1:]

return l.tolist()



Expand All @@ -210,8 +236,9 @@ def slices_2():
# array([[ 2],
# [ 7],
# [12]])
l=arr[0:3,1]

return None
return l.tolist()



Expand All @@ -230,8 +257,9 @@ def slices_3():
### e.g return (arr).tolist()
# array([[16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])
t=arr[3:]

return None
return t.tolist()


# Great job!