Sunday, 10 August 2014

5. Development of Code

Q

As you begin to develop more complex programs you will experience error messages.  The great thing about Python is that it tends to explain the error you have made in detail.  Your job is to understand what these mean and to put them right.

LEARNING OBJECTIVES

You will learn to:
Interpret common error messages
Understand how to debug a program
Understand and use comment statements
Understand the file/new window
Save a program in scripting mode


TASK 1

Copy and run this code and explain the result.

>>>print hello world


TASK 2

Copy and run this code and explain the result.

>>>print("hello world")

TASK 3

Python produces different types of error.  In your learning blog match the type of error with the description of error.


Type of error

TypeError

RuntimeError

NameError

ZeroDivisionError

KeyBoardInterrupt

 

Description of the error

Dividing a number by zero.

When a program is interrupted from the keyboard by pressing control+c.

When a name is used that is not known about (often a variable name that is misspelt).

When an operation is attempt that is invalid for that type of data.

An error occurs when the program is running.


TASK 4

 

Experiment with Python to see if you can create each type of error.

Hint: A full list of Python errors can be found here:http://www.tutorialspoint.com/python/standard_exceptions.htm

TASK 5


Copy and run this code and explain the result.

# Programmer Amy Smith 6th September 2013


TASK 6


Use file/new window to open a script file in the IDLE.
Copy this code into the file.

print("So long and thanks for all the fish")

print("The answer is",6*7)

 

Save the file using file/save. Always give the filename an extension of .py. Use a meaningful filename and store in a suitable folder.
Run the commands by using run/run module

HOMEWORK

Using the Python IDLE

Write your own summary sheet to describe how to run Python programs, including how to save and open .py files. Include any other commands that you have found useful.  

Using the Python IDLE

 

How to open a new window

 

How to save a file

 

How to open a file

 

How to run a program

 

 

 

 

 

 

 

Useful tips

 

alt p

Displays the last line you entered

Commands in the file are not colour coded

Save as a .py file

 

 

 

 



4. Maths





In these lessons we are going to look at the ease with which you can use Python to perform mathematical calculations.  Many of you will have covered this in KS3 so hopefully we should cover this section very quickly.

LEARNING OBJECTIVES

You will learn to:
Describe and use mathematical operators
Combine calculations with text
Describe the use of parenthesis and use of procedure

TASK 1

Copy and run these lines of code. Complete the table to explain what the mathematical operators do.
>>> 60/5
>>> 987+34
>>> 564*89
>>> 2**5
>>> 43-5
>>> 11//2
>>> 11%2

In a Google Doc make and complete the following table and link to your learning blog.

Mathematical operator symbol
Operation
/

+

*

**

-

//

%

 TASK 2

Using Python make up some mathematical calculations of your own and add an example to the table for each mathematical operator.  
A) Screen dump your calculations into a Google Doc and link to the learning blog.  

B) Also, complete the table with example calculations and an answer.

Mathematical operator symbol
Operation
Example
Answer
/
divide


+
add


*
multiply


**
exponential


-
subtract


//
integer division


%
modulus (remainder after the division)



TASK 3

A) Write a program to display this text on the screen and fill in the missing number.
8 cats have 4 legs each
The cats have ___ legs in total

B) Write a program to display this text on the screen and fill in the missing number.









A farmer with 1089 sheep sells 56 of them
The farmer has _____ sheep left


C) Write a program to display this text on the screen and fill in the missing number.









4 children pick 56 flowers each
The children each have ____ flowers

TASK 4

Copy and run these lines of code. What effect do the parentheses () have?
>>> 5 * 3 / 6 + 4
>>>(5 * 3) / (6 + 4)

TASK 5

Predict what you think will be displayed on the screen when this line of code is executed.
15 / 2 * 3 + 2
Now copy and run the code. Explain the answer you get. Is it what you predicted?
HOMEWORK
In a Google Doc  
Make up some multiple-choice questions on precedence. Each question must have four possible answers, one of which is correct.
Here are two examples:
What is the correct answer to the following expression?
>>> 7 + 4 * 5


A) 55 B) 27 C) 16 D) 33







What is the correct answer to the following expression?
>>> 6 -2 / 2 + 5

A) 0.57 B) 10 C) 7 D) 11

Check your answers are correct by using the interactive Python shell.
Try your questions out on other people. You must be able to explain to them why the answers are correct.


Thursday, 7 August 2014

02 2. Introduction to Programming


THIS MAY REQUIRE 2 ONE HOUR LESSONS

STARTER

With your partner discuss the following statement "Computers only do two things but do them extremely well".  Note your ideas in your learning blog.





Today we are going to start looking at the basics of programming using Python. THIS WILL BE THE ONLY language we will teach towards the controlled assessment.

Learning Outcomes

You will learn:
  • To describe the Python programming language and it's features
  • To use the interactive mode of IDLE
  • To display text on the screen
  • To describe keyboard shortcuts
  • To use escape sequences
  • To use ASCII art
Python

TASK 1

As a class we are going to look at the following questions about Python:

Python was released in 2010
True or False
Python was named after the TV series “Monty Python’s Flying Circus”
True or False
Python is proprietary software which is expensive to buy
True or False
Python was written by Bill Gates
True or False
YouTube is written in Python

Python Modes

There are two to modes to Python.  An interactive mode where you can test code and a scripting mode where you will write programs.
TASK 2
In interactive mode write a program to print "Hello Dave".

TASK 3

In interactive mode write a program that prints your name on the screen.
TASK 4

Copy and run this program:

print ("hello"*100)

TASK 5

In interactive mode now write a program that prints your name one hundred times.

TASK 6

Make a note of these keyboard shortcuts:


Keyboard shortcut
What does the keyboard shortcut do?
Control c

Control v

Control a

Control x

Control f

Control n

Control p

Control s

Control z



TASK 7

Copy and run this program:

print("This is the end")
print("Hold your breath and count to ten”)
print("Feel the earth move and then”)
print("Hear my heart burst again”)


TASK 8

Write a program that prints a verse of your favourite song.

TASK 9

Copy and run this program:

print("the rain falls","from the sky")

Now answer these questions:

What happens?
What effect does the  "," have?

TASK 10

Escape sequences can be used to alter how the information is displayed on the screen. An escape sequence is a back slash “\”.
Experiment with these escape sequences and complete the table.
print("\tQuestion what goes woof\t\tdogs\t\t\trabbits")
print("\n\nwhat kind of snake is good at maths?\n\nAn adder\n\n")
print("\n\nGoodbye\n\n")

Hint: Use alt p to display the last command entered in the IDLE shell.
Escape sequence
Effect
\t

\n

\\

\’

\”


TASK 11
Print this text using just one line of code.
Help, I need somebody
Help, not just anybody
Help, you know, I need someone
TASK 12
Write a program using print commands to display your initials five characters high on the screen.
X      X  X          X
XX    X X          X
X  X  X  XXXXXX
X    XX  X          X
X      X  X          X