If the same piece of code needs to be used several times we can use a loop – but only if those times are all together. If you need to run the same bit.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Program Design and Development
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to Python
Guide To UNIX Using Linux Third Edition
Main task -write me a program
Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).  A program is needed to prompt.
Chapter 1 Program Design
Python quick start guide
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Invitation to Computer Science, Java Version, Second Edition.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Python Libraries Importing and Calling functions.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Python – Procedures If the same piece of code needs to be used several times we can use a loop - but only if those times are all together. If you need.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
I Power Higher Computing Software Development High Level Language Constructs.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Getting Started With Python Brendan Routledge
Testing i. explain the importance of system testing and installation planning;
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Trace Tables In today’s lesson we will look at:
Whatcha doin'? Aims: To start using Python. To understand loops.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Functions.
Explain what touch develop is to your students:
Teaching London Computing
Functions, Procedures, and Abstraction
Log onto a computer first then ….
Coding Concepts (Sub- Programs)
ARRAYS 1 GCSE COMPUTER SCIENCE.
Coding Concepts (Basics)
Starter answer these questions in your book
Python Basics with Jupyter Notebook
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
CISC101 Reminders Assignment 3 due today.
Class code for pythonroom.com cchsp2cs
CPS125.
Functions, Procedures, and Abstraction
Using Modules.
Presentation transcript:

If the same piece of code needs to be used several times we can use a loop – but only if those times are all together. If you need to run the same bit of code again later, you can use a procedure.

In this example I want to print the squares of the numbers from 1 to 20: #This is a procedure called output #It will print a given number and its square def output(value): print (value,"squared =",value*value) #This is the start of the main program number = 1 while number < 21: output(number) number = number + 1

Here the top two lines are a procedure called output. def output(value): print (value,"squared =",value*value) number = 1 while number < 21: output(number) number = number + 1 The procedure expects to be given one variable (which it calls value) and will happily do its thing when told to. The main program is underneath, and the line that says output(number) will run the output procedure and give it the variable number to work with.

A parameter is the variable (or variables) that get passed to the procedure. You can require more than one parameter: Try the following; Comment on each line of code def output(number1,number2): print (number1,"+",number2,"=",number1+number2) for counter in range(20): number = counter**2 output(counter,number) You can pass as many parameters as you like to a procedure as long as you set it up correctly.

Once you have defined a procedure, you can call it in a number of ways: Try The following and comment on each line; def output(grade,score=50,feedback="Well done!"): print ("You scored",score,"which is a grade",grade,feedback) output("C") output("A",87) output("E",12,"Rubbish!") You can pass as many (or few) of the parameters as you like - as long as they’re in the right order. This means you have to plan the procedure quite carefully.

So far our procedures have just printed an answer to the console. Sometimes a procedure will have to send some data back. return This is called a return. Procedures that return something are called functions. In Python that isn’t a huge distinction, but in some programming languages it is very important.

Try the following and add comments on each line; def calculate(number): newnumber = number / return (newnumber) for counter in range(5,101,5): answer = calculate(counter) print (counter,"% = ",answer) The line return(newnumber) passes the value of the newnumber variable back to the main program. The line y = calculate(x) shows that the main program expects a value to be returned and has somewhere to store it.

What happens if you change the line: newnumber = number/100.0 to newnumber = number/100.0 Try it

You are going to write a program that will give the user the option to perform different string manipulation tasks. To start, the options should be: 1.Find the length of the string 2.Print only the first letter of the string 3.Convert it to upper case 4.Print each character of the string on a separate line 5.Reverse the string Before starting the code, we need to plan. How could you decompose this problem? Write a solution in pseudocode.

There are some procedures already built in to Python (print, for example). Modules (sometimes known as libraries in other languages) are collections of extra procedures and functions that are pre- written. Try this program first. number = 49 answer = sqrt(49) print answer It won’t work, but I’m trying to prove a point (sqrt is short for square root):

Now try the below import math number = 49 answer = math.sqrt(49) print answer Comments on each line explaining what the code is doing

Open your program Swap seat with a partner Look through your partner program Think of three things you can do to the program to make it stop working Change or edit your partner code so that the whole program does not work