PHYSICS 2235

Lab 5

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

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

#! /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

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-1We could accomplish the same thing with the

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.

python3 prime.py Enter n: 20where [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