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

Slides:



Advertisements
Similar presentations
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Structured programming
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Programming for Linguists An Introduction to Python 17/11/2011.
Programming for Linguists An Introduction to Python 24/11/2011.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Course A201: Introduction to Programming 11/04/2010.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Strings CS303E: Elements of Computers and Programming.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Hossain Shahriar Announcement and reminder! Final exam date December 19: 4pm-6pm Topics to be covered in this lecture Sort.
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)
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
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.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Input, Output and Variables GCSE Computer Science – Python.
String and Lists Dr. José M. Reyes Álamo.
Python unit_4 review Tue/Wed, Dec 1-2
Python - Lists.
CS 100: Roadmap to Computing
Variables, Expressions, and IO
Introduction to Strings
Introduction to Strings
Basic Python Collective variables (sequences) Lists (arrays)
Creation, Traversal, Insertion and Removal
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Python - Strings.
Data types Numeric types Sequence types float int bool list str
CISC101 Reminders Assn 3 due tomorrow, 7pm.
String and Lists Dr. José M. Reyes Álamo.
Wednesday 09/23/13.
Introduction to Strings
Methods – on strings and other things
Introduction to Value-Returning Functions: Generating Random Numbers
15-110: Principles of Computing
Topics Basic String Operations String Slicing
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Introduction to Strings
Python Strings.
Topics Basic String Operations String Slicing
CISC101 Reminders Assignment 3 due today.
Introduction to Strings
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Topics Basic String Operations String Slicing
Presentation transcript:

Hossain Shahriar

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

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’

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

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

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

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

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

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

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

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

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()?

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

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

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