HW3-comp122-fall2023

.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

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
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)
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