Homework 1a: Writing Python | Introduction to Python Programming

 

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.

Math Practice

In [2]:

A 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 = 1

Once 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)
# >>> 2

We 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)
# >>> 6

What 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.

In [3]:
19260.93
In [4]:
Success!

Calculate the result of 215 divided by 6 and save it in a variable named 'q2', then print it out.

In [5]:
35.833333333333336
In [6]:
Success!

Calculate the result of 3 divided by 0.3 and save it in a variable named 'q3', then print it out.

In [7]:
10.0
In [8]:
Success!

Calculate the remainder of 215 divided by 6 and save it in a variable named 'q4', then print it out.

In [9]:
5
In [10]:
Success!

Calculate the value of 9 raised to the 12th power and save it in a variable named 'q5', then print it out.

In [11]:
282429536481
In [12]:
Success!

Cast 3.5 to an integer and save it in a variable named 'q6', then print it out.

In [14]:
3
In [15]:
Success!

Data Types Practice

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.

In [18]:
<class 'str'>
In [19]:
Success!

Calculate the data type of True and save it in a variable named 'q8', then print it out.

In [20]:
<class 'bool'>
In [21]:
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.

In [22]:
<class 'float'>
In [23]:
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.

In [24]:
1
In [25]:
Success!

String Practice

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!

In [26]:
James Brian Patrick
In [27]:
Success!

Make the following string correct, storing it in a variable named 'q12':
q12 = "4 % 2 = " + (4 % 2))

In [28]:
4 % 2 = 0
In [29]:
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."

In [30]:
Albert Einstein's best quote is "I have no special talent. I am only passionately curious."
In [31]:
Success!

Post a Comment

0 Comments