PHYSICS 2235
Lab 5

Exercises
Assignment

tutorial.

Exercise 0.

Comparison operators

Comparison operator either return True or False. This will be particularly useful in while, conditional statements.
> Greater Than
< Less Than
== Equal 
!= Not Equal
>= Greater than or Equal to 
<= Less than or Equal 
start an emacs editor and create the file comp-ope.py to your directory. You will notice two variable x and y. Each is assigned a number. You will also notice print statements that include the comparison operators just mentioned. run you program. Now try to add "Equal to" and "Not equal to" operators and print if that was true or false for this example. Change the values of the variables y and x and see how the results change?

Explore what the following operands do.
x += y
x -= y
x *= y
x /= y

Simply add these lines one at a time and print (x). You can do this within the comp-ope.py file.

Logical operators

and (&) True if both operands are true
or (|) True if either operands are true
start an emacs editor and create the file log-ope.py to your directory and run it.

Exercise 1.

The following is a code that computes the real roots of the quadratic equation. Copy it to your directory. Lets call it quadratic.py.
#! /usr/local/bin/python3
import math
import sys
# Compute the real roots of the quadratic equation 

# Ask the user for a, b, and c and get them 
print("Solving the quadratic equation ax^2 + bx + c")
a, b, c = eval(input("Enter a, b, c "))

# Compute the "discriminant"
d = b*b - 4*a*c

# Get the sign of d 
signd = 1

if (d < 0):
    signd = -1
else:
    signd = 1

if (signd > 0): 
    x1 = -b + math.sqrt(d)/2*a
    x2 = -b - math.sqrt(d)/2*a
    # 1,5,6
    print("The roots are", x1, " ", x2)

else:
    # a=4, b=2, c=4
    print("The roots are complex")
    sys.exit(1)
Lets go through it line by line
  • python3
  • import
  • hash tag
  • input list
  • condition statement
  • arithmatic operation
  • exiting
  • Exercise 2.

    Last class we learned about for loop and the range function. To quickly re-cap.
  • for i in range(N): statement else: statement
  • range(N) = 0,1,2,3,4,..,N-1
    range(8,N) = 8,9,10,11,...,N-1
    range(8,N,2) = 8,10,12,14,...,N-1
    
    We could accomplish the same thing with the while loop.
  • i=0 # initialize i while i < N: statement i = i + 1 #increment


  • Here the loops is executed as long as the condition after the while loop is true. for example try the following lines and run your code:
    i=0 # initialize i 
    while (i < 5):
        print (i) 
        i = i + 1 #increment
    


    Now change the initialized value of i to 15.
    i=15 # initialize i 
    while (i < 5):
        print (i) 
        i = i + 1 #increment
    


    Would This loop will not print out anything?

    This is because the condition after the while loop is now false. The while loop will only run if the condition is true.

    Important: Note that you need to update the counter yourself (referred to as i in the next example).


    Note that here we have to take care to place initialization and incremental statements in the correct place. And it takes three statements to do the job of one for statement.

    Also note that the loop can go indefinitely as long as the statement after the while loop is true. For example:
    i=0 # initialize i 
    while (i < 5):
        print (i) 
    
    or
    i=15 # initialize i 
    while (True):
        print (i) 
        i = i + 1 #increment
    


    For those reasons the for loop is preferable unless we don't know the number of steps needed to solve your calculation.

    Assignment due next Monday by noon

    Problem 0.

  • In Exercise 1 you have made changes to fix the code. Submit your fixed version of the code .
  • Add a condition to the code to exit gracefully would the value of a or c be equals to 0. if a or c is zero the module should not output any root values neither. Submit these changes to exercise one in a file named quadratic.py.

    Problem 1.

    Write a python file that will let you know if your number n is even or odd. Where n is an integer command-line argument. Submit a file named even_odd.py. The file should print out if the number entered is even or odd.

    Problem 2.

    Write a python script prime.py that finds and prints to standard output all prime numbers greater than or equal to 2 and less than n, where n is an integer command-line argument. To run the code.
    python3 prime.py
    Enter n: 20
    
    where [n] is an arbitrary integer entered by the user.

    The output should look something like:
    the primes less than or equal to 20  are:
    2
    3
    5
    7
    11
    13
    17
    19
    ...
    


    Back to main Physics 2235 page