Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Functions. A set of statements (lines of code) that can be run repeatedly Goals: Learning Python by Lutz and Ascher –Code reuse –Procedural decomposition.
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.
 Pearson Education, Inc. All rights reserved Arrays.
Basic Input/Output and Variables Ethan Cerami New York
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Python Programming Fundamentals
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lists in Python.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Input, Output, and Processing
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
If statements while loop for loop
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
More Functions with Return Values CS303E: Elements of Computers and Programming.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Documenting a function. Documenting a function definition We have a template for information that we need you to put at the top of each function - if.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
 2005 Pearson Education, Inc. All rights reserved Arrays.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
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.
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.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Programming
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
CS 1110 Introduction to Programming Spring 2017
Presented By S.Yamuna AP/IT
Topic: Functions – Part 2
Functions CIS 40 – Introduction to Programming in Python
CSC113: Computer Programming (Theory = 03, Lab = 01)
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lesson 06: Functions Class Chat: Attendance: Participation
Introduction to Programming
Writing Functions( ) (Part 4)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Loops and Simple Functions
Introduction to Programming
GCSE Computing Mini Assignment.
Presentation transcript:

Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions -> operators -> expressions objects -> data model

Program Structure [import statements] [constants] def [function1 definition]: … return [values] def [function2 definition]: … return [values] main program In an exam situation: - do not put functions inside the main or other functions - if asked for a function, only supply the function** - if asked for a main/program, only supply the main** - if asked for a main program and a function, write both ** - ** and any related imports and constants

Libraries import library_name [constants] def [function1 definition]: … return [values] def [function2 definition]: … return [values] main program In an exam situation: - assume all functions for a question are in one file e.g.import func … ans = func.f1(p1,p2) ans2 = func.f2(ans) e.g.import math … r = math.radians(degrees) -> you will be supplied with a list of libraries and the functions you can use from those libraries

Main Program [import library_name] [constants] #main program statement … statement In an exam situation: - main programs are a collection of statements used primarily to test knowledge of: 1) simple program constructs 2) input and output 3) calling functions : - from Python libraries - ones you code in the exam - ones you are given - use constants if it will make your code more readable and/or simpler

Functions [import library_name] [constants] def name(argument list): statement … statement return [values] In an exam situation: - functions will be in the majority of questions - indentation important - return statement important - import libraries, if appropriate - use constants if it will make your code more readable and/or simpler - give preconditions and postconditions only if specifically asked for

Functions and Methods Functions: function_name(parameters) 1. user defined – the functions you write 2. built-in – the ones that come with Python, e.g. len(seq), print(string) Methods: type.function_name(parameters) -> built-in functions that are tied to a type 1. common methods suitable for a range of types, e.g. seq.count(item) 2. methods suitable for mutable types, e.g. seq.append(item) 3. methods suitable for a specific type, e.g. string.format(parameters)

Preconditions, Postconditions [import library_name] [constants] def [function definition]: """ Preconditions: parameter name - description (unit) (type, constraints) Postconditions: returns: return value name - description (unit) (type) prints: print value name – description (unit) (type) """ … remainder of function code return [return values]

def most_popular(file, n): """ Returns the most popular female and male names from a data file Preconditions: file - the names data file variable (file) n - number of female/male names to get from file (int > 0) Postconditions: returns: names_female - the n top female names (list) names_male - the n top male names (list) """

Write a function that returns one hand given the deck and the number of cards to be dealt. Use random.shuffle to shuffle the deck before dealing the hand. [variation Assignment 16/17] import random def deal_shuffle(deck, size): random.shuffle(deck) hand = [] for i in range(size): card = deck.pop() hand.append(card) return hand In an exam situation: - if you cannot do the question with the specified “givens”, add your own with comment

Write a function to print out one hand of cards given the hand as a list. If hand = [6, 8, 9,11,13], output would be J K. [A16, Q2] CARDS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] <- if line is long, continue on next line def print_hand(hand): for card in hand: print("{}".format(CARDS[card-1]), end=' ') print() return

Write a program using functions to generate a number of random numbers and determine the average as a floating point number. Output the result to one decimal digit. import random def find_avg(n, m): total = 0 for i in range(n): x = random.randint(0, m) total = total + x avg = total / n return avg n = int(input("Number of values: ")) max_val = int(input("Maximum value: ")) a = find_avg(n, max_val) print("Average: {0:.1f}".format(a)) In an exam situation: We are interested to see how you will break up the program. Do you know what needs to be passed and what needs to be returned? When is it appropriate to print in the function? VARIATION: Question worded as “Write a function to calculate …” with no parameters or return values specified.

Same question - using lists import random def generate_random_list(n,m): rlist=[] for i in range(n): x = random.randint(0, m) rlist.append(x) return rlist def find_avg(data_list): total = 0 for item in data_list: total = total + item avg = total / len(data_list) return avg n = int(input("Number of values: ")) max_val = int(input("Maximum value: ")) data = generate_random_list(n, max_val) a = find_avg(data) print("Average: {0:.1f}".format(a)) In an exam situation: - If we specify that a particular technique or data structure must be used, you must use it; otherwise, choose the approach you prefer. - You must always use looping constructs when appropriate. STUDY NOTE: Looking back at old assignments and labs are there different ways to do these questions now that you know more techniques?