$30
CMPSC 101
Introduction to Programming in Python
Homework 1 – Basic input/output and computation
Instructions:
Write/type the answers for questions 1-5 (no python code necessary).
For questions 6-8 write Python scripts (.py files). Use variables where necessary and give
meaningful names to variables. All script files should have a comment block at the top indicating
your name section and collaborators. Provide screenshots of the code and output in the solution.
Upload a .pdf file containing your solution along with your .py files.
Any solutions done on paper should be scanned to .pdf in good quality using CamScanner/Office
Lens/iScanner.
Collaboration is allowed for homework. However, you must acknowledge your collaborators
(check syllabus for details on collaboration policy). Individual submissions are required even if
you collaborate.
Full credit will be given only if all instructions are adhered to.
Problem 1. Indicate whether the following variables are valid or invalid: [5 points]
(a) my name
(b) new_sum
(c) 21guns
(d) simon&garfunkel
(e) _total
Problem 2. What will be the output of the following code? [5 points]
total = 99
print(“Total = “, “total”)
Problem 3. For the given assignment statements, what will be the Python data type of the
variables? [5 points]
(a) val1 = 20.00
(b) val2 = 5
(c) val3 = “10”
(d) val4 = 1.618
(e) val5 = “hello”
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
Problem 4: Write python statements for the following arithmetic operations: [10 points]
(a) Add 5 to variable b and assign the result to variable a
(b) Subtract 10 from variable b and assign the result to variable a
(c) Multiply variable b by 7 and assign the result to variable a
(d) Divide variable b by 10 and assign the result to variable a
(e) Increment the value in variable a by 5
Problem 5: Choose the correct answer for the following questions: [10 points]
1. Python is a(n):
A. Assembly language
B. Machine language
C. High-level language
D. Operating System
2. The identifiers myvar and myVar, when used in the same python program refer to the
same variable.
A. True
B. False
3. What will be the output of the following program, if you provide 1, 2 and 3 as inputs in
separate lines?
print(“Enter three numbers: ”)
num1 = eval(input())
num2 = eval(input())
num3 = eval(input())
avg = (num1 + num2 + num3) / 3
print(avg)
A. 1
B. 2
C. 2.0
D. <Error
4. What will be the output of the following code?
x = 1
x = x + 2.5
print(x)
A. 1
B. 3
C. 3.5
D. <Error
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
5. What will be the output of the following code?
a = 3
b = 11
b = a
a = b
print(a,b)
A. 3 11
B. 11 3
C. 3 3
D. 11 11
Problem 6. Write a program to calculate perimeter of a circle using the formula: 𝑝𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 =
2 × 𝑃𝐼 × 𝑟𝑎𝑑𝑖𝑢𝑠. The program should prompt the user to enter the radius of the circle. Use PI
= 3.14. Make sure your code works for any given radius. [20 points]
Sample program output:
Enter radius of circle: 50
Perimeter = 314.0
Problem 7: Write a program to convert temperature from Celsius to Fahrenheit scale using the
formula: 𝐹 =
9
5
𝐶 + 32. Prompt the user to enter the temperature in degree Celsius and display
the equivalent temperature in degree Fahrenheit. Make sure your code works for any given
temperature value. [20 points]
Sample program output:
Enter temperature in degree Celsius: 23.6
23.5 C is equivalent to 74.48 F
Problem 8: Write a program to calculate the time taken by a car to reach from one city to another.
Prompt the user to enter the names of the two cities, distance between the two cities in miles
(mention the names of the cities), and average speed of the car (in miles/hour). Display the
expected time required (in hours) to travel from the source to the destination city. Make sure
your code works for any given distance and speed. [25 points]
Sample program output:
Enter source city: State College
Enter destination city: New York
Enter distance between State College and New York (miles): 240
Enter average speed of the car (mph): 60
Expected time required to travel from State College to New York is 4.0 hours
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
The following files should be uploaded on Canvas:
1. hw1_solution.pdf
This file should contain answers for questions 1-5 and screenshots of code and output for
questions 6-8.
2. q6.py
This file should have the code for problem 6, and should be runnable in Python3.
3. q7.py
This file should have the code for problem 7, and should be runnable in Python3.
4. q8.py
This file should have the code for problem 8, and should be runnable in Python3.