Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?



Similar documents
Introduction to Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Computer Science for San Francisco Youth

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Python Loops and String Manipulation

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Computer Science 217

Welcome to Introduction to programming in Python

Fish Chomp. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

Introduction to Python

STEP 0: OPEN UP THE PYTHON EDITOR. If python is on your computer already, it's time to get started. On Windows, find IDLE in the start menu.

How to Build a Simple Pac-Man Game

Python Lists and Loops

River Dell Regional School District. Computer Programming with Python Curriculum

Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

Programming in Access VBA

Exercise 4 Learning Python language fundamentals

SPSS Workbook 1 Data Entry : Questionnaire Data

Introduction to Computer Science Using Python and Pygame

Exercise 1: Python Language Basics

Computational Mathematics with Python

Special Topic Lesson: they re, there, their

Mathematics as Reasoning Students will use reasoning skills to determine the best method for maximizing area.

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

Python 3 Programming. OCR GCSE Computing

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Computational Mathematics with Python

Crash Dive into Python

Early Math for School Readiness Kids Play Math: An Overview Front Porch Series Broadcast Calls

MAKING MATH MORE FUN BRINGS YOU FUN MATH GAME PRINTABLES FOR HOME OR SCHOOL

PYTHON Basics

CSC 221: Computer Programming I. Fall 2011

Here are the top ten Christmas activities 2009

Computer Programming In QBasic

Computing and Communications Services (CCS) - LimeSurvey Quick Start Guide Version 2.2 1

Direct Translation is the process of translating English words and phrases into numbers, mathematical symbols, expressions, and equations.

Computational Mathematics with Python

20 CODE CHALLENGES. GCSE (9 1) Computer Science GCSE REFORM. February 2015

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

MATHS ACTIVITIES FOR REGISTRATION TIME

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Writer Guide. Chapter 15 Using Forms in Writer

Introduction to Programming with Python Session 3 Notes


Python Programming: An Introduction to Computer Science

Comprehensive Medical Billing and Coding Student CD Quick Start Guide By Deborah Vines, Ann Braceland, Elizabeth Rollins

Getting together. Present simple 1. New Year in Vietnam. Reading: Everybody s birthday. Word focus: Special occasions

Chapter 15 Using Forms in Writer

Code::Blocks Student Manual

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Random Fibonacci-type Sequences in Online Gambling

Invent Your Own Computer Games with Python

SQUARE-SQUARE ROOT AND CUBE-CUBE ROOT

Mathematics. Introduction

Clifton High School Mathematics Summer Workbook Algebra 1

Fruit Machine. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

Teaching Pre-Algebra in PowerPoint

SCRATCH PROGRAMMING AND NUMERACY IN SENIOR PRIMARY CLASSES

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

Introduction to Python

Using SPSS, Chapter 2: Descriptive Statistics

Integrated Skills in English ISE I

Year 8 KS3 Computer Science Homework Booklet

In 2 Hockey Leadership Course Planning and Delivery Guidance

Import and Export User Guide PowerSchool Student Information System

Software Requirements Specification

Computer Science Practical Programming

Everyday Math Online Games (Grades 1 to 3)

Tapescript. B Listen and write the words. C Help the baby spider. Draw a red line. D Help the baby frog. Listen and draw a green line.

COMSC 100 Programming Exercises, For SP15

Female Child s date of birth: Last name: State/ Province: Home telephone number:

Chapter 2 Writing Simple Programs

Python Programming: An Introduction To Computer Science

Acing Math (One Deck At A Time!): A Collection of Math Games. Table of Contents

Standard 12: The student will explain and evaluate the financial impact and consequences of gambling.

Raptor K30 Gaming Software

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

Introduction to Python for Text Analysis

Add or Subtract Bingo

The Dance Lesson. A good dance lesson should contain some or all of the following:-

Sue Fine Linn Maskell

My Family FREE SAMPLE. This unit focuses on sequencing. These extension

Main Effects and Interactions

Grade 1 Level. Math Common Core Sampler Test

What to Expect on the Compass

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Python Programming: An Introduction to Computer Science

Practical Programming, 2nd Edition

HOW TO WRITE S THAT CONVERT CHEAT SHEET secrets to s that convert

Code Kingdoms Learning a Language

The first program: Little Crab

