PHYSICS 2235
Lab 3

Exercises
Assignment

Today we introduce the Python (think “Monty”, not the reptile) computing language. Python is an interpreted language. We shall see it is a powerful and generally user-friendly.

A useful link is
www.python.org. Note that we will be using Python version 3.4 in this course. You may find this tutorial useful.

Exercise 0.

Remember we created a hello.py file in our last class. Copy it into your working directory. You can run the lines inside the hello.py in python interactively. Simply type python3. This command opens an interactive Python session with Python version 3.
Python 3.4.5 (default, Dec 11 2017, 16:57:19) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

At the prompt, type print('Hello, world!').

To exit interactive mode, type CTRL-D or type quit() or exit() on the interpreter command line.

Exercise 1.

Start another code. Lets call it myinformation.py. Start this code by printing out
print('My name is : Fill your name')
print('My age is: Fill your age').
print('My street number is: Fill your street number')
Now run your code myinfo.py.

You will get an output

print('My age is: Fill your age').
                                 ^
SyntaxError: invalid syntax
Hint: What is the problem here? Where is the arrow pointing to?

After you solved the problem your output should be:

My name is : James Matthews
My age is: 18
My street number is: 1300 South

Now lets try to use a prompt. Write a code were you request from a user (you at this point) to enter the information from the terminal. using the prompt will result in the code waiting for a response before it continues.

name = input("Enter your name:")
age = input("Enter your age:")
street = input("Enter your street number:")
print('My name is', name)
print('My age is',age).
print('My street number is',street)
Here in each case you have to write your response on a single line and hit Enter key to proceed. Now change the first line of the code from
name = input("Enter your name:")
to
names = input("Enter your name:")

Run your code.

You must have gotten the NameError: name 'name' is not defined

Explain what happened?

Name = input("Enter your name:")
Run your code.

Now change it to

NAME = input("Enter your name:")
Run your code. Submit your error and explanation for all the variables (names, Name, NAME). Submit it in problem 0 in assignments. Call your file names.txt

Exercise 2.

Variable types: Name the following code myVariables.py
a = "My name is: "
b = 322
c = 51.1
d = 1 + 2j
Here a,b, c, and d are defined as variables. We stored the values "My name" , 322, 51.0, 1+2j in these variables.

try to print out one or all of these variables?

try to print the variable C instead of c. Did that work?

you should understand from Exercise 0 why not.

Note that the variables are case sensitive.

There are also variables that are reserved for python. Built in names can not be used as variables for example: print, input. print and input are two functions you have used already in this class ro print variables and use the prompt to enter variables.

These "variables" we invented their names come in four data types:

 
string : a bunch of characters surrounded by single or double quotes. 
integer: a number with no decimal point
float:  a number with decimal point
complex: a number written with j to indicated the imaginary part
create another string (call it a1). assign a1 the value James. Use a single or double quotes. Note that strings can be connected using the "+" operator: Try the following:
b. print ( a + a1).
c. print ( b + c ).
d. print ( a + b ).
c. print ( a + c ).
Do all the previous prints work?

Here the operation d fails because you are adding a number to a string. You can fix this by in this case by converting the number to a string. You can do that by using the function str.

b1 = str(b)
print (a +  b1)
Here instead of adding a ( a string) to b (an integer) [a failed operation that does not work in python] we converted b to a string in order for this operation to work. Python is quite particular about doing operations with the correct variable type.

Does it work now?

We just used str to converted a variable type from integer to string. In the same manner the function int converts a variable to integer while float converts a variable to a float.

Now assume you have the following code:

b = 322
b1 = str(b) 
c = 51.1
try to calculate b1 + c. Convert the correct variable using the correct function to get the proper value of the sum. Print out the sum and submit your code in problem 1. Call your code sum.py

Exercise 3.

Assume that you are trying to convert from centigrade (Tc) to Fahrenheit (Tf). You know the formula is
Tf = (9/5)*Tc + 32
In python.
>>> Tc = 40
>>> Tf = (9/5)*Tc + 32
>>> print (Tf)
104.0
Now do the conversion for several centigrade temperature values (45, 62, -10).


Recalculating for different Tc was a pain wasn't it.
From a programming perspective it would be much easier to write a code that would calculate the temperature for multiple Tc values. At this point it is useful to do this in a python file. Create a file called convert.py containing the following lines:
#! /usr/plocal/bin/python3


Tc = 40
ratio = 9/5
constant = 32
Tf = ratio*Tc + constant

print(Tf)
Now do the conversion for several centigrade temperature values (45, 62, -10).






You should submit the Assignment by the end of the class. Last chance to submit the Assignment is this Friday by noon.

Problem 0

Submit names.txt addressing the question in Exercise 1.

Problem 1.

submit sum.py addressing the question in Exercise 2.

Problem 2.

Write a script that calculates the area of a disk with a radius r= 100 cm.
Let your program require you to enter the value of r from the command line in units of cm. The output of your code should be the area in . Submit code area_disk.py .



Hint: Pay attention to your units.

Hint: Calculate the final answer on paper and compare to code result.

Hint: Don't forget to discuss/compare answers with fellow students.


Problem 3.

Write a script that calculates the mass of a sphere given a radius r and mass density according to the formula Assume to be 0.75 g/cm3.


Use the script to define the variable radius r = 10 mm. The output of your code should be the mass in g. Print out the mass. Submit code mass_sphere.py .

Hint: Pay attention to your units.

Hint: Calculate the final answer on paper and compare to code result.

Hint: Don't forget to discuss/compare answers with fellow students.

Back to main Physics 2235 page