Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 

Slides:



Advertisements
Similar presentations
Lecture 07 – Iterating through a list of numbers.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
Structured programming
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Classes 2 COMPSCI 105 S Principles of Computer Science.
Fundamentals of Python: From First Programs Through Data Structures
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Fundamentals of Python: First Programs
Programming for Linguists An Introduction to Python 24/11/2011.
Munster Programming Training
Introduction COMPSCI 105 SS 2015 Principles of Computer Science.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Programming Training Main Points: - Python Statements - Problems with selections.
Introduction. 2COMPSCI Computer Science Fundamentals.
Lecture 20 – Test revision COMPSCI 101 Principles of Programming.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Built-in Data Structures in Python An Introduction.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
Lecture 04 – Models of memory Mutable and immutable data.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Lists COMPSCI 105 S Principles of Computer Science.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Input, Output and Variables GCSE Computer Science – Python.
Control structure: Repetition - Part 1
COMPSCI 107 Computer Science Fundamentals
3.1 Fundamentals of algorithms
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
Chapter 2 - Introduction to C Programming
CS 115 Lecture 8 Structured Programming; for loops
Section 6: Sequences Chapter 4 and 5.
CMSC201 Computer Science I for Majors Lecture 09 – For Loops
Chapter 2 - Introduction to C Programming
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Coding Concepts (Sub- Programs)
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
4. sequence data type Rocky K. C. Chang 16 September 2018
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
For loops Taken from notes by Dr. Neil Moore
COMPUTER PROGRAMMING SKILLS
Introduction to Programming
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

Lecture 03 – Sequences of data

 At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules  Example code:  Perform calculations on the elements of a list  Determine if a number is prime or not  Convert a mark from a percentage into a letter grade 2COMPSCI Computer Science Fundamentals

 Used to generate integer numbers within a range of values  Includes the start value, but excludes the stop value  range(stop)  range of values from 0 to stop value  range(start, stop)  range of values from start to stop value  range(start, stop, step)  range of values from start to stop value, increasing by step each time 3COMPSCI Computer Science Fundamentals range(10) range(4, 10, 2) … 8 9 range(3, 8) range(10, 4, -2)

 A function is a sequence of instructions designed to perform a task, and is packaged as a unit.  Functions have a name  Functions accept arguments  Functions return values  Syntax  Indentation rather than braces are used to signify blocks of code  Variables defined within the scope of a function are not available outside the function 4COMPSCI Computer Science Fundamentals def rectangle_area(width, height): return width * height

 Write a function called triangle_area that calculates the area of a triangle given the base and height values  area = ½ base * height  Write a function that asks the user for the times tables to print, then prints out the times tables from 1 to 10 (inclusive) 5COMPSCI Computer Science Fundamentals Enter the times tables: 4 4 x 1 = 4 4 x 2 = 8 … 4 x 10 = 40 >>> a = triangle_area(10, 20) >>> print(a) 100.0

 Determine if a number is prime or not  A number less than 2 is not prime  A number equal or greater than 2 is prime if it is not divisible by any other number except itself and 1 6COMPSCI Computer Science Fundamentals def is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True

 Code is stored in modules  We want to reuse code as much as possible  Build up libraries of code  Importing a module allows you to access code in that module  Python.org has a list of all the modules and functions provided 7COMPSCI Computer Science Fundamentals >>> import math >>> math.pi >>> math.sqrt(4) 2.0

 Python provides a wide range of built-in functions, including  round(value [, number of decimal places])  max(value, value [, value …])  min(value, value [, value …])  float(value)  int(value)  str(value)  type(value) 8COMPSCI Computer Science Fundamentals

 Sequences allow you to store values in an organized fashion.  strings, lists, tuples  9COMPSCI Computer Science Fundamentals

 Strings are a sequence of characters  Strings also have a number of other functions that can be used  split() is especially useful 10COMPSCI Computer Science Fundamentals >>> name = 'Andrew' >>> name[0] 'A' >>> 'd' in name True >>> len(name) 6 >>> name + ' ' + 'Luxton-Reilly' 'Andrew Luxton-Reilly'

 Write a function that determines whether a given string of text contains a vowel or not. The function should return True if the text contains a vowel. 11COMPSCI Computer Science Fundamentals

 Lists are a built-in type in Python  Use square brackets to signify a list  Lists can contain any type of data, or any mixture of data 12COMPSCI Computer Science Fundamentals >>> [1, 2, 3] [1, 2, 3] >>> ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] >>> [1, 5.899, 'Hello'] [1, 5.899, 'Hello']

 Numerous list functions are supported  Use help(list) to find out the functions  Use Python.org to find out more 13COMPSCI Computer Science Fundamentals >>> x = [1, 2, 3] >>> len(x) 3 >>> x + [4] [1, 2, 3, 4] >>> x += [5] [1, 2, 3, 5] >>> 3 in x True >>> x[0] 1

 Write a function that sums the elements of a list that contains numbers.  Write a function that accepts a list and returns a new list with the same contents, but in reverse order. 14COMPSCI Computer Science Fundamentals

 A piece of a sequence can be obtained using the following syntax  sequence_name[x:y]  where x is the index of the first element and y is the index after the last element 15COMPSCI Computer Science Fundamentals >>> name = 'Andrew' >>> name[0:0] '' >>> name[0:1] 'A' >>> name[1:4] 'ndr'

 Actually, the syntax allows for a third value, used to define the step size between elements included in the slice. If a value if omitted, it defaults to [start:end:1]  If the step size is negative, it starts at the end and steps backward towards the start. 16COMPSCI Computer Science Fundamentals >>> name = 'Andrew' >>> name[:4:2] 'ad' >>> name = 'Andrew' >>> name[::-1] 'werdnA'

 Used to iterate through a sequence 17COMPSCI Computer Science Fundamentals numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) name = "Andrew" for c in name: print(c)

 Write a function that returns a list containing the squares of all the values between 1 and n (inclusive).  Write a function that counts the number of vowels in a given string. 18COMPSCI Computer Science Fundamentals >>> squares(5) [1, 4, 9, 16, 25]

 Functions are designed to perform a well-defined task.  Functions can be defined with parameters to generalise a task  Functions always return a value, the default return value is None  Sequences consist of data that is organised and indexed as a sequence of values 19COMPSCI Computer Science Fundamentals