Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data types Numeric types Sequence types float int bool list str

Similar presentations


Presentation on theme: "Data types Numeric types Sequence types float int bool list str"— Presentation transcript:

1 Data types Numeric types Sequence types float int bool list str
Python maintains data by type : float Numeric types >>> type(3.14) <type ‘float’> int >>> type(True) <type ‘bool’> bool list >>> type(‘writer’) <type ‘str’> Sequence types str >>> type([1,2,3]) <type ‘list’>

2 string functions What are the following strings? s1 = "ha"
len + * str(42) returns '42' converts input to a string len('42') returns 2 returns the string’s length 'XL' + 'II' returns 'XLII' concatenates strings 'VI'*7 returns 'VIVIVIVIVIVIVI' repeats strings s1 = "ha" Given these strings s2 = "t" Strings have some of their own operations defined for them – addition, multiplication (repetition of the same string, similar to math), length of string function (len), and conversion function (str) Indexing and slicing are useful operations for chopping strings into smaller pieces. The string data type also supports operations for putting strings together. Two handy operators are concatenation (+) and repetition (*) Concatenation builds a string by .gluing. two strings together. Repetition builds a string by multiple concatenations of a string with itself. Another useful function is len which tells how many characters are in a string. note that * is commutative, + is not hat 'hahathathat' What are the following strings? s1 + s2 hat 2*s1 + s2 + 2*(s1+s2) hahathathat

3 s = 'Bucknell University'
String surgery s = 'Bucknell University' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 s[ ] indexes into the string, returning a one-character string index ‘B’ s[0] returns ‘l’ s[6] returns Let’s remember what a string is: a sequence of characters. One thing we might want to do is access the individual characters that make up the string. In Python, this can be done through the operation of indexing. We can think of the positions in a string as being numbered, starting from the left with 0. Indexing is used in string expressions to access a specific character position in the string. The general form for indexing is s[6] = l s[11] returns ‘i' len(s)  19 s[len(s)] return "string index out of range" s[11] returns ‘i’ What returns 'v'? s[12] len(s) returns 19 s[len(s)] returns ERROR python != English

4 s = 'Bucknell University'
Negative indices… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 s = 'Bucknell University' -19 -17 -15 -13 -11 -9 -7 -5 -3 -1 -18 -16 -14 -12 -10 -8 -6 -4 -2 Negative indices count backwards from the end! s[-1] returns ‘y’ s[-10]  'U' s[-0] = 'B' s[-10] returns ‘U’ s[-0] returns ‘B’

5 s = 'Bucknell University'
Slicing what if you want a bigger piece of the pie??? s = 'Bucknell University' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 s[ : ] slices the string, returning a substring the first index is the first character of the slice the second index is ONE AFTER the last character s[0:8] returns 'Bucknell' Second parameter means up to but no including that index. Just as if we were saying: My land is from here to the road. Usually it would mean that the road is not yours. Note that missing the beginning index returns the start of the string s[9:18] returns 'Universit' s[17:] returns 'ty' s[:] returns 'Bucknell University'

6 s = 'Bucknell University'
Slicing s = 'Bucknell University' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 s[ : ] slices the string, returning a substring s[15:-1] What are these slices? ‘sit’ s[15:-1]  'sit' s[4:7]  'nel' 'Uni'  s[9:12] or s[9:-7] 'Universe'  s[9:16] + 'e' s[4:7] ‘nel’ 'Uni' s[9:12] How do you get: 'Universe' s[9:16] + ‘e’

7 if you don't want your neighbor to get any… s = 'Bucknell University'
Skip-Slicing if you don't want your neighbor to get any… s = 'Bucknell University' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:10:2] returns 'Bcnl ' returns ’tisr' s[17:13:-1] s[17:13:-1]  'tisr‘ 'clnr'  s[2:15:4] 'm' + s[1::6]  'mule‘ s[::-1]  'ytisrevinU llenkcuB' 'clnr' s[2:15:4] What skip-slice returns 'm' + s[1::6] What does this return? returns ’mule' s[::-1] returns ’ytisrevinU llenkcuB'

