HW3-comp122-fall2023.ipynb - Colaboratory

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

9

Uploaded by DeaconBat3708

Report
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 1/9 HW 3 COMP 122 Fall 2023 1) Write a function named sphere_volume_area that takes a number r and returns the volume and area of a sphere. Hint: Volume of sphere = 4/3(pi x r^3) and Area of sphere is 4 pi r ^2 import math as mt r = float(input('Enter radius')) def sphere_volume_area(r): pi = mt.pi vol_sphere = (4/3)*(pi * r**2) area_sphere = 4 * pi * r**2 return vol_sphere, area_sphere sphere_volume_area(r) Enter radius3 (37.69911184307752, 113.09733552923255) The ¦nd() method returns the index of ¦rst occurrence of the substring (if found). If not found, it returns -1. Python String ¦nd() message = 'Python is a fun programming language' # check the following indexes print("the index of the first fun is ", message.find('fun')) print("the index of the first 'y' is ", message.find('y')) print("the index of the first 'P' is ", message.find('P')) print("there is no 'k' in the message string therefore the output is ", message.find('k'))
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 2/9 the index of the first fun is 12 the index of the first 'y' is 1 the index of the first 'P' is 0 there is no 'k' in the message string therefore the output is -1 2) Given a variable, x, that stores the value of any decimal number, write Python code that prints out the nearest whole number to x. For example, for x = 2.1345 output should be 2 and for x = 2.781 the output should be 3. NOTE: You cannot use round function #your code here x = (float(input('Enter decimal number'))) if x > 0: nearest_whole_number = int(x + 0.5) else: nearest_whole_number = int(x - 0.5) print(nearest_whole_number) Enter decimal number3.5 4 start and end values are Optional. start: asking python where to start the search. Default is 0 end: asking python where to end the search. Default is to the end of the string More on String ¦nd() method ---- string.¦nd(value, start, end)
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 3/9 message = 'Python is a fun programming language' # check the following indexes print("the index of the first 'o' is ", message.find('o')) print("the index of the second 'o' is ", message.find('o',5)) print("length of message string is ", len(message)) print("is there any more 'o' after index 18: ", message.find('o',19,36)) the index of the first 'o' is 4 the index of the second 'o' is 18 length of message string is 36 is there any more 'o' after index 18: -1 3) Assume mystring is a variable that holds a string. Write Python code that prints out the position of the second occurrence of 'python' in mysting, or -1 if it does not occur at least twice. mystring = "I love python that loves python" print ("the index of the first 'python' is ", mystring.find('python')) #it should print 25. Add your code in (...) print ("the index of the second 'python' is ", mystring.find('python', 8)) #it should print -1, for the following text. Add your code in (...) mystring = "I love a python who loves me" print(len(mystring)) print ("is there any more 'python' after index 25: ", mystring.find('python', 25, 28)) the index of the first 'python' is 7 the index of the second 'python' is 25 28 is there any more 'python' after index 25: -1
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 4/9 4) Given the variables s and t de¦ned as: s = 'Comp122' t = 'myPython' write Python code that prints out Python122 without using any quote characters in your code. s = 'Comp122' t = 'myPython' print(t[2:9] + s[4:]) Python122 5) Write Python code that assigns to the variable url a string that is the value of the ¦rst URL that appears in a link tag in the string page.Your code should print http://skylinecollege.edu Extracting Links #page = contents of a web page on my.smccd.edu page = ('<div class="col-xs-12 col-sm-3"><a href="http://skylinecollege.edu"><img ' 'src="images/Skyline_Logo_color.png" alt="Skyline College Logo"></a> </div>' ) page.find('href') start = page.find('"', page.find('href')) + 1 end = page.find('"', start) url = page[start:end] print(url)
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 5/9 http://skylinecollege.edu 6) Write a function named true_len that takes a word and returns the number of letters in the given word. word = input('Enter word') def true_len(word): count = 0 for i in word: count = count + 1 return(len(word)) true_len(word) Enter wordgiraffe 7 7) Write a function names my_ns that takes an integer (n) and computes the value of n+nn+nnn+nnnn. for example, my_ns(4) should return 4936 n = int(input('Enter value')) def my_ns(n): nn = n * 11 nnn = n * 111 nnnn = n * 1111 print(n + nn + nnn + nnnn) my_ns(n) Enter value4 4936
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 6/9 Python Conditions and If statements Python supports the usual logical conditions from mathematics: Equals: a == b Not Equals: a != b Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to: a >= b These conditions can be used in several ways, most commonly in "if statements" and loops. An "if statement" is written by using the if keyword. Python if ... else x = 10 y = 200 if x > y: print("x is greater than y") The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition". elif x = 10 y = 200 if x > y:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 7/9 print("x is greater than y") elif x < y: print("y is greater than x") y is greater than x The else keyword catches anything which isn't caught by the preceding conditions. else x = 10 y = 200 if x > y: print("x is greater than y") elif x < y: print("y is greater than x") else: print("x and y are equal") y is greater than x 8) Write a function named even_OR_odd that takes an integer and Check if the number is Odd or Even
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 8/9 number = int(input('Enter value')) def even_OR_odd(number): if number % 2 == 0: print('even') else: print('odd') even_OR_odd(number) Enter value3 odd 9) Write a function named sum_or_zero that takes three integers and returns the sum of the three integers. However, if two values are equal sum will be zero. a = int(input('Enter value')) b = int(input('Enter value')) c = int(input('Enter value')) integer = a, b, c def sum_or_zero(integer): if a == b or b == c or a == c: print(0) else: print(a + b + c) sum_or_zero(integer) Enter value1 Enter value1 Enter value3 0
9/19/23, 9:49 PM HW3-comp122-fall2023.ipynb - Colaboratory https://colab.research.google.com/drive/14HlMiTuTJXHim_PDNdNE-axVRCs1r9Py#scrollTo=LjyZKy-fbtF_&printMode=true 9/9 0s completed at 9:49 PM Colab paid products - Cancel contracts here 10) Write a function named only_int that takes two opjects. Your function should add the two objects if both objects are an integer type. If there are not the int type, your code should return False # object_1 = () # object_2 = def only_int(object_1, object_2): if type(object_1) is int and type(object_2) is int: return object_1 + object_2 else: return False print(only_int(1, 2)) print(only_int("string", "not an int")) 3 False
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help