Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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)

7 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]

8 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) ------------------------------------------------------- """

9 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

10 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 6 8 9 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

11 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.

12 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?


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

Similar presentations


Ads by Google