Introduction to Python Programming
University of Pennsylvania
Homework 1a : Instructions
Homework 1a: Writing Python
This assignment is designed to give you practice writing code and applying lessons and topics for the current module.
This assignment deals with the following topics:
Math
Data types
Strings
The Assignment
This assignment is designed to give you practice writing Python and using Jupyter Notebook.
############################################################ EXECUTE THIS CELL BEFORE YOU TEST YOUR SOLUTIONS ############################################################import impsol = imp.load_compiled("sol", "./sol.py")from nose.tools import assert_equalA note on variable initialization
Variable initialization refers to the act of declaring a variable (giving it a name) and assigning it a value. A variable's name is declared on the left side of the = and its value on the right side. This can be done in a single line of code, like the below. Note that a variable's name may not contain spaces or special characters, with the exception of the underscore _.
my_variable = 1Once initialized, referring to my_variable in subsequent lines of code will actually refer to my_variable's value, which has been set to 1. However, as their name implies, variables can have changing values. For example, we might do something like the below:
my_variable = 1
print(my_variable) # This will print out the value of my_variable
# >>> 1
my_variable = 1 * 2 # We've now changed the value of my_variable to be (1 * 2) = 2
print(my_variable)
# >>> 2We can also reference a variable's current value in it's own re-assignment! This can be done if the variable has already been assigned a value previously. For example:
my_variable = my_variable * 3
print(my_variable)
# >>> 6What the above line is doing is setting the value of my_variable equal to the current value of my_variable, which we set to 2, multiplied by 3. This ends up being: my_variable = 2 * 3 = 6
Calculate the result of 3.93 multiplied by 4901 and save it in a variable named 'q1', then print it out.
# your code hereq1=3.93*4901print(q1)19260.93
############################# TEST YOUR SOLUTION ############################## Note: All test cases are read-onlyassert_equal(q1, sol.q1)print("Success!")Success!
Calculate the result of 215 divided by 6 and save it in a variable named 'q2', then print it out.
# your code hereq2=215/6print(q2)35.833333333333336
############################# TEST YOUR SOLUTION #############################assert_equal(q2, sol.q2)print("Success!")Success!
Calculate the result of 3 divided by 0.3 and save it in a variable named 'q3', then print it out.
# your code hereq3=3/0.3print(q3)10.0
############################# TEST YOUR SOLUTION #############################assert_equal(q3, sol.q3)print("Success!")Success!
Calculate the remainder of 215 divided by 6 and save it in a variable named 'q4', then print it out.
# your code hereq4=215%6print(q4)5
############################# TEST YOUR SOLUTION #############################assert_equal(q4, sol.q4)print("Success!")Success!
Calculate the value of 9 raised to the 12th power and save it in a variable named 'q5', then print it out.
# your code hereq5=9**12print(q5)282429536481
############################# TEST YOUR SOLUTION #############################assert_equal(q5, sol.q5)print("Success!")Success!
Cast 3.5 to an integer and save it in a variable named 'q6', then print it out.
# your code hereq6=int(3.5)print(q6)3
############################# TEST YOUR SOLUTION #############################assert_equal(q6, sol.q6)print("Success!")Success!
Calculate the data type of "False" (notice the quotes around the word False!) and save it in a variable named 'q7', then print it out.
# your code hereq7=type("False")print(q7)<class 'str'>
############################# TEST YOUR SOLUTION #############################assert_equal(q7, sol.q7)print("Success!")Success!
Calculate the data type of True and save it in a variable named 'q8', then print it out.
# your code hereq8=type(True)print(q8)<class 'bool'>
############################# TEST YOUR SOLUTION #############################assert_equal(q8, sol.q8)print("Success!")Success!
Calculate the data type of the result of 1000 divided by 10 and save it in a variable named 'q9', then print it out.
# your code hereq9=type(1000/10)print(q9)<class 'float'>
############################# TEST YOUR SOLUTION #############################assert_equal(q9, sol.q9)print("Success!")Success!
Cast the value of 6.3 divided by 3.8 to an integer. Save it in a variable named 'q10', then print it out.
# your code hereq10=int(6.3/3.8)print(q10)1
############################# TEST YOUR SOLUTION #############################assert_equal(q10, sol.q10)print("Success!")Success!
Concatenate the strings 'James', 'Brian', and 'Patrick' - store the result in a variable called 'q11', then print it out. Make sure to add a single empty space between the names!
# your code herea='James 'b='Brian 'c='Patrick'q11=a+b+cprint(q11)James Brian Patrick
############################# TEST YOUR SOLUTION #############################assert_equal(q11, sol.q11)print("Success!")Success!
Make the following string correct, storing it in a variable named 'q12':q12 = "4 % 2 = " + (4 % 2))
# your code hereq12 = "4 % 2 = " + str(4 % 2)print(q12)4 % 2 = 0
############################# TEST YOUR SOLUTION #############################assert_equal(q12, sol.q12)print("Success!")Success!
Save the following quote, (including the double quotes), in a variable called 'q13', then print it:
Albert Einstein's best quote is "I have no special talent. I am only passionately curious."
# your code hereq13=("Albert Einstein's best quote is \"I have no special talent. I am only passionately curious.\"")print(q13)Albert Einstein's best quote is "I have no special talent. I am only passionately curious."
############################# TEST YOUR SOLUTION #############################assert_equal(q13, sol.q13)print("Success!")Success!
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.