Uploaded by dhowritz457

Python Data Structures & Functions Reference

advertisement
FUNCTIONS:
Follow def function(): format. To call it in the main command, do function(parameter)
To obtain the return variable from the function in main, do object = function(parameter). This
lets you access whatever you want returned in the main function.
Can return multiple values → Ex. return first, last can be access in the main function by
First_name, last_name = function(input1, input2)
LISTS:
Error is item is not found in the list.
Lists are created through []. Lists are mutable and indexable → Order matters.
Can store any data type → strings, boolean, integers, floats, etc.
Indexes start at 0 and go to the length of the list → can also index backwards with negatives.
- When starting with negatives, the last index is -1, second to last is -2, etc.
Can index out some elements using list_name[start:end] → Will return a list with the start index
as the first index and the last index being the INDEX BEFORE END. does not include end.
If either index is left out, it defaults to 0 or the index before end for start and end respectively.
Ex. [1,2,3,4,5,6,7] → list_name[2:6] → [3,4,5,6,]
[1,2,3,4,5,6,7] → list_name[1:-1] → [2,3,4,5,6] → does not include the end index.
For i in range(len(list)) → lets you iterate through the indexes.
Strings are immutable → cannot mutate strings by assigning values at specific indexes.
* works the same as in strings → list1 = [0], list2 = list1*5 → list2 = [0,0,0,0,0]
+ works the same as in strings → list1 = [1,2], list2 = [3,4], list1+list2 = [1,2,3,4]
Item in list → iterates through items in list, True if item is found and False if not.
2D LISTS:
Can have lists of elements as an index within another list. → [1,2,[3,30],3,4]
To index into inner lists, list_name[2][1].
For i in range(len(list)):
For j in range(len(list[i])): → lets you iterate through the indexes of inner lists.
TUPLES:
Can access elements through indexing and looping but is IMMUTABLE. Order matters.
Access elements like tuple_name[index]. Slice in the same manner as a list.
For a return first, last instance, if you do name = function(input1, input2), you will get the
returned elements in tuple form.
To convert between list and tuple, just do list(tuple_name) or tuple(list_name).
SETS:
Are unordered and thus cannot iterate through indexes → can still store any data type.
Creating empty set → use set() command instead of (), since () creates an empty tuple.
To iterate over sets → for elem in set_name. Again, CANNOT index.
Can use in operator like lists → returns True if item is in list, False if not.
Cannot use * and + commands like in lists and tuples.
DICTIONARIES:
Stores elements as key:value pairs, are indexable (by key), mutable, and iterable. Order does not
matter as we check for values/keys by indexing the KEYS; cannot index by the values.
Can store any data type: boolean, ints, strings, floats, etc.
Indexing: dict_name[key] → returns the value associated with the key.
Dict_name[key] = value → if key already exists, value replaces existing value
- If key doesn’t exist, the new key:value pair is added to the dictionary.
Can delete key:value pairs using del dict_name[key]
For var in dictionary → lets you iterate over the keys in a dictionary
For key, value in dict_name.items() → lets you iterate over the values in dictionary.
FILES:
Access files by file_variable = open(filename, mode) where mode can be read (‘r’), write (‘w’),
or append to end of file (‘a’). Must close at end by file_variable.close().
(‘w’) will erase contents of file if they exist, if not it creates a new file.
(‘a’) preserves the existing file and adds data to the end of the file.
For var in file_variable: → lets you read through the file line-by-line.
STRING COMMANDS:
str.strip() → does not include sep in the returned string, if two delimters are together a string of
“ “ (one space) is returned, if 3 are together it returns “ “ (two spaces), etc.
EXCEPTIONS:
In the format of try and except, formatted like if and else statements. The computer executes the
try commands, and if an exception arises or an error occurs, it will go to the except statements
and SKIP the try statements. The same works for vice versa.
- Can import coding files by saving it to the same folder and doing import filename.
- Call main like if __name__ == ‘__main__”: (enter) main()
Email = input_file.read()
Email = email.split(‘ ‘)
For i in range(len(email)):
New_word = ‘’-->
word = email[i]
if not_word[j].isdigit():
if word.isdigit():
new_word += word[i]
new_word = word
email[i] = new_word
else: for j in range(len(word))--> email = ‘ ‘.join(emial).
Download