Due Date: Monday, April 12, 2021.
Open a terminal and type the command python.
You should see the prompt change to ">>>" which means that the Python interpreter has been launched.
At the prompt, type
print "Hello world"
and then enter. The message should be printed when you do that. This is a simple output statement.
Go back to the previous command using the up arrow key (like in the shell) and edit the string to make two strings from it for the two words and separate it by a comma. You can use the left-right arrows for the editing. Execute this command.
The result is the same, but this time we printed two strings instead of one. Python adds a space in between any two items that we print.
Define a variable called name and assign it your first name as a string.
For example, in my case I would write
name = "Dana"
Go back to the second printing of the Hello world and replace the second string with the variable name (without the quotes). Execute the edited command.
Type an arithmetic expression without even assigning it or printing it, as for example, 12.7 - 31/5, then execute it. Try different combinations of operators and constants.
Type the following command:
from math import *
This is the equivalent of the C++ #include statements.
Try various combinations of mathematical functions, as for example cos(2.0/3*pi) or log(10).
Type the following:
for i in range(4):
Note what happens after you press enter. The interpreter is now waiting for you to continue.
Start the next instruction with a tab, and then type:
print i
You will need to press enter twice to tell Python that the body of the loop has ended.
Repeat the procedure and add a comma at the end of the printing statement for i. Also in the for instructions, try a call to the range function with two integer parameters separated by a comma.
The comma at the end of a print statement will not end it in a new line. With two parameters, the function range uses them as lower and upper bounds for the range it creates. Exit the interpreter with Ctrl-D.
Open an emacs window to edit a buffer called lab11.py.
a. Start the script by specifying the Python interpreter:
#!/usr/bin/python
This is similar to what we did for bash scripts.
Follow that line with a comment containing your name and the lab number.
Just like for bash, each comment line must start with the "#" symbol.
Type the following instructions:
print "Stapler before dawn"
print "Waiting in perfect silence"
print "Dreams of loose paper"
Note: poem from virtualstapler.com, author C. Allen.
Save the file and return to the terminal. Run the script with the following command:
python lab11.py
You should see the poem printed out.
Still in the terminal, change the permission to the Python file to be able to execute it, and then run the script as
lab11.py
or
./lab11.py
if the first one doesn't work.
Let's store the strings in some variables instead of printing them out.
Replace the word 'print' in the 3 instructions with the names of 3 variables such as word1, word2, word3, followed by the = sign, to convert these printing statements into assignments.
These assignments will then also serve as declarations for the 3 variables.
Add a fourth instruction that prints the 3 variables separated by commas and execute the script to see the result.
The result should be almost the same as before, but the three lines we should be merged into one now.
To restore the end of line characters, let's store the entire poem in a variable by that name:
poem = word1 + "\n" + word2 + "\n" + word3
Note how the expression below uses the '+' to concatenate the strings.
Print this new variable instead.
Let's turn this code into a function.
Add the following line before the 5 instructions:
def words():
This declares a function called words with no parameters. All of the other instructions should be below this one to become its body.
Indent the 5 instructions further inside by one tab. Add two empty lines after the last one and delete the tab to end the function body.
Remember that the structure of the program is defined by indentation in Python.
After the function (on a non-indented line), add the following section to the code:
if __name__ == '__main__':
Note that the name and main symbols are enclosed by two underline symbols on each side.
Add a function call to the function words in this section, indented by a tab, just like you would in C++.
This section will be executed only if the script is executed as an independent program (the way we are using it) and not if it imported in another module.
Save the file and test it again to make sure that it works.
Create a GitHub account if you don't have one already. Go to github.com to create it.
Once you have an account, create a new repository called "Python Exercise". Edit the readme file and document the course name, semester, and lab number, along with your name. Then add the file lab11.py to this repository.
Edit the settings of the repository on the web. Go to the bottom of the settings page and change the visibility of the repository to make it public.
Upload to Canvas: The file lab11.py and the link to the repository you created on github.