Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.

Similar presentations


Presentation on theme: "Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can."— Presentation transcript:

1 Hossain Shahriar shahriar@cs.queensu.ca

2 Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can be held after Dec 16 Topics to be covered in this lecture(s) String operators and function Modular programming

3 String operator Concatenation (+): “abc” + “def” >>> ‘abcdef ‘ Repeat (*): “abc”*3 >>> ‘abcabcabc’ 'abc'*'abc‘ >>> Error String elements (characters) can be accessed with index (start from zero) >>> str =‘hello’ >>> str[0] >>> ‘h’ >>> str[-1] # access elements from the reverse direction >>> ‘o’

4 String operator Note that string elements cannot be changed str =‘hello’ str[0]=‘x’ >>> Error You should create a new string even if you need to change just one character Let us assume that we want to replace “hello” with ‘xello’ >>> str2 = ‘x’ + str [1: 5] # obtain substring using slice >>>

5 String functions str.len(): returns the length of a string str.find(sub[, start[, end]]): returns the lowest index in the string where substring sub is found str.lstrip([chars]): returns a copy of the string with leading characters removed str.replace(old, new[, count]): returns a copy of the string with all occurrences of substring old replaced by new str.split: returns a list of the words in the string, using sep as the delimiter string

6 Modular programming For simplicity, we have been relying on one function so far! def main() : for i in range(5) : print i main() # main function call Python allows you to write class that contains one or more method definition and calls

7 Modular programming (class) class MyClass: i = 12345 def f(self): #method, not a function return 'hello world' c = MyClass() #instantiating a class print c.i >>> 123456 print c.f() >>> 'hello world'

8 Modular programming (hierarchies) A module can contain one or more of the following: Functions Classes A package is a collection of modules Python has a set of built-in functions (BIF) We have already used some of them input, print, range, open, close

9 Modular programming (class) Difference between a function and a method A method is defined within a class f method of MyClass Must be invoked by naming the class/object that owns the method Example: str.find, c.f() A function belongs to a module and is not defined in a class A function is invoked directly Example: all of our previous assignments use function call main()

10 Modular programming (module) Python offers useful modules and you can call their functions For example, math module has a square root function (sqrt) To call sqrt function, import the module math >>> import math >>>x = math.sqrt(4) >>> x >>> 2

11 Modular programming (cont.) Now, we will break a large program into small set of function definitions and calls We will only call main function, which subsequently does the rest of the task We need to first understand how function received arguments and return values

12 Modular programming (breaking the task) Let us assume that you want to develop a calculator Compute addition and subtraction of two input numbers The result is displayed to output User provides two numbers and a choice of operation Let us develop a modular programming plan We break the task into subtasks that are non overlapping Write two functions for the operations Write a function to receive numbers and invoke the operations based on the choice Q: How will we combine all the functions in main()?

13 Modular programming def add(a, b): return a +b def sub(a, b): return a-b def inputProcess(): a = input(‘give an integer’) b = input (‘give an integer’) c = input (‘choice (1 = addition, 2 = subtraction)’) if c ==1: print add (a,b) if c ==2: print sub (a,b) def main(): inputProcess () main()

14 Modular programming (handling missing parameter) def add (a = 10, b = 5): return a +b def sub(a, b): return a-b def inputProcess(): a = input(‘give an integer’) b = input (‘give an integer’) c = input (‘choice (1 = addition, 2 = subtraction) ’) if c ==1: print add (a) # result is a +5, where 5 is obtained from the default value if c ==2: print sub (a,b) def main(): inputProcess () main()

15 Modular programming (cont.) Q: How to return multiple values from a function? A: Save the values in a list and return the list def addsub (a, b): #we want to perform both add, sub c = [0, 0] #assume the initial results are zero and zero c[0] = a+b c[1] = a – b return c def main(): a, b = input (‘give two numbers’) result = addsub(a,b) print ‘add:’, c[0], ‘sub:’, c[1] main()


Download ppt "Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can."

Similar presentations


Ads by Google