lab01 Introduction Version 3.7
lab01 Introduction Version 3.7
This lab will introduce you to the basics of the Python environment IDLE. 1.2 Variables and Assignment Statements
WARNING : Python is case sensitive (i.e., capitalization matters) and relies on
proper indentation, so make sure you type things exactly as stated throughout Next, type the following:
the rest of this writeup. val = 5-34
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) The shell should display the current value of val (i.e., -29). Now type the fol-
[MSC v.1916 64 bit (AMD64)] on win32 lowing:
Type "help", "copyright", "credits" or "license()" for more information.
>>> val2
That last line (i.e, >>>) is the shell prompt. The Python shell (also known as the You’ll receive a message (in red) indicating that an error has occurred: the name
Python interpreter) is waiting for you to type in an expression for it to evaluate val2 has not been defined.
or a statement for it to execute. From the Shell menu, select Restart Shell . You should see a message in the
shell indicating that the shell has been restarted. (You can still see everything
you typed before, along with the shell’s responses, but their effects have been
1.1 Working at the Shell Prompt erased from the shell’s internal memory.)
To see this, evaluate val at the prompt. Whereas it used to have the value −29,
At the prompt, type the following expression (and then hit Return/Enter): the name val is no longer defined. M ore t o t he p oint: e verything t hat may
3 + 4 *5 - 6 have been defined in the previous incarnation of the shell has been wiped from
memory.
The shell displays 17, which is the result of evaluating the expression you typed.
Notice that the shell uses different colors to distinguish text you type from the
results it returns. 1.3 System Function for Output: print()
Next, type the following expression: Type the following at the prompt:
3 + 4 *5 # - 6 def hello (name):
print ("Hi", name)
This time, the result is 23: everything to the right of the hash mark (#) is consid- print ("Welcome to Python!")
ered to be a comment and is ignored by the shell.
Page 1
Lab 1: Introduction to IDLE
Note that, when you hit return after the first line, the shell automatically indents. num1
After typing the third line, hit return an additional time, and you should see the num2
prompt again. num3
You’ve just defined a function called hello. Type the following to the shell Note that, although you entered the same response for each input request, the
prompt, one at a time: shell seems to view the values of num1, num2, and num3 as being different. (We
hello ("Joule") will talk much more about this later this semester.)
hello (1.3)
hello ()
Note that, in the last case, you see a message indicating that hello requires an 2 The IDLE Editor
argument (i.e., a value passed into it) but one wasn’t provided. Once again, the
shell uses red text to indicate some sort of error. As we saw previously, when you restart the shell, any definitions (such as func-
tion definitions or variable assignments) get erased. Hence, it’s important to be
able to save definitions in a file for later use.
1.4 Getting User Input: input()
To open up an editting window, use the File menu and select New File . Be-
At the shell prompt, type the following: fore typing anything, take a look at bottom, righthand corner of the window
that just appeared: you’ll see the text Ln: 1 Col: 0. This text indicates the current
input ("What’s your name? ") location of the editting cursor. Although this information is unnecessary right
now, it will prove useful later this semester, as you try to debug programs.
Note that the shell displays the text What’s your name?, followed by a blink-
ing cursor: it appears to be waiting for something. In fact, it is: it’s waiting for a In the editing window, type the following (except actually include your name
user response. on the first line):
Type in your name, and hit return. You should see your response, this time in # Name: <insert your name here>
single quotes. (For example, if you typed Joule, then the shell will display # Lab 1: Intro to the Python IDE (IDLE)
’Joule’.)
Python’s input function is the primary way to take in user input. You can as- # the definition of a simple function
sign the user’s input directly to a name/variable, as follows (try it now): def hello (name):
result = input ("Your name? ") print ("Hi",name)
result print ("Welcome to Python!")
The function input always returns a string value (we’ll talk much more about
strings later). However, sometimes we want to treat user input as a number. We ##
can accomplish this by wrapping the input function inside another function # the main portion of the program
##
that performs the necessary conversion. Try each of the following, and enter 17
as the response for each query:
user = input ("What’s your name? ")
num1 = input ("Your favorite number? ") num = int (input ("What’s your favorite number? "))
num2 = int (input ("Your favorite number? "))
num3 = float (input ("Your favorite number? ")) for j in range (1,num+1):
Page 2
Lab 1: Introduction to IDLE
print ("\nAttempt ",j) Save a copy of your interactions with the shell as follows:
hello (user)
1. Click on the shell window to make sure it’s active.
print ("\n\nGoodbye for now!")
2. From the File menu, select Save As , and then save it as
lab1shell.txt in the same directory/folder in which you saved the
As before, the hash marks indicate comments. To save the file, head to the File program.
menu and select Save As , and then save it as lab1.py somewhere in your H:
drive (create a directory/folder where you will save all of your Python code for
this class).
3 What and How to Submit
Next, head to the Run menu and select Run Module . There are two possibili-
ties here: The purpose of this lab was to walk you through the IDLE environment. Sub-
mit both lab1.py (make sure your name’s included!) and lab1shell.txt
Possibility #1: A dialog box shows up indicating some sort of error happened. through Blackboard. These files should be submitted by the end of the lab ses-
What this means is that you likely mistyped something. sion.
Some messages are more helpful than others. If you can’t figure out
what needs to be fixed, flag me down, and I’ll be happy to help you fig-
ure out what’s wrong. Once you’ve resolved the problem, try again to
Run Module (repeat as necessary).
Possibility #2: The Python shell restarts, and the user is prompted to enter a
name. Enter your name, and answer the next prompt with a small but
positive integer (such as 3 or 5).
You’ve just successfully written your first Python program (or script, to use the
Python community’s standard vocabulary).
You can still interact with the shell and access the new definitions you’ve just
introduced. For example, type the following lines at the shell prompt:
user
num
hello
hello ("Yancey")
hello (user)
Note that user and num are names/variables that were introduced in your
script, and hello is the function defined at the top of the file.
You can use the function hello as many times as you’d like. However, if you
want to rerun the entire script, you will need to Run Module again. For now,
however, we’ll leave well enough alone.
Page 3