Learning to Program in Python

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Introduction to Python
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
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
BIT116: Scripting Lecture 05
7 - Programming 7J, K, L, M, N, O – Handling Data.
3.1 Fundamentals of algorithms
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Python Programming Module 3 Functions Python Programming, 2/e.
Topic: Python’s building blocks -> Statements
Line Continuation, Output Formatting, and Decision Structures
Repetition (While-Loop) version]
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction to Programming
Lecture 2 Introduction to Programming
Functions CIS 40 – Introduction to Programming in Python
Functions.
Learning to Program in Python
Ruth Anderson UW CSE 160 Winter 2017
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Unit# 9: Computer Program Development
Learning to Program in Python
Line Continuation, Output Formatting, and Decision Structures
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Introduction to Programming
Learning to Program in Python
Number and String Operations
Learning to Program in Python
Learning to Program in Python
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Learning to Program in Python
Ruth Anderson UW CSE 140 Winter 2014
Coding Concepts (Basics)
Introduction to TouchDevelop
Introduction to Programming
Introduction to Programming
Introduction to Programming
Learning to Program in Python
Python programming exercise
PYTHON: BUILDING BLOCKS Sequencing & Selection
Flowcharts and Pseudo Code
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Unit 3: Variables in Java
Chopin’s Nocturne.
Introduction to Python
Introduction to Programming
Introduction to Programming
Introduction to Programming
Presentation transcript:

Learning to Program in Python Concept 7 FUNCTIONS (Definitions) Practice and principles of good programming. The intention of these slides over several classes is to engage in tasks suing definitions. The KEY question for this lesson should be : When to use functions? The lesson should provide the answers: 1. Through the tasks and 2. Explicit reasons why functions are essential to efficient, readable code and CT skills

Learning Intentions Create definitions Pass parameters to definitions From this lesson the students will be able to: Create definitions Pass parameters to definitions Design solutions using their own definitions The intention of this lesson is for students to solve as many problems as possible using definitions and in the process learning more about what defintions can do. Also students will be revising previous concepts such as variables, loops, lists and dictionaries. Choose the puzzles and tasks that suit the level of the students.

Passing Variables to a Definition Predict what the definition above will do when called. Encourage the students to predict what each line in the body of the definition will do, and how you would call this definition.

Passing Variables to a Definition The function is passed a list and a new element for the list. (These are the inputs, or parameters, of the function. In this case mylist and mystring) The new element is added on to the start of the list, and the list printed out. The general syntax is : def function_name(parameters): Encourage the students to predict what each line in the body of the definition will do, and how you would call this definition.

Call Me Go to your IDE and try defining the function. Encourage the PRIMM concept of reading and modifying the code. (Predict Run Investigate Modify Make) In this case, the students could append instead of insert. Or perhaps do both, so the list is topped and tailed with the new element. Go to your IDE and try defining the function. Pass a few more parameters to it to lengthen the list. You can also append a new element to the end of the list. LO 1.22 students should be able to read, write, test, and modify computer programs

Create Your Own Create a list containing the names of 2 famous people. For example : [“Lincoln” , “Yeats”] Write a program, using definitions, that allows the user to add more elements to the list. The definition should update the list and print it out each time it is updated. The user wants to add between 1 and 10 people to the list. The user must be asked the number of new people they want to add. As an extension to this exercise, some groups of students could be asked to write definitions to add or remove an element. The user could then be asked what they want to do regarding the list. The Scratch symbol indicates that this task should also be done in Scratch (or similar block-based language). The html resource, containing the Scratch program and guiding the students, is part of the same folder as this Python resource, on the ncca.ie website. Encourage the students to edit this html code with their work and reflections as part of their ePortfolio. LO 2.6 students should be able to construct algorithms using appropriate sequences, selections/conditionals, loops and operators to solve a range of problems, to fulfil a specific requirement

FUNCTIONS We can pass in as many parameters to a function as we like: def fancyNumberOfInputs(a, b, c, x, y, z) BUT we can only ever have one (if any) return from a function. https://docs.python.org/3/library/functions.html ...... Gives a list of Python built-in functions. The definitions to open or close a file, e.g. myFile = open(“Dbfamouepeople.txt”,’w’), are good examples of definitions we have used that are pass multiple inputs (or parameters)

Return my Call The command return sends back the variable to wherever the function was called. (usually in the main program) What data type will be returned from the halveMe function? It is a float because of the division by 2.

