Python Summary Cheat Sheet - 3
Built - In - Functions
Name | Usage | |
---|---|---|
print() | Function prints the message to the screen or any other standard output device. | |
int() | Converts valid data of any type to integer. | |
str() | Converts data of any type to a string. | |
id() | To find the id of a object. | |
round(number, digits(optional)) | Rounds the float value to the given number of decimal digits. | |
bool() | Converts to boolean data type. | |
ord(character) | Gives unicode value of the character. | |
chr(unicode) | Gives character with the unicode value. | |
list(sequence) | Takes a sequence and converts it into list. | |
tuple(sequence) | Takes a sequence and converts it into tuple. | |
set(sequence) | Takes any sequence as argument and converts to set, avoiding duplicates. | |
dict(sequence) | Takes any number of key-value pairs and converts to dictionary. | |
float() | Converts to float data type. | |
type() | Check the datatype of the variable or value using. | |
min() | Returns the smallest item in a sequence or the smallest of two or more arguments. | |
max() | Returns the largest item in a sequence or the largest of two or more arguments. | |
sum(sequence) | Returns the sum of items in a sequence. | |
sorted(sequence) | Returns a new sequence with all the items in the given sequence ordered in increasing order. | |
sorted(sequence, reverse=True) | Returns a new sequence with all the items in the given sequence ordered in decreasing order. | |
len(sequence) | Returns the length of the sequence. | |
map() | Applies a given function to each item of a sequence (list, tuple etc.) and returns a sequence of the results. | |
filter() | Method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. | |
reduce() | Receives two arguments, a function and an iterable. However, it doesn't return another iterable, instead, it returns a single value. |
Floating Point Approximation: Float values are stored approximately.
Floating Point Errors: Sometimes, floating point approximation gives unexpected results.
Different compound assignment operators are: +=
, -=
, *=
, /=
, %=
Single And Double Quotes: A string is a sequence of characters enclosed within quotes.
Escape Characters: Escape Characters are a sequence of characters in a string that is interpreted differently by the computer. We use escape characters to insert characters that are illegal in a string.
We got a new line by adding \n
escape character.
Name | Usage |
---|---|
\n | New Line |
\t | Tab Space |
\\ | Backslash |
\' | Single Quote |
\" | Double Quote |
Set Methods, Operations and Comparisons
Set Methods:
Name | Syntax | Usage |
---|---|---|
add() | set.add(value) | Adds the item to the set, if the item is not present already. |
update() | set.update(sequence) | Adds multiple items to the set, and duplicates are avoided. |
discard() | set.discard(value) | Takes a single value and removes if present. |
remove() | set_a.remove(value) | Takes a value and removes it if it is present or raises an error. |
clear() | set.clear() | Removes all the items in the set. |
Set Operations:
Union: Union of two sets is a set containing all elements of both sets.
Syntax: set_a | set_b
(or) set_a.union(sequence)
Intersection: The intersection of two sets is a set containing common elements of both sets.
Syntax: set_a & set_b
(or) set_a.intersection(sequence)
Difference: The difference of two sets is a set containing all the elements in the first set but not the second.
Syntax: set_a - set_b
(or) set_a.difference(sequence)
Symmetric Difference: Symmetric difference of two sets is a set containing all elements which are not common to both sets.
Syntax: set_a ^ set_b
(or) set_a.symmetric_difference(sequence)
Set Comparisons: Set comparisons are used to validate whether one set fully exists within another.
issubset(): set2.issubset(set1)
Returns True if all elements of the second set are in the first set. Else, False.
issuperset(): set1.issuperset(set2)
Returns True if all elements of second set are in first set. Else, False.
isdisjoint(): set1.isdisjoint(set2)
Returns True when they have no common elements. Else, False.
Tuples
Tuple: Holds an ordered sequence of items. Tuple is an immutable object, whereas a list is a mutable object.
Accessing Tuple Elements: Accessing Tuple elements is also similar to string and list accessing and slicing.
Tuple Slicing: The slice operator allows you to specify where to begin slicing, where to stop slicing, and what step to take. Tuple slicing creates a new tuple from an old one.
Membership Check: Check if the given data element is part of a sequence or not.Membership Operators in
and not in
.
Tuple Packing: ()
brackets are optional while creating tuples. In Tuple Packing, Values separated by commas will be packed into a tuple.
Unpacking: Values of any sequence can be directly assigned to variables. Number of variables in the left should match the length of the sequence.
Dictionaries
Dictionaries: Unordered collection of items. Every dictionary item is a Key-value pair.
Creating a Dictionary: Created by enclosing items within {curly} brackets. Each item in the dictionary has a key-value pair separated by a comma.
Immutable Keys: Keys must be of an immutable type and must be unique. Values can be of any data type and can repeat.
Accessing Items:To access the items in dictionary, we use square bracket [ ]
along with the key to obtain its value.
Accessing Items - Get: The get()
method returns None if the key is not found.
Membership Check: Checks if the given key exists.
Adding a Key-Value Pair:
Modifying an Existing Item: As dictionaries are mutable, we can modify the values of the keys.
Deleting an Existing Item: We can also use the del keyword to remove individual items or the entire dictionary itself.
Sets
Sets: Unordered collection of items. Every set element is Unique (no duplicates) and Must be immutable.
No Duplicate Items: Sets contain unique elements
Immutable Items: Set items must be immutable. As List is mutable, Set cannot have list as an item.
Dictionary Views & Methods
Dictionary Views:
View | Syntax | Usage |
---|---|---|
keys | dict.keys() | Returns dictionary Keys. |
Values | dict.values() | Returns dictionary Values. |
items | dict.items() | Returns dictionary items(key-value) pairs. |
Dictionary Methods:
Name | Syntax | Usage |
---|---|---|
copy | dict.copy() | Returns copy of a dictionary. |
update | dict.update(iterable) | Inserts the specified items to the dictionary. |
clear | dict.clear() | Removes all the elements from a dictionary. |