Download File - Miss patel`s Computing hub

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Miss Patel – Barr Beacon School
Python Workbook
Name: ……………………… Form: ………
Tutor: ………………………
1
Miss Patel – Barr Beacon School
About this workbook
Work through this booklet during Computer Science Python lessons. Things will be discussed
in lesson and this book will also provide examples and explanations to what you are
learning. You will be required to write your own explanations, answers and complete your
own tasks in this book during the course. You will use this booklet to revise from and help
you remember what you have learnt.
When you see ‘KW:’ this means you need to find this keyword in the back of the booklet and
write down the definition.
Introduction
What is Python?
Python is a programming language which is widely used in Computing to create applications.
Python is generally used to introduce programming languages because it is easy to learn and
also used by professional software developers in industry.
Using what you have learn about Python in the lesson answer the following questions?
What is programming?
…………………………………………………………….…………………………………………………………….……………
……………………………………………….………………………………………………………………………………………
………………………………….…………………………………………………………….………………………………………
…………………….…………………………………………………………………………………………………………………
What is a computer program and where can we find them? (use the words instructions,
hardware and software in your answer).
…………………………………………………………….…………………………………………………………….……………
……………………………………………….………………………………………………………………………………………
………………………………….…………………………………………………………….………………………………………
…………………….…………………………………………………………………………………………………………………
…………………………………………………………….…………………………………………………………….……………
……………………………………………….………………………………………………………………………………………
………………………………….…………………………………………………………….………………………………………
…………………….…………………………………………………………………………………………………………………
KW: Program, Software, Hardware
2
Miss Patel – Barr Beacon School
1. Getting started with Python
1.1.

Writing your first program
Open up a Python IDLE IDE
This is the Python shell, and is useful for short commands / simple instructions.


This is called interactive mode/shell (your instructions will be executed straight away
Locate the command prompt (>>>), after the command prompt type in the following
code:
print(“Hello World”)

Hit enter – Well done you just created your first Python Program
What happens?
…………………………………………………………….…………………………………………………………….…………………
……………………………………………………….…………………………………………………………….………………………
Notice, the print is in purple and the Hello World is in green – this is called syntax highlighting. Python
automatically assigns different colours to different elements of the code.
KW: IDLE, command prompt
Activity

Write a programme to write your name on the screen
Write your code here:
3
Miss Patel – Barr Beacon School
What do you think the following will do?
print(“Hello World”)
-
print(“Hello)
-
print(Hello World)
-
prin(“Hello World”)
-
What do they actually do?
print(“Hello World”)
-
print(“Hello)
-
print(Hello World)
-
prin(“Hello World”)
-
KW: Syntax highlighting, Syntax errors
Syntax Errors
What is syntax? Syntax is used to describe the rules that determine the way instructions
and code have to be written. When there are errors in your code an error message will
appear.
Write some code that will bring up a syntax error:
What is the error message?
How can you fix this? (This is called debugging)
4
Miss Patel – Barr Beacon School
Calculations in Python
Remember python operators:
+
Addition
-
Subtraction
*
Multiplication
/
Division
Try the following in Python:
1564 + 8467
67599 - 455
4.55 * 455
5510 / 5
KW: Debugging, Operators
Can you use multiple operators in one calculation? Try two out and paste your code below:
*
*
5
Miss Patel – Barr Beacon School
Variables
In Python we use variables to store information like numbers, values, text, etc. A variable
can be given any name that is meaningful which is then given a value.
For example we want to store 23 cookies in jar 1 we would use the following command:
jar1 = 23
We can then reuse this variable in our code or print the value we stored inside jar1 easily :
print (jar1)
Activity

Using the variable jar1 which you have just created calculate in Python
how many cookies we would have altogether if we had 9 jars. Write
your code here:
…………………………………………………………….…………………………………………………………
What if we decided to store 30 cookies in each jar and wanted to calculate how many we
would have in 9 jars? What could we do?
Create 2 variables: max and min
Carry out a calculation that will print a new value using max and min
Write your code below:
6
Miss Patel – Barr Beacon School
Variables and user input
For this section you will need to work in the IDLE text editor. The text editor allows you to
edit the code in chunks and then run it when you are happy.
To get this you will need to go to “File” in shell mode, and click “New Window / New File”,
this will open up a new blank document called untitled.
You will not be able to run any code until you have saved this file, so give it a meaningful
name and save it in your area in your Miss Patel folder.
Let’s create a program that will ask a user to enter their name and return a personalised
message using a variable to store their name.
Type the following code in IDLE (not in the shell)
name=raw_input("What is your name? ")
print ("Hello " + name)
To run this code you will need to go to “Run” and click “Run module”.
This will run your code and open up a new python shell window. You will see you print
messages appear – type in your name and hit enter.
KW: variable
You have seen we can embed the contents of a variable in defined string. Try the following:
age=raw_input("How old are you? ")
print "You are , age, "years old"
There is a syntax error in the code above. Can you find it and say why it has given a syntax
error message?
…………………………………………………………….…………………………………………………………….…………………
……………………………………………………….…………………………………………………………….………………………
KW: String, Syntax
7