From 1c6a81443f3362cac427827148c0d2ebac2a7580 Mon Sep 17 00:00:00 2001 From: Jaskaran Singh <96643707+JaskaranSingh-01@users.noreply.github.com> Date: Sun, 6 Feb 2022 23:46:24 +0530 Subject: [PATCH] Add files via upload --- Python_Exercise.py | 309 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 Python_Exercise.py diff --git a/Python_Exercise.py b/Python_Exercise.py new file mode 100644 index 0000000..0b63a47 --- /dev/null +++ b/Python_Exercise.py @@ -0,0 +1,309 @@ +# -*- coding: utf-8 -*- +"""Assignment_python1.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1JVYuKtWa6h9TqNEzRqlB_FEfjv0w8D-h +""" + +#@title Default title text +def power(a,b): + + + if b==0: + return 1 + elif a==0: + return 0 + elif b==1: + return a + else: + return a**b + + # ** What is 7 to the power of 4?** + +power(7,4) + +def split_str(s): + + # ** Split this string:** +# + s = "Hi there Sam!" + +# **into a list. ** + return s.split() + +split_str("Hi there Sam!") + +def format(planet,diameter): + +# ** Given the variables:** +# +# planet = "Earth" +# diameter = 12742 + +# ** Use .format() to print the following string: ** + print('The diameter of {} is {} kilometers'.format(planet,diameter)) +# The diameter of Earth is 12742 kilometers. + + return None + +format(planet="Earth",diameter=12742) + +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[3][1][2][0] + + return lst[3][1][2][0] + +indexing(lst=[1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]) + +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']}]}]} + + return d['k1'][3]['tricky'][3]['target'][3] + +# return None + +dictionary(d={'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}) + +def subjective(): + +# ** What is the main difference between a tuple and a list? ** + c = print('Tuple is immutable') + + return c + +subjective() + +def domainGet(email): + + # ** Create a function that grabs the email website domain from a string in the form: ** + + return email.split('@')[-1] + +domainGet('user@domain.com') + +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 'dog' in st.lower().split() + +findDog('Did a dog bite you?') + +def countDog(st): + +# ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** + mark=0 + for word in st.lower().split(): + if word == 'dog': + mark +=1 + + return mark + +countDog("The dog or domestic dog is a domesticated descendant of the wolf which is characterized by an upturning tail") + +# ** 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'] + + +def lambdafunc(seq): + seq = ['soup','dog','salad','cat','great'] + b= list(filter(lambda test: test[0] == "s",seq )) + print(b) + return None + +lambdafunc(seq = ['soup','dog','salad','cat','great']) + +# **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. ** + + +def caught_speeding(speed, is_birthday): + if is_birthday == True: + allowed_speed = speed + 5 + else: + allowed_speed = speed + + if allowed_speed >80: + return 'Big Ticket' + elif allowed_speed >60: + return 'Small Ticket' + else : + return 'No Ticket' + +caught_speeding(75,True) + +import numpy as np + + +def create_arr_of_fives(): + + #### Create an array of 10 fives + arr = np.ones(10)*5 + print("The array is: ") + print(arr) + #### Convert your output into list + #### e.g return list(arr) + print("The output list is: ") + return list(arr) + +create_arr_of_fives() + +import numpy as np + + +def even_num(): + + ### Create an array of all the even integers from 10 to 50 + arr = np.arange(10,51) + print("The array is: ") + print(arr) + ### Convert your output into list + ### e.g return list(arr) + print("The output list is: ") + return list(arr) + +even_num() + +import numpy as np + + +def create_matrix(): + + ### Create a 3x3 matrix with values ranging from 0 to 8 + arr = np.arange(9).reshape(3,3) + print("The array is: ") + print(arr) + ### Convert your output into list + ### e.g return (arr).tolist() + print("The output list is: ") + return (arr).tolist() + +create_matrix() + +import numpy as np + + +def linear_space(): + + ### Create an array of 20 linearly spaced points between 0 and 1 + arr = np.linspace(0,1) + print("The array is: ") + print(arr) + ### Convert your output into list + ### e.g return list(arr) + print("The output list is: ") + return list(arr) + +linear_space() + +import numpy as np + + +def decimal_mat(): + + ### Create an array of size 10*10 consisting of numbers from 0.01 to 1 + arr = np.arange(1,101).reshape(10,10)/100 + print("The array is: ") + print(arr) + ### Convert your output into list + ### e.g return (arr).tolist() + print("The output list is: ") + return (arr).tolist() + +decimal_mat() + +import numpy as np + + +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]]) + print("The sliced array is: ") + array = arr[2:,1:] + print(array) + print("The output list is: ") + return (array).tolist() + +slices_1() + +import numpy as np + + +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]]) + array = arr[:3,1:2] + print("The array is: ") + print(array) + + print("The output list is: ") + return (array).tolist() + +slices_2() + +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]]) + array = arr[3:5,:] + print("The sliced array is: ") + print(array) + print("The output list is: ") + + return (array).tolist() + +slices_3() \ No newline at end of file