Lab 1

pdf

School

New York University *

*We aren’t endorsed by this school

Course

1114

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

4

Uploaded by ChancellorDolphinMaster963

Report
CS-UY 1113 - Lab 1 January 27th, 2023 You will need Python 3 installed on your laptop for this lab. Ideally, you would be using Idle or Thonny as a development environment, which would include Python. If you need help with this, please connect to a teaching assistant or instructor ASAP! All lab work must be submitted to Gradescope before you leave the lab. You must try each problem at least once. It is your responsibility to remember to submit your work. Gradescope is odd in that when you submit one file, all previous submissions (are deleted). So be sure that your final submission includes all solution files. Problem 1 1. Start Thonny on your computer. 2. Select “New” from the “File” menu to create a new Python file. This will open a new, empty file editing window. 3. Name the file lab1_p1.py and save it somewhere where you can find it later. Note that the file has a .py suffix. The lab assistants will instruct you how to submit this file to Gradescope when you are finished. 4. We want the file to contain basic information about the assignment: your name, your course, the date, the name of the assignment. However, we want Python to ignore this information, as it isn’t code and it’s useful only for a human being reading your file (e.g. for your professor or TA). Such text to be ignored is called a comment. Write a comment at the top of the file like this, but replace "Pat McStudent" with your name (both first and last name) # Pat McStudent # CS - UY 1113 # 27 January 2023 # Lab 1 Notice that each line above begins with a pound-sign (#) to indicate that it is a comment. Alternatively, you can write a comment like this: """ Pat McStudent CS - UY 1113 27 January 2023 Lab 1 """ 1
Notice that the comment begins with three quotation marks in a row ( """ ) and continues until the next three quotation marks in a row. Both of the above two syntaxes (using # or using """ ) are equivalent. Text that is not part of a comment will be processed normally by Python and must obey the usual rules of Python syntax. In the future, all submitted code for labs should begin with such a comment header. Note that you may lose points in a problem if your solution file does not include the comment header. 5. Below the header, in this file, enter the following statement which will print a nice greeting. Be sure to provide your name in the part where it says "<your name>" , but replace the <your name> with your name (leaving the double quotes). print("Hello Professor DePasquale, my name is", "<your name>") print() print("In this class, I understand that I...") print("* can work on labs with another student,") print("* must work on all other graded work without outside help, and") print("that if I am caught ", end="") print("in violation of the Student Code") print("of Conduct, I will be subject to disciplinary action.") 6. Finally, before you upload to Gradescope, test the program by running it. Be sure that the output your program produces is as shown below (including capitalization, spacing and the comma!), but of course with your given and family name present. Hello Professor DePasquale, my name is <your name> In this class, I understand that I... * can work on labs with another student, * must work on homework without outside help, and that if I am caught in violation of the Student Code of Conduct, I will be subject to disciplinary action. 2
Problem 2 Open a new file on Thonny and name it lab1_p2.py . In this file, enter the program below. Be sure to add the header comments as you did in Problem 1 (above). Be sure to check your output before submitting to Gradescope! Is the output correct? Be sure to check your spelling and use of space characters. This will impact your auto-grader test results. Get accustomed to being precise now, you are engineers after all. The details matter! import datetime founding_date = datetime.date(1831, 4, 18) today = datetime.date(2023, 1, 27) time_difference = today - founding_date age = time_difference.days print("Today is", today, "and NYU is", age, "days old.") Problem 3 Open a new file on Thonny and name it lab1_p3.py . In this file, enter the program below. Be sure to add the header comments as you did in Problem 1 (above). Be sure to check your output before submitting to Gradescope! # NYU Washington Square: 40.724663768 -73.990329372 # NYU Shanghai: 31.22499 121.53376 from math import sin, cos, sqrt, atan2, radians # approximate radius of earth in km R = 6373.0 lat1 = radians(40.724663768) lon1 = radians(-73.990329372) lat2 = radians(31.22499) lon2 = radians(121.53376) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) distance = R * c print("The distance (in km) from NYU - NYC and ", end="") print("NYU - Shanghai is:", distance) 3
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
Problem 4 Open a new file on Thonny and name it lab1_p4.py . In this file, enter the program below. Be sure to add the header comments as you did in Problem 1 (above). Be sure to check your output before submitting to Gradescope! # Python program which performs simple arithmetic operations number_one = 29 number_two = 13 # adding the given two numbers add_sum = number_one+number_two # printing the result of sum of two numbers print("Adding the given two numbers", number_one, "+", number_two, "=", add_sum) # subtracting the given two numbers diff_num = number_one-number_two # printing the result of difference of two numbers print("Subtracting the given two numbers", number_one, "-", number_two, "=", diff_num) # multiply the given two numbers prod_result = number_one*number_two # printing the result of product of two numbers print("Multiplying the given two numbers", number_one, "*", number_two, "=", prod_result) # dividing the given two numbers division_result = number_one/number_two # printing the result of quotient of two numbers print("Dividing the given two numbers", number_one, "/", number_two, "=", division_result) 4