8 Lists  collections of any data
L = [ 3.14, True, 'third', 42 ] Commas separate elements. Square brackets tell python you want a list. L = [ 3.14, [2,40], 'third', 42 ] Lists are more general than strings. Strings are always sequences of characters, whereas lists can contain values of any type. You can have a list in a list!

9 List operations len(L) L[0] L[0:1] 'hi' length indexing slicing
L = [ 3.14, [2,40], 'third', 42 ] len(L) L[0] L[0:1] length indexing slicing Write what each returns: len(L)  4 L[0]  3.14 'hi'  L[2][1:3] Indexing: This behavior is counterintuitive, but it might help to imagine the indices pointing between the characters The operator [:] returns a slice of a sequence. Slicing a portion of a list: create a new list, and copy the portion of the original list into this new list. How could you extract from L 'hi'

10 Repeats list a number of times
List operations + * concatenation multiplication Joins two lists Repeats list a number of times >>> lst = [1,2,3] >>> lst * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> P = [ 3.14, [2,40], 'third', 42] >>> R = ['a','b','c'] >>> P + R [3.14, [2, 40], 'third', 42, 'a', 'b', 'c']

11 The in operator – membership testing for lists and strings
>>>'i' in 'alien' True False >>> 3*'i' in 'alien' False >>> 'i' in 'team' >>> 'cs' in 'physics' True Be sure to point out the NOT >>> 'sleep' not in 'CSCI 203' True True >>> 42 in [41,42,43] >>> 42 in [ [42], '42' ] False

12 Mutable and immutable sequences
Lists are mutable Strings are immutable Once a string is created, individual elements of string or the string as a whole cannot be changed Individual items or entire slices can be replaced through assignment statements >>> st = 'ABC' >>> st[0] 'A' >>> st[0]='B' Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> st[0]='B' TypeError: 'str' object does not support item assignment >>> lst = ['A', 'B', 'C'] >>> lst ['A', 'B', 'C'] >>> lst[0] = 'B' ['B', 'B', 'C'] Lists are mutable. That means that the value of an item in a list can be modified with an assignment statement. Python Strings Strings, on the other hand, cannot be changed in place. Python strings are "immutable" which means they cannot be changed after they are created (Java strings also use this immutable style). It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example: >>> greeting = 'Hello, world!' >>> greeting[0] = 'J' TypeError: object does not support item assignment The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later. An item is one of the values in a sequence. The best you can do is create a new string that is a variation on the original: >>> greeting = 'Hello, world!' >>> new_greeting = 'J' + greeting[1:] >>> print (new_greeting) Jello, world! This example concatenates a new first letter onto a slice of greeting. It has no effect on the original string. Display in IDLE greeting, new_greeting and their ids Since strings can't be changed, we construct *new* strings as we go to represent computed values. So for example the expression ('hello' + 'there') takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'. >>> s="hello" >>> id(s) >>> s=s+"there"

13 Raising and razing lists
Answers Raising and razing lists pi = [3,1,4,1,5,9] L = [ 'pi', "isn't", [4,2] ] M = 'You need parentheses for chemistry !' 4 8 12 16 20 24 28 32 Part 1 Part 2 6 'pi' What is len(pi) What is L[0] ['pi'] 3 What is len(L) What is L[0:1] 5 'i' What is len(L[1]) What is L[0][1] [4,1] M[31:34] What is pi[2:4] What slice of M is 'try' pi[0:3] 'parent' What slice of pi is [3,1,4] What is M[9:15] What slice of pi is [3,4,5] pi[::2] What is M[::5] 'Yeah cs!' What are pi[0]*(pi[1] + pi[2]) and pi[0]*(pi[1:2] + pi[2:3]) ? 15 [1,4,1,4,1,4]


Download ppt "Data types Numeric types Sequence types float int bool list str"

Similar presentations


Ads by Google