Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to CS Nov 17, 2016.

Similar presentations


Presentation on theme: "Intro to CS Nov 17, 2016."— Presentation transcript:

1 Intro to CS Nov 17, 2016

2 Today Additional operations in list Additional operations in string
Dictionary

3 list data structure A sequence of values. Order matters. Values can be any type. They are called elements or sometimes items. empty_list = [] num_list = [17, 123] mix_list = [34, 26, ‘abc’, 56, ‘xy’] Use index to access data (positive or negative index) [34, 26, ‘abc’, 56, ‘xy’] | | | | | index: or :

4 access a list mix_list = [34, 26, ‘abc’, 56, ‘xy’]
access individual element with index mix_list[2] = ‘abc’ mix_list[-2] = 56 In operator is for element search. Return True or False if 26 in mix_list: print(“find 26!!!”) Traverse a list with For loop for elem in mix_list: elem = elem*2 print(elem, end=‘,’)

5 list slice syntax: list_name[start : stop : step]
example: txt=[11,22, 33,44,55,66,77] txt[0:5]  start=0, stop= 5-1 txt[2:]  start=2, stop=end txt[:3]  start=0, stop= 3-1 txt[0::2]  start=0, stop= end, step=2 txt[::]  print whole list

6 list execise myList = [‘a’, ‘b’, ‘c’, ’d’, ‘e’, ‘f’,’c’]
write down the result of these expressions myList[3] = myList[-3] = myList[1:4] = ‘d’ in myList result is: ‘y’ in myList result is:

7 Lab 4.3.1 Write a python program that takes user input from stdin
Reverse the input string Print the reversed string to stdout

8 Lab 4.3.1 solution inStr = input("Please enter your string: ")
newStr = "" for idx in range(len(inStr) - 1, -1, -1): newStr = newStr + inStr[idx] print("Here is the reversed string: ", newStr)

9 Lab 4.3.1 solution 2 inStr = input("Please enter your string: ")
print("Solution 2: ", inStr[::-1])

10 [::-1] works for list myList = [1, 2, 3, 4]
print(”Reversed list: ", myList[::-1])

11 Separator in s.split myStr = 'Python*Is*Fun!'
myList = myStr.split(sep = "*") print(myList) >>>['Python', 'Is', 'Fun!']

12 Lab 4.3.2 Print the addresses using a for loop
adrList = ["Name = Bill Jones Address=123 Main Street" , "Name = Wm Smith Address=948 High Street" , "Name = Abe Lincoln Address=657 Gettysburg,DC", "Name = Boss Hog Address=123 Deep South" ]

13 Lab 4.3.2 solution for item in adrList:
print (item.split("Address=")[1])

14 Dictionary In Python, a “dictionary” is a data container that stores multiple items of data as a list of key:value pairs The key:value pairs are unordered Values are referenced by their associated keys Key must be unique within a dictionary Key is typically a string name

15 Dictionary Example Create a dictionary
>>>myDict ={'name':'Bob', 'ref':'Python', 'sys':'Win'} >>>print(myDict) {'ref': 'Python', 'sys': 'Win', 'name': 'Bob'} Display a value referenced by its key >>>Print(‘\nReference:’, myDict[‘ref’]) Reference: Python Display all keys >>>Print(‘\nKeys:’, myDict.keys()) Keys: dict_keys(['sys', 'name', 'ref'])

16 Dictionary Example Delete one pair from the dictionary
>>>del myDict['name'] Add a new pair >>>myDict[‘user’] = ‘Tom’ Display the new dictionary >>>print('Dictionary:', mydict) Dictionary: {'sys': 'Win', 'ref': 'Python', 'user': 'Tom'} Search a key >>>print('Is there a name key?:', 'name' in myDict) Is there a name key?: False

17 Sorted Keys() returns a list of keys in random order
Use sorted() to sort the keys >>>sortedKeys = sorted(myDict.keys()) >>>print('\nsortedKeys:', sortedKeys) sortedKeys: ['ref', 'sys', 'user']

18 Summary Additional operations in list Additional operations in string
Dictionary


Download ppt "Intro to CS Nov 17, 2016."

Similar presentations


Ads by Google