Module 2: A few more getting-started examples | CMSC 105 Elementary Programming - Fall 2024

Module 2: A few more getting-started examples

Note: Create a text file called module2.txt where you will store you answers to exercise questions. The questions that are not related to changing code. You will submit this file on Blackboard along with your code.

Objectives

By the end of this module you will be able to:

Comments

A comment is like a note-to-self that you include directly in a program as a way to explain something to yourself for later, or to someone else who reads your program.

# This is a comment
    # This is one too, but not recommended 

print("Something")      # And so is this

Let’s explain:


Exercise 1: Write up the above in a file called my_comments.py. Fix the second comment to start at the beginning of the line, and add an entirely new comment line of your own. Attach this file to the Blackboard module 2 assignment (and do this for every exercise in the future that involves a program).


Sometimes one needs a comment to spill over multiple lines, as in

# I wrote this program at midnight
20 seconds before the deadline
print("Something") 

Notice the missing # in the second line of the comment.


Exercise 2: Write up the above in a file called comment_error.py. What is the error displayed when you try to run the program? Write the error in your module2.txt file.


Whitespace

Consider the following program:

print   (  "Hello World!"     ) 

Notice the spaces inserted in various places.


Exercise 3: Write up the above in whitespace_example.py. Does the program run? Write your answer in module2.txt file. Remember: if it’s not clear where to write your answer, write it in your module text file (for this module that’s module2.txt).


Consider this variation

  print("Hello World!") 

(Two spaces before print).


Exercise 4: Write up the above in whitespace_example2.py. What is the error produced? Write your answer in module2.txt.


Finally, look at:

print("Hello     World!") 

(Extra spaces between Hello and World.)


Exercise 5: Write up the above in whitespace_example3.py. What is printed out? Write your answer in module2.txt.


Let’s point out a few things:

Strings

A string in Python is a sequence of letters, digits, or symbols (like $ or *) surrounded by either

Note:

First, note that we can use single or double quotes for different strings in the same program:

print('Hello')
print("World!") 

Exercise 6: Confirm that Hello and World! get printed on two lines by writing the above in my_string_example.py.


A print statement prints the string within parenthesis and then goes to the next line of output, which is why we see World! on the next line.

To keep printing on the same line:

print("Hello", end=' ')
print("World!")

Exercise 7: Confirm by writing the above in my_string_example2.py.


We’ll now go the other way and have a single string itself contain a directive to spill over to the next line.

print('Hello\nWorld!') 

Notice the backslash \ followed by n inside the string: ‘Hello\nWorld!’


Exercise 8 Write up the above my_string_example3.py. What is the output? Write your answer in module2.txt.


Strings can embed special so-called escape sequences that begin with backslash.

This will give us one way to print a quote:

print('My friend\'s friend\'s dog\'s friend')

Another way is to use one set of quotes to delimit the string that are different from the ones used within:

print('My friend\'s friend\'s dog\'s friend')
print("bit my friend's dog's ankle")
print('who yelped "owww"')

How does one print a backslash itself? By using a double backslash:

print("The backslash character, \\, is less intimidating now")

Exercise 9: Write a program called practice_escaping.py that prints out

  "    "   \\\
  "    "    \    
  """"""    \    
  "    "    \    
  "    "   \\\  

Another use of backslash: to make long strings

print("An Ogden Nash poem:")
print("The camel has a single hump; 
The dromedary, two; 
Or else the other way around. 
I’m never sure. Are you?")

Exercise 10: Write a program called my_string_example4.py with the above program and run it. What is the error you observe? Write your answer in module2.txt.


To spread a single string over multiple lines, one uses a triple quote as in:

print("An Ogden Nash poem:")
print('''The camel has a single hump; 
The dromedary, two; 
Or else the other way around. 
I’m never sure. Are you?''')

Exercise 11: Write a program called my_string_example5.py with a 5-line limerick.


Empty strings:

Case sensitivity

What if we had used uppercase P instead of lowercase p in print?

Print('Hello World!')

Exercise 12: Write up the above program in case_error.py and run it. What is the error you get? Write your answer in module1.txt.


What if we changed the case inside a string?

print('helLo WoRLd!')

Exercise 13: Write up the above program in my_string_example6.py and run it to see if it works.


Python is case sensitive but strings are like data inside programs, which means they can be whatever we like.