How Old Are They? This problem gives you the chance to: form expressions form and solve an equation to solve an age problem. Will is w years old.

Setting up a basic database in Access 2003

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Kids College Computer Game Programming Exploring Small Basic and Procedural Programming

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Version 0.1. General Certificate of Secondary Education June Unit 1: Statistics Written Paper (Foundation) Final.

Decision Logic: if, if else, switch, Boolean conditions and variables

Transcription:

Python KS3 Programming Workbook Do you speak Parseltongue? Name ICT Teacher Form

Welcome to Python 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 Python and the Raspberry Pi To create a new python program on the Pi, you can use a text editor called Joe s Text Editor Type: To run or execute the program type: joe (the name of your program).py python (the name of your program).py KEY WORDS Code Program Variable Loop Else IF ELIF While For Written and prepared by Mr D.Aldred 2012 Page 2

To Execute the program code press F5 Your first program Hello World TYPE Print Hello There What does it do? To write this program, load up JustBASIC and start a new program (File New) Save your program as Hello Now Try These: Write a program to write your name on the screen Save as MyName Write a program to write the words to your favourite song Save as Song Inputs Your Favourite You can use Python to enter in data, this is called an INPUT. The data that is entered is stored inside a VARIABLE. A variable is like a box that can be used to store a piece of information. We can put different information into the box and open the box at any time START A NEW PROGRAM AND TYPE IN THIS CODE: Print "welcome" x=raw_input ("What is your name?") Print x 1. The variable is called x, this is like calling the box x 2. The raw_input allows the user to enter any symbol or letters. 3. The input allows the user to enter numbers Now see if you can complete these: Write a program that asks for your favourite food and then returns the statement, I like x as well. Save as FavFood. (Use commas to separate the I like and as well Write a program that asks for 2 of your friend s names and then states which friend is the best friend. Save as BestFriends Make up one of your own Written and prepared by Mr D.Aldred 2012 Page 3

Working with numbers Python is also a powerful calculator, go back to the shell mode and type in 1+1 EQUATION ANSWER What does it do? 2+5 67/34 20**5 30*4 3==6 3!=5 39==39 range(100) range(46,100) range(100, 1000, 7) In the text editor window you have to type the command to print range ( ). Can you create a program that list all the numbers starting from 100 to 10000, going up in intervals of 9? You can also float numbers as in create a decimal. Return to the text editor window. TYPE IN x = 3 print(x) y = float(x) print(y) A SIMPLE PROGRAM TO WORK OUT HOW MANY CALORIES YOU CAN EAT IN A DAY Now see if you can complete these: print "Welcome" c=input("how many calories have you eaten today?") s=2000-c print "You can eat", s, "calories today" Jennifer wants to carpet her new room with pink carpet, Create a program that will work out the area of any sized room, (l x w), Save as Square Create a program that will work out the area of any triangle, (h x w), Save as Triangle Write a program to work out how many days you have been alive for? Save as Age What do you need to work out? Written and prepared by Mr D.Aldred 2012 Page 4

IF all ELSE fails If and Else are powerful features where you can create different outcomes IF certain criteria or numbers are met. Else the programme will do something ELSE. START A NEW PROGRAM AND TYPE IN THIS CODE: if 10==10: print "hello" Notice the : at the end of the first line, this tells the computer to do whatever is on the next line, the second line sis also indented to show that it is related to the first line. AFTER : YOU SHOULD USE TAB THE NEXT LINE Now change one of the 10s to a different number, what happens? WHY? Now we want the program to feedback and different answer depending on the size of a number. See if you can create the program to ask the users to Input a number and then if the number is bigger than 500 state big number ELSE print small number, ANSWER OVER THE PAGE: Explain in your own words what a variable is? What have you enjoyed so far? What have you found difficult? What would help? Written and prepared by Mr D.Aldred 2012 Page 5

ANSWER x=input("please enter a number") if x >500: print "Big number" else: print "small number" The Christmas ELIF What about three responses the number of calories that are entered. For example, less than 250, less than 500, more than 500? You can use the ELIF function, this means else if. START A NEW PROGRAM AND TYPE IN THIS CODE: Calorie Counter Part 2 x=input("please enter a number") if x >500: print "Big number" elif x > 250: print "medium number" else: print "small number" Load the calorie counter program you made earlier, can you now create the program so that it asks if you are male or female and then calculate the calories based on your gender. Save as Calorie2 (HINTS: Place all the variables at the top of the page, watch the indents) More than words The length function, what does it do? Try these two examples. x= frank print len(x) a=( cat ) print a, len(a) What does the len() command do? Adding Strings A string is a block of text. It could be a single word, a sentence or an entire paragraph. In Python we use the term str to mean a string. Written and prepared by Mr D.Aldred 2012 Page 6

TRY OUT THE FOLLOWING CODE: Looping, For start = "Hello, " name = input("what is your name? ") end = ". How are you today?" sentence = start + name + end Sometimes you want something to keep happening, this is called a loop. There are several types of loop, the FOR loop and the WHILE Loop. Try te example below and save as ForExample What is the for command doing? a = ('cat', 'dog', 'frog') for x in a: print x, len(x) What does the x do? The for command is useful if you want to hide a word, like a password, which should be kept secret. Now see if you can complete these: Create a program that coverts the variable apples are nice into zeros. Save as Zeros Create a program that has a variable called p which is the password, then the program prints the letters as * to hide the real password. Save the file as Password. What do you notice about the way it displays the characters? What happens if you place a comma after the print *, Using letters instead of numbers In the Python shell we can show how letters or variables can be used with values / numbers assigned. These can then be used with mathematical functions. TRY THIS >>> h=156453737 >>> print h*h*h Written and prepared by Mr D.Aldred 2012 Page 7

While statements Like the For Loop the While Loop will do something while a condition is being met, While I am feeling hungry, eat food. Once the while condition has been met the program stops the loop, once you are not hungry, stop eating TRY THIS x=1 while x==1: print "EPIC FAIL" Explain what the program lines doing? Countdown You can create a countdown by taking away 1 from the variable number each time, ie x = 5, x-1, so now x =4, x-1, so now x=3 and so on. The code is x =x-1, Change the x variable value to 10, the while should be changed so that the while the x value is greater than 1. Finally add x = x-1 to the last line of the program. Save as Countdown Pausing The program runs very fast and all the instructions are completed very quickly. To slow down the instructions or pause between them we can use the sleep function. The sleep function is storedin the time module, so we first have to import the time module. TRY THIS USING YOUR COUNTDOWN PROGRAM, Add the codes import time to the first line of your program (This imports the time features) Add the code, time.sleep(2) after the print x feature? What does it do? Now see if you can complete these: Create a program that asks the user to enter a number of times that a sentence will be displayed, and then displays it that many times. Save as Create a program that asks the user to enter a number of times that a the sentence, (You asked me to display this,? times) and counts down each time. Saves as Number of times Create a program that asks the user to enter a number of seconds until countdown. The program counts down in one second intervals and when it reaches 0 states BLAST OFF. Save as Blastoff Written and prepared by Mr D.Aldred 2012 Page 8

Random Numbers If you are going to create a simple game you require the program to automatically select a random number. This requires the use of the random module. import random x = random.randrange(100) print x The randrange(100) selects any number from between 1 and 100, had it been (500) it will select any number between 1 and 500 ASSIGNMENT Using all the command you have learnt so far, create a game below, save as GuessingGame ACTION Welcomes the user to the game Picks a random number Asks the user to guess the number If the number user number matches the random number state you win! If the number the user number is higher than the random number state your number is too high If the number the user number is lower than the random number state your number is too low The program loops until the user guesses correctly HINTS PRINT RANDOM INPUT IF ELIF ELSE WHILE LOOP EXTENSION The user only gets 5 turns to guess the correct number, HINT you will need to use the break function to stop the program from running. Create a new game that asks 10 questions, if they get the answer correct it adds points to the score, if they answer incorrectly no score is added. The game reports the score at the end of the session. Extension BONUS: Writing to a file Python can be programmed to write and read from files. You made need to try this at home due to the security settings at the school system TRY THESE, WRITING TO A FILE fob=open('c:/folder/folder/documents/file.name.txt', 'w') fob.write('this is the test to write to the document') fob.close() Written and prepared by Mr D.Aldred 2012 Page 9

EVALUATION What have you enjoyed so far? What have you found difficult? What would help? What are your strengths in Python programming? What do you need to do next? Written and prepared by Mr D.Aldred 2012 Page 10