CM151: Lab -- Simple Programs


Lab Goal:

In this lab, we cover:

  1. Python Names,
  2. Statements,
  3. Advanced Output,
  4. Assignment Statements, and
  5. Definite Loops.

In this lab you may work in groups of 2 if you would like. However, I recommend that each person work on his/her own code (and submit their own code) to gain experience using Python. If working in a group, be sure that all code references both authors.

For this lab you will be asked to answer several questions and add code where ever you encounter "???". Save your solutions and modifications to a separate file that you will load to the moodle. The name of the file will be listed in each section.

Don't forget - you'll want to use idle3 and python3 for all your work.


Python Names (part1_names.py)

Below are possible names, one on each line. For each of the following possible names determine those that are valid. If the possible names is incorrect, explain why it is incorrect. In your part1_names.py file, assign a number to each of the valid variable names, one per line. If the name is invalid, add a comment to the code explaining what's wrong with it.

  • lafayette
  • lafayette college
  • lafayette_college
  • Lafayette_College
  • LafayetteCollege
  • Lafayette-College
  • LafayetteCollege@Easton

Expressions (part2_expressions.py)

In the following code replace the ??? and produce the output line : You are -3.33333333333

			a = 4
			b = ???
			c = 2.0
			e = ???
			f = 0.1
			g = (a - b) / (c - e)
			h = ???
			print (h + " are ", g)
		

Advanced Output (part3_advanced.py)

Recreate the following output using the print statement and the end functionality. There are hints provided in the shown output indicating how many print statements are used.

		Hi!  This line should be easy! Just use one print statement.
		But this should be more work and must consists of three print statements.
		You can also, add punctuation, with the end, allowing this line to use four prints!

Assignment Statements (part4_assign.py)

Replace the ??? with a single line of code, so the output below will be generated.

	a = 1
	b = 2
	c = 3
	print("A = ", a)
	print("B = ", b)
	print("C = ", c, end = "\n\n")
	???
	print("A = ", a)
	print("B = ", b)
	print("C = ", c, end = "\n\n")
	???
	print("A = ", a)
	print("B = ", b)
	print("C = ", c, end = "\n\n")
	???
	print("A = ", a)
	print("B = ", b)
	print("C = ", c, end = "\n\n")

Output:

	A =  1
	B =  2
	C =  3

	A =  3
	B =  1
	C =  2

	A =  2
	B =  3
	C =  1

	A =  1
	B =  2
	C =  3

Definite Loops (part5_loops.py)

Create the four definite loops that produced the following output, each using a single print statement. Note that at least the first three loops can use a range statement and do not need an explicit sequence.

				0 1 4 9 16 25 36 49 64
				1 3 5 7 9
				0 1 3 6 10 15
				-2 3 -1 4 0 5 1 6 2 7  
			

Extra Loopy

This section is an optional challenge. Nothing to submit here.

Using loops and two print statements, have Python print out the following:

			* * * * *
			* * * * *
			* * * * *
			* * * * *
			* * * * *
			

Submission:

Once you have your five programs working, upload them to Moodle.