2nd Semester Intro to Computer Science

Lab 1.03 - Printing & Variables

Part 1 - Printing Practice

Practice typing out some statements in the editor part of the IDE, then hit “Run” at the top of the screen:

Expression Expected Output Did anything unexpected happen?
print("1")    
print(1)    
print(1 + 2)    
print("1" + "2")    
print("this" + " " + "is" + " " + "a" + " " + "sentence" + ".")    

SNAP Flashback - Print Comparison

Code

Image

Part 2 - Variables Practice

1. In your Console

Type and run the following

animal = "dogs"
print(animal + " are really cool.")

In your Notebook

Respond to the following

  1. What happens?
  2. How would you make the program print out “cats are really cool” instead?

2. In your Console

Type and run the following code

print(dogs + " are cool.")

Continue In your Notebook

Respond to the following questions

  1. What output does this produce?
  2. Why does this happen?

3. In your Console

Rewrite the following Snap! Program in Python

snap_blocks_variables

Part 3 - Four Fours

The four fours challenge

Using four 4’s and any operations, try to write equations that have the numbers from 0 to 4 as the answer. You should use Python’s arithmetic operations:

  • + addition
  • - subtraction or negation
  • * multiplication
  • / division
  • ( ) parentheses for grouping
  • ** power

You may also use 44 or 4.4, which count as two fours, or .4, which counts as one four. For example, one solution for zero is:

print("Zero is", 44-44)

Can you find a different solution?

Here are what the results, but not the source code, will look like. (Note: answers may have trailing zeros if floating point arithmetic is used which is fine, i.e. 1 may be displayed as 1.0)

Zero is 0
One is 1
Two is 2
Three is 3
Four is 4

Bonus

Print the output below, but only using one line of code. Feel free to use online resources.

Wow!
This is on a new line!

Bonus 2

Can you find four fours for 5 to 10?

Edit this page Report an issue