print("Slide 28: Other Operators") #comparison operators print("A==B True only if the teo lists are exactly equal") print("A!=B True only if lists differ in anyway") print("Slide 29: Other OPerators") monthly = [0]*12 monthly produce = ['apple', 'orange', 'broccoli'] drygoods = ['cereal', 'bread'] groceries = produce + drygoods groceries print("Slide 30: List Modifying Methods") print("data.append(val) : add val to end of data list.") print("data.insert(i, val) : inserts val after first i elements in data list.") print("data.extend(otherlist) : appends otherlist to end of data list.") print("data.remove(val) : removes first occurence of val in data list.") print("pop() : removes and returns last elemt from data list.") print("Slide 31: List Modifying Methods (continued)") print("data.pop(i) : removes and returns elemt with index i in data list.") print("data[i] = val : replaces element at index i in data list with val.") print("data.reverse() : reverses order of data list elements.") print("data.sort() : sorts data list into increasing order.") print("Slide 32: List Methods Returing a Value") print("len(data) : return current length of data list.") print("data[i] : returns element at index i from data list.") print("val in data : return True if val in data list else False.") print("data.count(val) : returns number of occurrences of val in data list.") print("data.index(val) : returns index of earliest occurence of val in data list.") print("Slide 33: List Methods Returning a Value (continued)") print('''data.index(val, start) : same as previous index method except it starts looking at start rather than at start of data list''') print('''data.index(val, start, stop) : same as above except stops just short of stop index''') print('''dataA == dataB : Returns True if contents of dataA are pairwise identical with contents of dataB, else False.''') print("Slide 34: List Methods Returning a Value (continued)") print("dataA!=dataB : Returns True if contents not pairwise identical else False.") print('''Returns True if dataA is lexicographically (dictionary order) less than dataB, else False.''') print("Slide 35: Lists Generating a New List (continued).") print('''data[start:stop] : Return new list that is a "slice" of the original. It includes elements from start, up to but not including stop.''') print('''data[start:stop:step] : same as slice method above except now stepsize is whatever the step variable is.''') print("Slide: Lists Generating a New List (continued)") print('''dataA + dataB : Generating a third list that is dataB items added to the end of dataA. 'ABC' + 'DEF' = 'ABCDEF'.''') print('''Generates a new list of data items repeated k times. 'ABC'*3 becomes 'ABCABCABC'.''') print('''dataA += dataB : dataA becomes dataA with dataB added to the end. This is the same as dataA = dataA + dataB.''') print('''data*=k : data becomes data k times. This is the same data = data*k''') print("List, Tuple, and Str") print("Lists are mutable meaning that they can be changed.") print("Other sequence types are tuple and str") #tuples use parenthesis () around the items separated by commas. They are immutable. #str enclosed in single quotes/double quotes. Also immutable. #many operations are the same for these classes. Obviously list operations where #the list is changed are not possible with tuples and strings print("Slide 38: Str Class") print("Strings are immutable") #remeber help(str) can give string methods. print('str() creates an empty string string. Can also assign using: strValue=".') print("String Literals (constants) enclosed in single quotes/double quotes.") print('''A \n causes a new line. The \ is sometimes called the quote character so that instead of n we get the new line character''') print("Slide 39: Behaviors Common to Lists and Strings") greeting = 'Hello' len(greeting) greeting[1] print("Slide 40: Behaviors Common to Lists and Strings (continued)") greeting = 'Hello' greeting[0] = 'J' #Str is immuntable print("Slide 41: Behaviors Common to Lists and Strings (continued)") #called slicing alphabet = 'abcdefghijklmnopqrstuvwxyz' alphabet[4:10] #default for 1st number is beginning of string alphabet[:6] print("Slide 42: Behaviors Common to Lists and Strings (continued)") alphabet = 'abcdefghijklmnopqrstuvwxyz' #no 2nd number then stop is end of string. alphabet[23:] #third number is the step size which is 1 if the third value ius not present alphabet[9:20:3] print("Slide 43: Behaviors Common to Lists and Strings (continued)") musing = 'The Shallow may fly south with' 'w' in musing 'ow ma' in musing 'South' in musing print("Slide 44: Behaviors Common to Lists and Strings (continued)") musing = 'The Shallow may fly south with the sun' musing.count('th') musing.index('th') musing.index('ow ma') print("Slide 45: Behaviors Common to Lists and Strings (continued)") #lexigraphic order (dictionary order). #check for lexigraphic order and return True or False. #string comparison print("== equality test. True if strings exactly the same.") print("!= inequality test. Do strings differ in some way.") print("< left operand is less than or equal to right operand.") print(">left operand is greater than right operand.") print(">= left operand is greater than or equal to right operand.") print("Slide 46: String Methods not a Part of Lists") formal = 'Hello. How are you?' informal = formal.lower() screaming = formal.upper() formal informal screaming print("Slide 47: lower() Method - Before and After") person = 'Alice' persion = person.lower() print("Slide 48: str Methods That Convert Between Strings and a List of Strings")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question