sequences:strings,lists and tuples. sequences are items that are ordered sequentially and...

25
SEQUENCES:STRINGS,LISTS AND TUPLES

Upload: dorcas-weaver

Post on 20-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

C ONTINUE.. Sequence types works with all standard type operators (*,/,+,**) There are operators belongs to sequence type operators as below: Sequence operatorFunction seq [ ind ]Element located at index ind of seq seq [ ind1:ind2 ]Elements from ind1 up to but not including ind2 of seq seq * expr seq repeated expr times seq1 + seq2 Concatenates sequences seq1 and seq2 Obj in seq Tests if obj is a member of sequence seq Obj not in seq Tests if obj is not a member of sequence seq

TRANSCRIPT

Page 1: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

SEQUENCES:STRINGS,LISTS AND TUPLES

Page 2: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

SEQUENCES Are items that are ordered sequentially and

accessible via index offsets into its set of elements.

Examples: strings, lists and tuples String – consists of a sequence of characters

String =“Hello” The first character of string “Hello” is ‘H’

H E L L O0 1 2 3 4

-5 -4 -3 -2 -1

Figure2-1:How sequence of elements are stored and accessed

Page 3: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE.. Sequence types works with all standard type

operators (*,/,+,**) There are operators belongs to sequence type

operators as below:Sequence operator

Function

seq[ind] Element located at index ind of seqseq[ind1:ind2] Elements from ind1 up to but not

including ind2 of seqseq * expr seq repeated expr timesseq1 + seq2 Concatenates sequences seq1 and

seq2Obj in seq Tests if obj is a member of sequence

seqObj not in seq Tests if obj is not a member of

sequence seq

Page 4: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

STRINGS Strings can be created by enclosing

characters in quotes. Single ‘ ’, double ‘” ” are same in Python Python used raw_string operator to create

literal quotes Strings are a literal or scalar type means that

they are treated by the interpreter as a singular value and not are not containers that hold other Python objects.

String are immutable meaning that changing an element of a string requires creating a new string.

Page 5: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CREATING AND ASSIGNING STRINGS Can assign using scalar value or having str ( )

factory function make one and assigning it to a variable.

examples:>>> aString =‘Hello world!’>>> anotherString =“Python is cool!”Print aStringHello world>>>anotherString‘Python is cool!’>>> s=str(range(4))>>> s‘[0,1,2,3]’

Page 6: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

UPDATE STRING String can be update by reassigning a

variable to another string. The new value can be related to its previous

value or to a completely new string Example:>>>aString =‘Hello World!’>>>aString=aString[:6] +’Python!’>>> aString‘Hello Python!’>>>aString =‘new string’>>>aString‘new string’

Page 7: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

REMOVE CHARACTERS AND STRINGS Recall- string are immutable meaning that it

individual characters cannot be remove from an existing string.

To allow this, the string needs to be empty or put together another string that drops the pieces that were not interested in.

Example: we want to remove one letter from “Hello World!”, sat letter l.

>>>aString =‘Hello World!’>>>aString= aString[:3]+aString[4:]>>>aString‘Helo World!’

Page 8: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE.. To clear or remove a string, assign an empty

string or use the del statement. Examples:>>>aString=‘ ‘>>>aString‘ ‘>>>del aString

Page 9: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

STRINGS AND OPERATORS>>> str1=‘abc’>>>str2=‘lmn’>>>str3=‘xyz’>>>str1<str2True>>>str2 !=str3True>>>str1<str3 and str2==‘xyz’False

Strings are compared

lexicographically (ASCII value

order)

Page 10: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

MEMBERSHIP 2 operators –in, not in The membership asks whether a string / sub

string appears in another string. If that character appears in the string, return

True else False Membership operation is not used to

determine if a substring is within a string. To determine substring in string used the

string methods or string module functions such as find()/index() (and their brethren rfind() and rindex()).

Page 11: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE..>>> ‘bc’ in ‘abcd’True>>>’n’ in ‘abcd’False>>>’nm’ not in ‘abcd’True

