Você está na página 1de 12

Introduction to Python

Worksheet for LESSON 1


Starting Python
Open up Python (Start > IT > Python)

The development environment we will use to write our Python programs is


called IDLE. IDLE is Pythons Integrated Development and Learning
Environment. It is software that makes it easy to write Python programs. We
will be using IDLE to type in our programs and run them.

Figure 1.1: The IDLE program's interactive shell on Windows XP.

IDLE has two main window types, the Shell window and the Editor window. It
is possible to have multiple editor windows simultaneously. The window that
appears when you first run IDLE is called the interactive shell. A shell is a
program that lets you type instructions into the computer.
The Python shell lets you type Python instructions, and the shell sends these
instructions to software called the Python interpreter to perform. We can type
Python instructions into the shell and, because the shell is interactive, the
computer will read our instructions and respond in some way. (Ideally in a way
that we expect but that will depend on whether we write the correct
instructions.)

TASK 1 ARITHMETIC
CALCULATIONS
Type the below expression in IDLE and press
enter:
2+2
Write down the response from the computer below:

Now try the following expressions and write


down your responses accordingly:
8-4
6+10
10/5
3**2
Were the answers the same as the Maths equivalent?
Now try these:
addition = 72 + 23
subtraction = 108 - 204
multiplication = 108 * 0.5
division = 108 / 9

TASK 2 SETTING VARIABLES


As mentioned earlier Variables are locations we create that can
be used to store data in. Its just like having a box to hold
items in e.g.
Typing myBag = 4 will create a variable called myBag and
store the number 4 in it.
Type in these lines of code exactly as the appear and write
down what happens
myBag = 4
print
myBag
myBag + 2
myBag* 5
myBag = 8
print
myBag
print
mybag
For the last line you should have got an error. This is because
Python is cAsE sEnSiTiVe which means that the program
doesnt treat myBag as being the same as mybag!
Predict what will happen if you type these lines of code into
Python
myBag = 8
print myBag
myBag =myBag
+1
print myBag
myBag =myBag +
myBag
print myBag
apples = 2
pears =3
myBag = apples

+ pears
print myBag
To show what is currently being stored in a Variable we need to
use the PRINT command which will print the data (on to the
computer screen)

TASK 2.0 REASSIGNING


VARIABLES
Now you know how to use variables to store values.
Say my_int = 7. You can change the value of a variable by
"reassigning" it, like this:
my_int is set to 7 below. What do you think will happen if we
reset it to 3 and print the result?
my_int = 3

Try it and see! Change the value of my_int from 7 to 3 in the


editor.
Here's some code that will print my_int:
print my_int
All the numbers used have been whole numbers which are also
known as INTEGERS. There are many other data types which
we will use in computing.

TASK 2.1 USING DATA TYPES:


LIST (ARRAY), INT, FLOAT, AND
BOOLEAN
Type the following into Python.
myName = enter your name
print myName
Type in the following and explain what is happening in each
Type this into Python What is printed out
What do you
think is
happening
print myName
print myName [0]
print myName [4]
print myName
[2:5]
print myName [3:]

Set the variables to the values listed in the instruction:

Set the variable my_variable equal to the value 10.


Set the variables to the values listed in the below instructions:

my_int=7
my_float=1.23
my_bool=True

A boolean is like a light switch. It can only have two values. Just
like a light switch can only be on or off, a boolean can only be
True or False.

TASK 3 STRINGS IN PYTHON


Each character in a string is assigned a number. This number is
called the index.
The string "POTTER" has six characters,
numbered 0 to 5, as shown below:
+---+---+---+---+---+---+
| P | O | T | T | E| R |
+---+---+---+---+---+---+
0
1 2 3 4 5
So if you wanted "O", you could just type
"POTTER"[1] (always start counting from 0!)
Type the following into Python.
myName = enter your name
print myName
Type in the following and explain what is happening in each

Type this into Python

What is printed out

What do you
think is
happening

print myName
print myName [0]
print myName [4]

TASK 3.0 PYTHON SYNTAX:


Whitespace
In Python, whitespace is used to structure code. Whitespace is
important, so you have to be careful with how you use it.
The code below is badly formatted. Type it in and press enter
and see what happens:
def spam():
eggs = 12
return eggs
print spam()
You should see an error message. We'll fix it in the next exercise!
File"python",line2
eggs=12
^
IndentationError:expectedanindentedblock

Whitespace Means Right Space


IndentationError:expectedanindentedblock

You'll get this error whenever your whitespace is off.


Properly indent the code with four spaces before eggs on line 2
and another four before return on line 3.
You should indent your code with four spaces.

TASK 4.0 COMMENTS


Single Line Comments
The # sign is for comments. A comment is a line of text that
Python won't try to run as code. It's just for humans to read.
Comments make your program easier to understand. When you
look back at your code or others want to collaborate with you,
they can read your comments and easily figure out what your
code does.

Multi-Line Comments
The # sign will only comment out a single line. While you could
write a multi-line comment, starting each line with #, that can be
a pain.

Instead, for multi-line comments, you can include the whole


block in a set of triple quotation marks:
See below example of a Multi-Line Comment.
"""Sipping from your cup 'til it runneth over,
Holy Grail.
"""

Write a comment against each line of code. Tip always start with
the #.
my_variable =42
my_bag = 7

5.0 MY FIRST PYTHON


PROGRAM
So far all the lines of code have been written directly into the
Python Shell. The next task will create and save a Python
program.
Go to File | New Window (or press Ctrl + N) to open up a new
window to write a program in.
Type in the following code
myName = My name is Mrs Borteye
Mrs Borteye with your name)
print myName

(replace

This time Python wont automatically print the name as the


program is not being run. To run the program, it needs to be
saved and run inside the Python Shell.
Go to Run | Run Module (or press F5). Python will prompt you
to save the program. Save it as My First Program in your
Year 9 Computer Science folder.

TASK 6.0 The INPUT


FUNCTION
The Input Function:
One way to get data is directly from the user.
Create a new program as follows in the editor, and save it with File Save As....`,
using the name hello_you.py.

person = input ('Enter your name: ')

print ('Hello', person)

Run the program. In the Shell you should see


Enter your name:

Follow the instruction (and press Enter). Make sure the typing cursor is in the Shell
window, at the end of this line. After you type your response, you can see that the
program has taken in the line you typed. That is what the built-in
function input does. First it prints the string you give as a parameter (in this
case 'Enter your name: '), and then it waits for a line to be typed in, and returns
the string of characters you typed.
The parameter inside the parentheses after input is important. It is a prompt,
prompting you that keyboard input is expected at that point, and hopefully indicating
what is being requested. Without the prompt, the user would not know what was
happening, and the computer would just sit there waiting!

We are going to write a new program to allow anybody to type


their name in and the program will greet them.
Open a new window in Python and type in the following code.
name = input (Please enter your name )
print Hello + name
Print name + is awesome
Save your code and run it in the Python Shell.
Go back to your code and edit it so that the computer asks the
user more questions and then gives a suitable response.
Add some comments to your code using hash tags #.
Write you name at the top of the program using the # and then
print it out

So far we have learnt about


Topic

I cant
do this

I can do
this with
some

I can
do this
on my

help

own

Using the python shell as a


calculator
Creating and reassigning
variables
Using integers
Using strings
Using a new window to create a
program
Using comments to add notes to
your program
Using the Input command to
prompt users to input
information
Tick off how confident you feel
about the topics we have covered
so far

Você também pode gostar