Redjumpman Python-Essentials
Redjumpman Python-Essentials
dict.values() Returns a dictionary view of all the values d.values() --> dict_view([1, 2, 3])
dict.items()
dict.keys() Returns a dictionary view of all the keys d.keys() --> dict_view(['b', 'a', 'c'])
dict.get(key, Attempts to return the value of the key specified, otherwise it returns the d.get("X", "not valid") --> "not valid"
default=None) default
dict.pop(key, default) Pops a key. Returns a key error if default is not specified and the key dne. d.pop('a') --> 1
dict.popitem() Pops a random key value pair from the dictionary d.popitem() --> ("b", 2)
dict.update(iterable_pair) Updates the dictionary when given a iterable pairing. d.update({"z": 4}) --> {"a": 1, "b": 2, "c": 3}
d.update([('x', 3)]) --> {"a": 1, "b": 2, "c": 3, "x":
3}
All keys must be hashable. If something is immutable (strings, tuples, etc), then it can be a key. Things like sets and lists cannot be keys.
File Operations
f.read(n) reads up to n characters in the file, otherwise the entire file if n is not f.read() --> "Hello World\n"
specified.
f.readlines() Returns a list of strings. Each line in the file is put in a list f.readlines() --> ["Hello World\n", "Goodbye World\n"]
f.writelines(iterable) Writes a list of lines to the file f.writelines(["hello world", "goodbye world"])
f.seek(offset, from) Moves the current position of the file f.seek(5) --> Move five characters ahead
f.seek(-5, 0) --> Start at the first character and move
back 5.
f.tell() Returns the current position in the file, by counted characters f.tell() --> 13
open(file_path, opens a file at the given file path returns the raw, unread file.
'mode')
Always close the file RIGHT after you read it. Not 50 lines later.
When using the with statement, you do not need to close the file.
readline() will return the first line. If you run it again, it returns the next line.
Each line ends in a "\n"
Reading a file always returns a string or a list of strings.
s.isdisjoint(other) Returns True if s and other do not share any elements. s.isdisjoint({7, 9}) --> True
s.issubset(other) Returns True if all elements of s is in other. s.issubset({1, 2, 3, 4, 5}) --> True
s <= {1, 2, 3, 4, 5} --> True
s.issuperset(other) Return True if all elements other is in s. s.issuperset({1, 4}) --> True
s >= {1, 4} --> True
s.intersection(other) Set of elements common to both sets s.intersection({1, 3, 5, 7}) --> {1, 3}
s & {1, 3, 5} --> {1, 3}
s.difference(other) Returns any elements that exists in s, but not in other. s.difference({2, 3, 7}) --> {1, 4}
s - {2, 3, 7} --> {1, 4}
s.symmetric_difference(other) Returns all the elements that are not shared between the sets s.symmetric_difference({2, 9}) --> {1, 3, 4, 9}
s ^ {2, 9} --> {1, 3, 4, 9}
Strings
str.split(sep) Splits a string by the separator and returns a list of strings "1,3,4,5".split(',') --> ["1", "2", "3"]
str.strip() Removes leading and trailing whitespace " hello ".strip() --> "hello"
str.replace(old, Replaces all old with new in the string "Hello Moon".replace("Moon", "World") --> "Hello
new) World"
str.join(iterable) concatenates a list of strings. ", ".join(["1", "2", "3"]) --> "1, 2, 3"
str.startswith(string) Returns True if str starts with string "Hello".startswith("Hel") --> True
str.lower() Returns a string with all characters in lowercase "ALLCAPS".lower() --> "allcaps"
str.upper() Returns a string with all characters in uppercase "loud noises".upper() --> "LOUD NOISES"
str.find(char) Returns the first index in the string where char is found, otherwise returns - "abracadbra".find("d") --> 6
1.
Strings (cont)
str.format(*args, Formats a string according to your specifiers "The {} costs {} dollars.".format("movie", 10) --> "The movie costs 10
**kwargs) dollars."
list.extend(sequence) Extends a list by adding another list to the end my_list.extend([4,5,6]) --> [1, 2, 3, 4, 5, 6]
list.index(element) Returns the index of the element in the list my_list.index(3) --> 2