From ae99d954383fb086d08ee0a6bcda9c111c31aaf7 Mon Sep 17 00:00:00 2001 From: gujashu <96698908+gujashu@users.noreply.github.com> Date: Mon, 7 Feb 2022 14:25:09 +0530 Subject: [PATCH] Update Python_Exercises.py --- Python_Exercises.py | 68 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..35c8049 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -7,6 +7,8 @@ def power(a,b): # ** What is 7 to the power of 4?** + c = a*a*a*a + print('The power of {} to {} is {}' .format(a,b,c)) return None @@ -19,6 +21,7 @@ def split_str(s): # s = "Hi there Sam!" # # **into a list. ** + s.split() return None @@ -33,6 +36,8 @@ def format(planet,diameter): # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. + print('The diameter of {var} is {D} kilometers' .format(var==planet,D==diameter)) + return None @@ -43,6 +48,9 @@ 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] + lst[3][1][2][0] return None @@ -52,6 +60,10 @@ 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']}]}]} + + d['k1'][3]['tricky'][3]['target'][3] return None @@ -60,7 +72,7 @@ def dictionary(d): def subjective(): # ** What is the main difference between a tuple and a list? ** -# Tuple is _______ +# Tuple is immutable where as list is mutable._______ return None @@ -74,6 +86,10 @@ def domainGet(email): # user@domain.com # # **So for example, passing "user@domain.com" would return: domain.com** + + + + return None @@ -81,6 +97,12 @@ def domainGet(email): 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. ** + + st = input("Enter the word here.") + if (st=='dog' or st=='DOG'): + print("TRUE") + else: + print("Sorry") return None @@ -88,6 +110,15 @@ def findDog(st): def countDog(st): # ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** + + st = "Dog is a very good animal Dog is dog because Dog is animal" + + a=0 + b="Dog" + for line in st: + if b in line.split(): + a += 1 + print(a) return None @@ -102,6 +133,10 @@ def lambdafunc(seq): # **should be filtered down to:** # # ['soup','salad'] + + seq = ['soup','dog','salad','cat','great'] + list(filter(lamda word: word[0]=='s', seq.split())) + return None @@ -114,6 +149,16 @@ 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. ** + + if (speed<=60): + print("No ticket") + if ( 61<=speed<=80): + print("Small Ticket") + if ( speed>=80): + print("Big ticket") + else(is-_birthday=='Yes"): + print("True"): + return None @@ -128,6 +173,8 @@ def create_arr_of_fives(): #### Create an array of 10 fives #### Convert your output into list #### e.g return list(arr) + ar = np.fives(5) + list(ar) return None @@ -138,6 +185,8 @@ 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) + ar = np.arrange[10,50,2] + list(ar) return None @@ -148,6 +197,9 @@ def create_matrix(): ### Create a 3x3 matrix with values ranging from 0 to 8 ### Convert your output into list ### e.g return (arr).tolist() + ar = np.arrange(9) + ar.reshape(3,3) + return (ar).tolist() return None @@ -158,6 +210,9 @@ def linear_space(): ### Create an array of 20 linearly spaced points between 0 and 1 ### Convert your output into list ### e.g return list(arr) + ar=np.linspace(0,1,20) + list(ar) + return None @@ -168,6 +223,9 @@ def decimal_mat(): ### 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() + ar=np.arrange(0.01,1).reshape(10,10) + return (ar).tolist() + return None @@ -190,6 +248,7 @@ def slices_1(): # [17, 18, 19, 20], # [22, 23, 24, 25]]) + return None @@ -210,6 +269,9 @@ def slices_2(): # array([[ 2], # [ 7], # [12]]) + arr2=(arr[0:3,1]) + return (arr2).tolist() + return None @@ -230,6 +292,10 @@ def slices_3(): ### e.g return (arr).tolist() # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) + arr2=(arr[0:3,4]) + + + return None