A201 Introduction to Programming 1

A201 Lab 2

Date: Tuesday, August 28, 2028.
Due Date: Tuesday, September 5, 2023.

Note. You can find more documentation on Python here:
https://docs.python.org/3/.

Ex. 1. a. Python console.

Make sure your computer is booted on Windows. Open the Python Console from the Start menu, Python 3.6, Python 3.6. You should see the prompt change to ">>>" which means that the Python interpreter has been launched.

Type

print("Hello world")
and then enter.

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.

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

b. Python as a calculator.

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.

Importing a library: type the following command:
from math import *

Try various combinations of mathematical functions, as for example cos(2.0/3*pi) or log(10).

You can close the console for now.

Ex. 2. Python Script.

Open the Python Idle application from the Start menu, Python 3.6. This also contains a Python console, but can also let you edit the code in a file and run it.

From the File menu, create a new file, then save it as lab1.py. Make sure to save it from time to time.

a. Writing and executing a small program.

Let's start with the first program seen in class:

# My first Python program
print("Hello world")

Copy this code to the file you created. Add another comment at the top of the file containing your name.

Run the script (or module or program) by going to the Run menu, then Run Module. In the future, you can shortcut it by hitting the F5 key instead. You should see the result in the Idle console.

Modify the text that is printed, for example, by saying that this is your first program, then run it again.

b. Adding variables.

Copy the following code discussed in class into your file lab1.py:

# A small program converting from degrees 
# Fahrenheit to Celsius
# C = (F - 32) * 5 / 9

print("Enter a degree number in Fahrenheit")
degreesF = float(input())
degreesC = (degreesF - 32) * 5 / 9
print("Converted to Celsius:", degreesC)

Add a similar piece of code after it where you do the opposite conversion, from Celsius to Fahrenheit. Input the degrees in Celsius, perform the conversion, then output the result. You can use the same variable as before.

Lab Submission

Upload the file lab1.py in Canvas, Assignments, Homework 1. You can wait until you finish the homework to upload both files at the same time.