diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..c4baeb6 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -6,115 +6,143 @@ def power(a,b): - # ** What is 7 to the power of 4?** + a=int(input()) + b=int(input()) + c= a**b + print(c) return None def split_str(s): + a=str(input("enter your string:")) + b=a.split() + print(b) - # ** Split this string:** -# -# s = "Hi there Sam!" -# -# **into a list. ** - return None def format(planet,diameter): + a=input("enter the name of your planet:") + b=int(input("enter the diameter:")) + print("The diameter of ",a," is ",b," kilometres.") -# ** Given the variables:** -# -# planet = "Earth" -# diameter = 12742 -# -# ** Use .format() to print the following string: ** -# -# The diameter of Earth is 12742 kilometers. - return None def indexing(lst): + lst=[1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] + print(lst[3][1][2][0]) -# ** Given this nested list, use indexing to grab the word "hello" ** - -#lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] - return None def dictionary(d): + d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} + print(d['k1'][3]['tricky'][3]['target'][3]) -# ** 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']}]}]} - - return None def subjective(): + immutable -# ** What is the main difference between a tuple and a list? ** -# Tuple is _______ - return None def domainGet(email): + a=input("enter your email:") + b=len(a) + z=-1 + for i in a: + z+=1 + if i=='@': + x=a[z+1:b] + print(x) + else: + continue - # ** Create a function that grabs the email website domain from a string in the form: ** -# -# user@domain.com -# -# **So for example, passing "user@domain.com" would return: domain.com** + return None 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. ** - + s=input("enter your string:") + l=s.split() + for i in l: + if i=='dog': + print('true') + else: + continue + return None def countDog(st): - -# ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** + ctr=0 + s=input("enter your sring:") + l=s.split() + for i in l: + if i=='dog': + ctr=ctr+1 + else: + continue + print("dog occured ",ctr," times.") return None def lambdafunc(seq): + a=[] + b=int(input("how many elements do you want:")) + for i in range(b): + c=input("enter your word:") + a.append(c) + print(a) + s=[] + for j in a: + d=j + if d[0]=='s': + s.append(d) + else: + continue + print(s) + #or + a=[] + b=int(input("how many elements do you want:")) + for i in range(b): + c=input("enter your word:") + a.append(c) + f=filter(lambda a: a[0]=='s',seq) + print(list(f)) - # ** 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'] -# -# **should be filtered down to:** -# -# ['soup','salad'] - return None def caught_speeding(speed, is_birthday): + speed=int(input("enter the speed:")) + birthday=input("is it your birthday:") + if birthday=='yes': + if speed<=65: + print("no ticket") + elif speed>65 and speed<=85: + print("small ticket") + elif speed<85: + print("big ticket") + elif birthday=='no': + if speed<=60: + print("no ticket") + elif speed>60 and speed<=80: + print("small ticket") + elif speed<80: + print("big ticket") - -# **You are driving a little too fast, and a police officer stops you. Write a function -# to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". -# 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 @@ -124,51 +152,55 @@ def caught_speeding(speed, is_birthday): def create_arr_of_fives(): + a=[] + for i in range (10): + a.append(5) + print(a) - #### Create an array of 10 fives - #### Convert your output into list - #### e.g return list(arr) - return None def even_num(): - - ### Create an array of all the even integers from 10 to 50 - ### Convert your output into list - ### e.g return list(arr) - + a=[] + for i in range(10,51,2): + a.append(i) + print(a) + return None def create_matrix(): - - ### Create a 3x3 matrix with values ranging from 0 to 8 - ### Convert your output into list - ### e.g return (arr).tolist() - + mat=np.arrange(0,9).reshape(3,3) + print(mat) + return None -def linear_space(): + def linear_space(): + a=1/20 + b=[] + c=0 + for i in range(20): + c=c+a + b.append(c) + print(b) - ### Create an array of 20 linearly spaced points between 0 and 1 - ### Convert your output into list - ### e.g return list(arr) - return None def decimal_mat(): + a=0.01 + b=[] + c=0 + for i in range(100): + c=c+a + b.append(c) + print(b) - ### Create an array of size 10*10 consisting of numbers from 0.01 to 1 - ### Convert your output into list - ### e.g return (arr).tolist() - return None @@ -177,19 +209,31 @@ def slices_1(): # This is a given array arr = np.arange(1,26).reshape(5,5) - # array([[ 1, 2, 3, 4, 5], - # [ 6, 7, 8, 9, 10], - # [11, 12, 13, 14, 15], - # [16, 17, 18, 19, 20], - # [21, 22, 23, 24, 25]]) - - # Write a code to slice this given array - ### Convert your output into list - ### e.g return (arr).tolist() - # array([[12, 13, 14, 15], - # [17, 18, 19, 20], - # [22, 23, 24, 25]]) + a=([[ 1, 2, 3, 4, 5], + [ 6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25]]) + x=[] + b=[] + c=[] + d=0 + y=[] + for i in range (2,5): + d=d+1 + for j in range (1,5): + if d==1: + x.append(a[i][j]) + elif d==2: + b.append(a[i][j]) + elif d==3: + c.append(a[i][j]) + y.append(x) + y.append(b) + y.append(c) + print(y) + return None @@ -198,19 +242,29 @@ def slices_2(): # This is a given array arr = np.arange(1,26).reshape(5,5) - # array([[ 1, 2, 3, 4, 5], - # [ 6, 7, 8, 9, 10], - # [11, 12, 13, 14, 15], - # [16, 17, 18, 19, 20], - # [21, 22, 23, 24, 25]]) - - # Write a code to slice this given array - ### Convert your output into list - ### e.g return (arr).tolist() - # array([[ 2], - # [ 7], - # [12]]) - + a=([[ 1, 2, 3, 4, 5], + [ 6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25]]) + + x=[] + b=[] + c=[] + d=[] + y=0 + for i in range (0,3): + y=y+1 + b.append(a[i][1]) + elif y==2: + c.append(a[i][1]) + elif y==3: + d.append(a[i][1]) + x.append(b) + x.append(c) + x.append(d) + print(x) + return None @@ -219,18 +273,28 @@ def slices_3(): # This is a given array arr = np.arange(1,26).reshape(5,5) - # array([[ 1, 2, 3, 4, 5], - # [ 6, 7, 8, 9, 10], - # [11, 12, 13, 14, 15], - # [16, 17, 18, 19, 20], - # [21, 22, 23, 24, 25]]) - - # Write a code to slice this given array - ### Convert your output into list - ### e.g return (arr).tolist() - # array([[16, 17, 18, 19, 20], - # [21, 22, 23, 24, 25]]) - + a=([[ 1, 2, 3, 4, 5], + [ 6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25]]) + + x=[] + b=[] + c=[] + d=0 + y=[] + for i in range (3,5): + d=d+1 + for j in range (0,5): + if d==1: + x.append(a[i][j]) + elif d==2: + b.append(a[i][j]) + y.append(x) + y.append(b) + print(y) + return None