Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:

Similar presentations


Presentation on theme: "SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:"— Presentation transcript:

1 SEQUENCES:STRINGS,LISTS AND TUPLES

2 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’ HELLO 01234 -5-4-3-2 Figure2-1:How sequence of elements are stored and accessed

3 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

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

5 C REATING 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 aString Hello world >>>anotherString ‘Python is cool!’ >>> s=str(range(4)) >>> s ‘[0,1,2,3]’

6 U PDATE 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’

7 R EMOVE 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!’

8 C ONTINUE.. To clear or remove a string, assign an empty string or use the del statement. Examples: >>>aString=‘ ‘ >>>aString ‘ >>>del aString

9 S TRINGS AND OPERATORS >>> str1=‘abc’ >>>str2=‘lmn’ >>>str3=‘xyz’ >>>str1<str2 True >>>str2 !=str3 True >>>str1<str3 and str2==‘xyz’ False Strings are compared lexicographically (ASCII value order)

10 M EMBERSHIP 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()).

11 C ONTINUE.. >>> ‘bc’ in ‘abcd’ True >>>’n’ in ‘abcd’ False >>>’nm’ not in ‘abcd’ True

12 B UILT - 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

13 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(‘ABCDa bcd’) ‘A’

14 CONTINUE enumerate() - Return an enumerate object Example: s=‘foobar’ >>>For i,t in enumerate (s): … print i,t 0 f 1 o 2 0 3 b 4 a 5 r Assign ‘foobar’ to s- enumerate object

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

16 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

17 Use input for entering NUMBER Use raw_input for entering string I NPUT AND V ARIABLES num = input("Type in a Number: ") str = raw_input("Type in a string:") print "num =", num print "num * 2 =",num*2 print "str =", str print "str * 2 =",str*2 IMP ORTANT OUTPUT : Type in a Number: 12.34 Type in a String: Hello num = 12.34 num * 2 = 24.68 str = Hello str * 2 = HelloHello

18 C ONTINUE.. 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

19 C ONTINUE.. 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.

20 E XAMPLE #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 distance Rate:5 Distance:10 Time: 2

21 C ONTINUE.. 5 rate distance 10 10/5=2 time = distance/rate Variables

22 E XERCISE 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

23 E XERCISE 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

24 E XERCISE 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 7 Example output: 3 nickels and 7 dimes = 85 cents. Design the solution with flowchart and transform the flowchart into Python’s code

25 S OLUTION – EXERCISE 3

26 S OLUTION – E 3 >>> print ('enter number of nickels and dimes ') enter number of nickels and dimes >>> num1=input('nickles: ') nickles: 3 >>> num2=input('dimes;') dimes;7 >>> total=num1*5+num2*10 >>> print num1,'nickles and ',num2,'dimes =',total 3 nickles and 7 dimes = 85

27 E XERCISE 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.

28 P SEUDOCODE – EXERCISE 4 Begin input 5 numbers in list input first number at index 0 input second number at index 1 input third number at index 2 input fourth number at index 3 input fifth number at index 4 Total = index 0 +index 1+index 2+index 3+index 4 The average is total/5 Print average End

29 S OLUTION – EXERCISE 4 >>> list=[0,0,0,0,0] >>> list[0]=input('input number at index 0:') input number at index 0:2 >>> list[1]=input ('input number at index 1:') input number at index 1:2 >>> list[2]=input('input number at index 2:') input number at index 2:2 >>> list[3]=input('input number at index 3:') input number at index 3:2 >>> list[4]=input('input number at index 4:') input number at index 4:2 >>> total=list[0]+list[1]+list[2]+list[3]+list[4] print "the average is :",total/5 the average is : 2


Download ppt "SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:"

Similar presentations


Ads by Google