0% found this document useful (0 votes)
1 views50 pages

Student Workbook 1 - Python Basics

Uploaded by

Neha Vadaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views50 pages

Student Workbook 1 - Python Basics

Uploaded by

Neha Vadaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

PYTHON

PROGRAMMING
Student Workbook

NEHA VADAHA
Name
YEAR 10
Class
Ms. DENISE
Teacher

1
2
Python is an easy to learn, powerful programming language. Python is a general-purpose,
interpreted high-level programming language whose design philosophy emphasizes code
readability.

Its syntax is said to be clear and expressive. Python has a large and comprehensive standard
library.

Python lets you write the code you need, quickly. And, thanks to a highly optimized byte
compiler and support libraries, Python code runs more than fast enough for most applications.

1.1 Downloading and Installing Python (Windows


Installation)
Before we can begin programming you'll need to install the Python software; specifically the
Python interpreter. (You may need to ask an adult for help here.) The interpreter is a
program that understands the instructions that you'll write in the Python language. Without
the interpreter, your computer won't understand these instructions and your programs won't
work. (We'll just refer to "the Python interpreter" as "Python" from now on.)
Because we'll be writing our games in the Python language, we need to download Python
first, from the official website of the Python programming language,
http://www.python.org

I'm going to give you instructions for installing Python on Microsoft Windows, not
because that's my favorite operating system but because chances are that's the operating
system that your computer is running. You might want the help of someone else to download
and install the Python software.
When you get to python.org, you should see a list of links on the left (About, News,
Documentation, Download, and so on.) Click on the Download link to go to the download
page, then look for the file called Python 3.1 Windows Installer (Windows binary -- does
not include source) and click on its link to download Python for Windows.

3
Figure 1-1: Click the Windows installer link to download Python for Windows from http://www.python.org

Double-click on the python-3.1.msi file that you've just downloaded to start the Python
installer. (If it doesn't start, try right-clicking the file and choosing Install.) Once the installer
starts up, click the Next button and just accept the choices in the installer as you go (no need
to make any changes). When the install is finished, click Finish.

Important Note! Be sure to install Python 3, and not Python 2. The programs in this
book use Python 3, and you'll get errors if you try to run them with Python 2.
The installation for Mac OS is similar. Instead of downloading the .msi file from the
Python website, download the .dmg Mac Installer Disk Image file instead. The link to this file
will look something like "Mac Installer disk image (3.1.1)" on the "Download Python
Software" web page.

If your operating system is Ubuntu, you can install Python by opening a terminal
window (click on Applications > Accessories > Terminal) and entering sudo apt-get install
python3 then pressing Enter. You will need to enter the root password to install Python, so
ask the person who owns the computer to type in this password.

There may be a newer version of Python available than 3.1. If so, then just download the
latest version. The game programs in this book will work just the same. If you have any
problems, you can always Google for "installing Python on <your operating system's
name>". Python is a very popular language, so you should have no difficulty finding help.

A video tutorial of how to install Python is available from this book's website at
http://inventwithpython.com/videos/.

4
1.2 Starting Python
If your operating system is Windows XP, you should be able to run Python by choosing
Start > Programs > Python 3.1 > IDLE (Python GUI). When it's running it should looking
something like Figure 1-2. (But different operating systems will look slightly different.)

Figure 1-2: The IDLE program's interactive shell on Windows.

IDLE stands for Interactive DeveLopment Environment. The development environment is


software that makes it easy to write Python programs. We will be using IDLE to type in our
programs and run them.

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

5
1.3 – Opening, Running and Configuring Python
We are using Python 3 interpreter release in April 2012 and can be downloaded from
http://www.python.org/download/releases/3.2.3/

The python software has two windows that we will use. The main window is called the Python Shell
and allows you to directly write in program lines and then a press of the return will execute them.
This is good for testing one line of code.

The second window is the “Edit Window” which allows you to enter several lines of code, or a create
a program, save it and then execute the code.

Opening the python software

Go to Start > All Programs> Subject > ICT > Python 3.2 > IDLE (Python
GUI)
Configuring Python to open the editor:
1. Click on the Options tab in the Python
GUI

2. Select the General tab in the IDLE


Preferences window that appears

3. Select the radio button: Open Edit


Window click Apply > OK

In the Python GUI, click on File and New


Window will open the Python Editor
(Alternatively: CTRL + N)

To create a new python program, you can use the edit window.

To save a program Type: (the name of your program).py

To run or execute the program type: python (the name of your program).py

6
● Inputs
● Outputs
● Variables
● Basic data types
● Operators

7
Task 1 - A Short Biography
Write a program that prints out the
following on separate lines:
Your first name
Your age
Your favourite hobby.

Write down below, the code you entered into


your python editor:

SEND “Neha” TO DISPLAY

SEND “14” TO DISPLAY

SEND “Listening to music”

#displays all the quotations

8
Task 2 - Rewriting History
Write a short program similar to your
biography program, however it must now:
1. Prompt the user for their name, age
and favorite cartoon.
2. Store these in a variable
3. Print on separate lines, as before,
however only use one print statement

Write down below, the code you entered

into your python editor:

SET x TO Neha

#Neha is named x

SET y TO 14

#14 is named y

SET z TO Tom and jerry

#Tom and Jerry is named z

9
SEND (x+ “\n”+y+ “\n”+ z+ “\n”) to display

# #displays all the quotations

10
Task 3 - Shopping Receipts
1. Write a short program for generating
nicely formatted receipts for your
shopping.
2. The user should be able to input 3
items, each with the price, and these
should be printed out after all three have
been inputted.
3. Also provide the total amount spent!
Write down below, the code you entered into your python editor:

SEND ‘Enter your items and the cost\n’ TO DISPLAY


SET ‘item1’ TO RECEIVE ‘Enter your item: ’ FROM KEYBOARD
#asks users to enter their item

SET ‘price1’ TO RECEIVE ‘Enter the price: ’ FROM KEYBOARD


#asks users to enter the price

SET ‘item2’ TO RECEIVE ‘Enter your second item: ’ FROM KEYBOARD


#asks users to enter their item

SET ‘price2’ TO RECEIVE ‘Enter the price: ’ FROM KEYBOARD


#asks users to enter the price

11
SET ‘item3’ TO RECEIVE ‘Enter your third item: ’ FROM KEYBOARD
#asks users to enter the item

SET ‘price3’ TO RECEIVE ‘Enter the price: ’ FROM KEYBOARD


#asks users to enter the price

SET ‘sum’ TO RECEIVE ‘price1+price2+price3’


SEND ‘total amount spend: ’, ‘sum’ TO DISPLAY

12
13
Task 4 - Comparative Operators
1.Write a program that takes two numbers, and
checks which of the two is greater.
2.Your program should return the greater of the
two numbers!

Write down below, the code you entered into your python editor:

SET ‘num1’ TO RECEIVE ‘Enter your first number: ’ FROM KEYBOARD


#asks users to enter the number

SET ‘num2’ TO RECEIVE ‘Enter your second number: ’ FROM KEYBOARD


#asks users to enter the second number

IF num1>num2 THEN
SEND ‘{} is the greatest’.format(num1) TO DISPLAY
ELSE
IF num1<num2 THEN
SEND ‘{} is the greatest’.format(num2) TO DISPLAY
ELSE
SEND ‘() and () are equal’.format(num1,num2) TO DISPLAY

14
Note: In Python, adding an = symbol creates a variable.
In order to test whether something is equal to something,
we use ==

15
16
Challenge 1
1. The NHS recommends that an adult
male takes on board 2, 500 calories
per day, and an adult woman takes on 2,
000 calories per day.
Write a program that asks for the
amount of calories a user has eaten
today. Subtracts how many calories they
have eaten and tells the user how many
calories they have left to eat today.
Write down below, the code you entered into your python editor:

17
SEND “your calories counter, it will calculate how much more calories you need to take today”
TO DISPLAY
SET gender TO RECEIVE “Gender: If male type 1, if female, type 2" FROM KEYBOARD
#asks the user to type a number

IF gender == 1 THEN
SET calorym TO RECEIVE "How much calories you took today?"
#asks the user to type their calories

SEND (2500 - calorym) TO DISPLAY


IF calorym>2500 THEN
SEND "You took more calories than you should." TO DISPLAY
IF calorym==2500 THEN
SEND "You're good to go!" TO DISPLAY
ELSE
SEND "You need to take more calories today." TO DISPLAY
IF gender==2 THEN
SET caloryf TO RECEIVE "How much calories you took today?" FROM KEYBOARD
#asks the user to type their calories
SEND (2000-caloryf) TO DISPLAY

18
IF caloryf>2000 THEN
SEND "You took more calories than you should." TO DISPLAY
IF caloryf==2000 THEN
SEND "You're good to go!" TO DISPLAY
ELSE
SEND "You need to take more calories today." TO DISPLAY
ELSE
SEND “please state your gender” TO DISPLAY

19
20
2. Create some code which will print a box
that looks like this:

+ -- +
| |
+ -- +
Write down below, the code you entered into your python editor:

21
SET m TO“+ -- +”
SET n TO “| |”
SEND m TO DISPLAY
SEND n TO DISPLAY
SEND m TO DISPLAY

#sends all the quotations to display

22
Teacher Feedback
Effort Grade Choose an item.

Even Better if (What you


can do to improve your
work)

Student Response

23
● Conditional Statements

24
Task 5 - Film Classification
Write a program (that uses an If Statement), which
asks the user to enter their age. If their age is under
18, then say “Sorry, this film is not for you”

Write down below, the code you entered into your python editor:

25
SET age TO RECEIVE “How old are you?” FROM KEYBOARD
#ask the user to type their age
IF age>=18 THEN
SEND “You can watch this film” TO DISPLAY
ELSE
SEND “Sorry, this film is not for you” TO DISPLAY
#display the quotation according to the if statement.

26
Task 6 - Programmable Calculator
Write a program (with an If Statement) that:

27
Gives a user an option to choose to either the add
or subtract operation.

Lets the user provide (two numbers), one number


must be an integer and the other number must be a
float and...

Applies the chosen operator onto the integer


and float number, also chosen by the user.

Write down below, the code you entered into your python editor:

SET number1 TO RECEIVE “Enter a number” FROM KEYBOARD


#ask the user to enter a number

SET number2 TO RECEIVE “Enter the second number” FROM KEYBOARD

28
#ask the user to enter a number

SET operation TO RECEIVE “"if you want to add type 1, if you want to
subtract type 2"” FROM KEYBOARD
#ask the user to enter a number to add or subtract

IF operation == 1 THEN
SEND (number1+number2) TO DISPLAY
IF operation == 2 THEN
SEND (number1-number2) TO DISPLAY
ELSE
SEND “Specify” TO DISPLAY
#displays the answer according to the if statement.

29
Task 7 - Grade Calculator
Write a short program that prompts the user to
enter the percentage mark they obtained for their
GCSE Computing exam.
Write the necessary conditionals to determine the
student’s grade.
Use multiple elifs to identify whether the user
achieved an A, B, C,D, E, Fail.

Write down below, the code you entered into your python editor:

30
set percentage to receive “enter your percentage” from
keyboard

if percentage>=80 then
send “Grade: A” to display
elif percentage>=70 then
send “Grade: B” to display
elif percentage>=60 then
send “Grade: C” to display
elif percentage>=50 then
send “Grade: D” to display
elif percentage>40 then
send “Grade: E” to display
else:
send “Grade: F” to display

#displays the quotation depending on the percentage given,


comparing it to if statement.

31
32
Challenge 2 – Python FunQuiz
Write a short program that asks the user five
questions
If the user gets the answer correct, print
"Correct" and award them a point.
If their final score is greater than 3 print out a
suitable message of encouragement.
Write down below, the code you entered into your python editor:
Send “welcome to the Python Fun Quiz” to display
Send "instructions:\nyou have to answer 5 questions\nfor
each question,you get 1 mark.\n " to display
Send "Note: write the first letter of the answer in the capital
case" to display

set score to 0
set q1 to receive"Question 1: What is the capital of Gabon?\
n" from keyboard
set a1 to “Libreville”
if q1=a1 then:
send “correct” to display
set score to score +1
else:
send “wrong” to display

set q2 to receive"Question 2: What is the capital of Russia?\


n" from keyboard
set a2 to “Moscow”
if q2=a2 then:
send “correct” to display
set score to score +1
else:
send “wrong” to display

33
set q3 to receive"Question 3: What is the capital of
Kazakhstan?\n" from keyboard
set a3 to “Astana”
if q3=a3 then:
send “correct” to display
set score to score +1
else:
send “wrong” to display

set q4 to receive"Question 4: What is the capital of Italy?\n"


from keyboard
set a4 to “Rome”
if q4=a4 then:
send “correct” to display
set score to score +1
else:
send “wrong” to display

set q5 to receive"Question 5: What is the capital of United


Kingdom ?\n" from keyboard
set a5 to “London”
if q5=a5 then:
send “correct” to display
set score to score +1
else:
send “wrong” to display

send “Your points are,”score to display


if score>3 then:
send "You have some geographical knowledge, i see..." to
display
else:
send "You need some geographical knowledge.." to
display
send “have a good day”

#displays the quotations according to your answer and your


score.

34
35
Teacher Feedback
Effort Grade Choose an item.

Even Better if (What you


can do to improve your
work)

Student Response

36
● Loops
● Functions

37
Task 8 - What’s my name?
Write a program that asks for your name and
then displays it 5-times on the screen.

Write down below, the code you entered into your python editor:

set name to receive “Enter your name:” from keyboard


set answer to int(5)
for i in range(answer):
send name to display
#displays your name 5 times on the screen as the instruction

38
was told.

39
40
Task 9 – Try again!
Using a while loop, write a program which
keeps on asking the user what the capital of
Brazil is, until the user gets it right. The
program should then say “well done!”

Write down below, the code you entered into your python editor:

41
while (true):
set answer to “Brazilia”
set question to receive “What is the capital of Brazil?”
from keyboard
if answer==question then
send “well done” to display
break
else
continue

#asks the users the question repeatedly until the user gets
the right answer.

42
43
Task 10 – What’s your function?
1. Write a function that will save your name
when you type it in, and print it out.
Write down below, the code you entered into your python editor:

44
def x():
set y to receive “Enter your name: ” from keyboard
send y to display
x()

45
2. Write a function that takes the radius of a
circle and works out the area.

Hint: Area (A) of a circle is = Pi x radius x


radius. Pi = 3.14
Write down below, the code you entered into your python editor:

46
def x():
set n to receive “Insert the radius of the circle to calculate the area using pi=3.14” from
keyboard
send(n*n*3.14) to display
x()

47
48
Teacher Feedback
Effort Grade Choose an item.

Even Better if (What you


can do to improve your
work)

Student Response

49
50

You might also like