Page 12: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

BUILT-IN FUNCTIONS Standard type functions – cmp()

Similar to value comparison operators, the cmp() build-in function also performs a lexicographic (ASCII value –based) comparison for strings.

>>>str1=‘abc’>>>str2=‘lmn’>>>str3=‘xyz’>>>cmp(str1,str2)-11 [ 97-108=-11, based on character a=97,

l=108]>>>cmp(str3,str1)23 >>>cmp(str2,’lmn’)0

Page 13: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE Sequence type

functions – len() Returns the number

of characters in the string

>>>str1 = ‘abc’>>>len(str1)3>>>len(‘Hello world!’)12 max() and min()>>> str2=‘lmn’>>>str3=‘xyz’>>>max(str2)‘n’>>>min(str3)‘x’

Max() return the greatest value and min() the smallest

value of characters in strings based on

lexicographic order

>>> min(‘abc12cd’‘1’>>>min(‘ABCDabcd’)‘A’

Page 14: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE enumerate() - Return an enumerate objectExample:s=‘foobar’>>>For i,t in enumerate (s):

print i,t0 f1 o2 03 b4 a5 r

Assign ‘foobar’ to s-enumerate

object

Page 15: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE zip() - This function returns a list of tuples>>> s,t=‘foa’, ’obr’>>>zip(s,t)[(‘f’,’o’,),(‘o’,’b’),(‘a’,’r’)]

f O a o b r

Page 16: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE String type functions

raw_input() – prompts the user with a given string and accepts and returns a user-input string.

>>>user_input = raw_input(“Enter your name: “)

Enter your name: John Doe>>>user_input‘John Doe’>>>len(user_input)8

Page 17: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

Use input for entering NUMBER

Use raw_input for entering string

INPUT AND VARIABLES

num = input("Type in a Number: ")str = raw_input("Type in a string:")

print "num =", numprint "num * 2 =",num*2print "str =", strprint "str * 2 =",str*2IMPORTANT

OUTPUT :

Type in a Number: 12.34Type in a String: Hellonum = 12.34num * 2 = 24.68str = Hellostr * 2 = HelloHello

Page 18: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE.. num variable gets data from input str variable gets data from raw_input If you want the user to type in a number use input because it returns a number

If you want the user to type in a string use raw_input because it returns a string

Page 19: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE.. Numbers are of type int or float (which are

short for ’integer’ and ’floating point’ respectively) Strings are of type string Integers and floats can be worked on by

mathematical functions, strings cannot.

Page 20: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

EXAMPLE #This programs calculates rate and distance problems

print "Input a rate and a distance"rate = input("Rate:")distance = input("Distance:")print "Time:",distance/rate

OUTPUT :Input a rate and a distanceRate:5Distance:10Time: 2

Page 21: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

CONTINUE..

5

ratedistance

10

10/5=2

time = distance/rateVariables

Variables

Page 22: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

EXERCISE 1

As a programmer you were asked to write a program that accepts user name, age and gender. Display user name, age and gender.

Based on the given problem: Identify the input

Determine the data type for each input /output Identify the output Design the solution using flowchart and

pseudocode Transform the design to Python language

Page 23: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

EXERCISE 2 As a programmer, you were asked to write a

program that able to compare the two input strings. Your program must allow user to input both strings and make a comparison on which string is bigger that the other. Based on the given problem: Identify the input Identify the output Design the solution using flowchart and

pseudocode Transform the design to Python language

Page 24: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

EXERCISE 3 Write a program to convert an input number of

nickels and dime into a total number of cents. For example, if the user inputs 3 and 7 for the number of nickels and dimes, respectively, the screen display at the end of the run would be :

Enter number of nickels and dimes 3 7Example output:3 nickels and 7 dimes = 85 cents.

Design the solution with flowchart and transform the flowchart into Python’s code

Page 25: SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible…

EXERCISE 4 Design a program using list that allow user to

input 5 numbers in a lists and get the average value.

Design your solution with a pseudocode and transform the design to Python’s code.