Return my Call The output is clearly of type float, as shown above. Encourage students to tweak this definition to another similar one. E.g. trebleMe and subtract 2. Try it out. Change the function to perform a different task, such as doubleMe.

Predict what this program will do when executed. ONE way or another Encourage students to Think Pair and Share their predictions, and then write the program in their IDE to check their predictions. Predict what this program will do when executed.

ONE way or another We have a definition called calculate() which is called from inside the loop. Calculate() takes in a number and divides that number by 100, returning the answer to the loop. The function converts a percentage to its number equivalent e.g. 15% = 0.15 Encourage students to Think Pair and Share their predictions, and then write the program in their IDE to check their predictions.

Create your Own A Classic Example Create a function to convert a number given in degrees Celsius to its Fahrenheit equivalent. Call your definition celToFah(). Its input is a temperature in degrees Celsius. The formula for conversion is : Fah = (9 * Cel) / 5 + 32 The UI should clearly state the input and the converted output. Encourage paired programming and students testing and evaluating each other’s programs and UIs.

Sample Code A skeletal example of what some code might look like. Ask the students what they would do to enhance this program. For example, add comments, print out statements explaining what the program does, allow decimals and thus cast the input as float, etc….. LO 2.7 students should be able to implement algorithms using a programming language to solve a range of problems

CHALLENGE Write 3 programs, using definitions, that offer a User Interface to do the following conversions : Convert miles to kilometres. Convert kilometres to miles. Converts in both directions! (Clue for definition 3 : One of the input parameters should tell your function which conversion to do. For example, if it is a Boolean variable, and called miles2kilometres, then if it is true the conversion is from miles to kilometres. If it is false, it is km to miles.) The first two conversions are similar to the previous slides. The third one is similar to the exercise in the first lesson on functions (The 5 step algorithm) where the 3rd and 4th steps were handled by one function. In the case of the 3rd definition above, a Boolean variable is described. E.g def convertDistance(distance, miles2kilometres) : It could be called in the following way …. Distance = convertDistance(48, True) …

The following task was completed during While loops. The Scratch challenge is to get the countDOWN and countUP numbers scrolling across the screen. This extension of this lower and upperLimit counting task can be completed in Scratch. So as the numbers go from say 10 to 11 to 12, these numbers must scroll across the screen from left to right and then from right to left during the countdown. There must be a use of definitions during the task. The Scratch symbol indicates that this task should also be done in Scratch (or similar block-based language). The html resource, containing the Scratch program and guiding the students, is part of the same folder on functions as this Python resource. What line of code is behind me?

A previous Challenge from the Dictionary Lessons Let’s say we have a string “dictionaries and lists” We need to count how many times each letter appears in the string. The information for each letter must be stored, easily accessed and also printed out. In the NCCA resources on understanding Dictionaries, this example was studied. Encourage students, in pairs, to brainstorm (coggle might help here) on the algorithm and flow of their program, before writing any code. If using Cheat Sheets, encourage students to use them. One of the NCCA examples for ALT2 (Analytics) analyses the words in a txt file in a similar fashion. Using ThinkPairShare, design an algorithm and pseudo code to solve this problem. LO 1.7 students should be able to develop algorithms to implement chosen solutions

Sample code from Previous Letter Count Challenge The sample code strips out white spaces (Cleans the data). This is optional. Also, the first for loop could be made more concise by using : for letter in sampleString : Students will now be asked to enhance their program by using definitions, and for example, asking the user for a string to analyse..

Enhancing the Letter Count Program . Using the sample code or otherwise, write a program that: Uses definitions to ask the user for a string clean the string of spaces and count the letters in the string print the analysis in a clear readable format. If using the sample code, can you use less lines of code to perform the same task? . The task is to enhance the program to analyse a string for recurrence of the same letters, using different definitions for each subtask. The previous slide’s sample code will output as shown above. LO 3.5 students should be able to structure and transform raw data to prepare it for analysis

Learning Review Create definitions Pass parameters to definitions From this lesson I am able to: Create definitions Pass parameters to definitions Design solutions using my own definitions Students should becomePractice and principles of good programming. The intention of these slides over several classes is to engage in tasks suing definitions. The KEY question for this lesson should be : When to use functions? The lesson should provide the answers: 1. Through the tasks and 2. Explicit reasons why functions are essential to efficient, readable code and CT skills