Essential Python Methods
Essential Python Methods
numbers = [1, 2, 3]
append() Adds an element at the end of the list. [1, 2, 3, 4]
[Link](4)
numbers = [1, 2, 3]
clear() Removes all the elements from the list. []
[Link]()
numbers = [1, 2, 3]
copy() Returns a shallow copy of the list. [1, 2, 3]
new_list = [Link]()
Returns the index of the first occurrence numbers = [10, 20, 30, 20]
index() 1
of a specified value. print([Link](20))
numbers = [1, 2, 3]
reverse() Reverses the order of the list in place. [3, 2, 1]
[Link]()
[Link]
SET METHODS
Method Description Code Example Output
set1 = {1, 2, 3}
add() Inserts an element into the set. {1, 2, 3, 4}
[Link](4)
set1 = {1, 2, 3}
Returns elements present in one
difference() set2 = {3, 4, 5} {1, 2}
set but not in another.
[Link](set2)
set1 = {1, 2, 3}
difference_up Removes elements found in
set2 = {2, 3, 4} {1}
date() another set from the current set.
set1.difference_update(set2)
set1 = {1, 2, 3}
Returns elements common to
intersection() set2 = {2, 3, 4} {2, 3}
multiple sets.
[Link](set2)
set1 = {1, 2, 3}
intersection_ Updates the set with elements
set2 = {2, 3, 4} {2, 3}
update() common to all sets.
set1.intersection_update(set2)
set1 = {1, 2, 3}
Returns True if two sets have no
isdisjoint() set2 = {4, 5, 6} True
elements in common.
[Link](set2)
set1 = {1, 2}
Checks if all elements of one set
issubset() set2 = {1, 2, 3, 4} True
are in another.
[Link](set2)
set1 = {1, 2, 3, 4}
Checks if the current set
issuperset() set2 = {1, 2} True
contains all elements of another.
[Link](set2)
[Link]
SET METHODS
Method Description Code Example Output
set1 = {1, 2, 3}
symmetric_ Returns elements unique to
set2 = {3, 4, 5} {1, 2, 4, 5}
difference() each set (not in both).
set1.symmetric_difference(set2)
set1 = {1, 2, 3}
Returns all elements from
union() set2 = {3, 4, 5} {1, 2, 3, 4, 5}
both sets without duplicates.
[Link](set2)
[Link]
DICTIONARY METHODS
Method Description Code Example Output
Returns a view object containing dict1 = {'x': 10, 'y': 20, 'z': 30} dict_values([10, 20,
values()
all dictionary values. print([Link]()) 30])
[Link]
DICTIONARY METHODS
Method Description Code Example Output
Returns a view object containing dict1 = {'x': 10, 'y': 20, 'z': 30} dict_values([10, 20,
values()
all dictionary values. print([Link]()) 30])
[Link]
STRING METHODS
Method Description Code Example Output
str1 = "A\tB\tC"
expandtabs() Sets the tab size within the string. A B C
print([Link](4))
[Link]
STRING METHODS
Method Description Code Example Output
str1 = "INTENSITY"
lower() Converts all letters to lowercase. intensity
print([Link]())
[Link]
STRING METHODS
Method Description Code Example Output
str1 = "AI\nML\nDL"
splitlines() Splits string at line breaks. ['AI', 'ML', 'DL']
print([Link]())
[Link]
STRING METHODS
Method Description Code Example Output
TUPLE METHODS
Method Description Code Example Output
Searches for a specified value and number_tup = (10, 20, 30, 20)
index() 2
returns its position (index). print(number_tup.index(30))
[Link]
PYTHON BUILT-IN FUNCTIONS
Function Description Code Example Output
b = bytearray([65, 66,
bytearray() Creates a mutable array of bytes. bytearray(b'ABC')
67])print(b)
class Demo:
@classmethod
Converts a method into a class
classmethod() def show(cls): Class Method
method.
print("Class Method")
[Link]()
[Link]
PYTHON BUILT-IN FUNCTIONS
Function Description Code Example Output
class A:
Deletes an attribute from an x=10
delattr() False
object. delattr(A,'x')
print(hasattr(A,'x'))
['__add__',
Lists attributes and
dir() print(dir([ ])[:3]) '__class__',
methods of an object.
'__contains__']
nums=[1,2,3,4]
Filters elements using a
filter() even=list(filter(lambda x:x%2==0,nums)) [2,4]
condition.
print(even)
s=frozenset([1,2,3])
frozenset() Creates an immutable set. frozenset({1,2,3})
print(s)
class A:
Retrieves the value of an
getattr() x=42 42
object’s attribute.
print(getattr(A,'x'))
['__name__',
Returns the global symbol
globals() print(list(globals().keys())[:3]) '__doc__',
table as a dictionary.
'__package__']
[Link]
PYTHON BUILT-IN FUNCTIONS
Function Description Code Example Output
class A:
Checks if an object has a specific
hasattr() x=10 True
attribute.
print(hasattr(A,'x'))
x=10; (unique
id() Returns the unique ID of an object.
print(id(x)) integer)
input() Reads a line of input from the user. input("Enter name: ") (User input)
class A:
pass
Checks if a class is a subclass of
issubclass() class B(A): True
another class.
pass
print(issubclass(B,A))
it=iter([1,2])
iter() Converts an iterable into an iterator. 1
print(next(it))
def test():
Returns the current local symbol x=5
locals() {'x':5}
table. print(locals())
test()
[Link]
PYTHON BUILT-IN FUNCTIONS
Function Description Code Example Output
it=iter([10,20])
next() Returns the next item from an iterator. 10
print(next(it))
o=object(); <class
object() Returns a new featureless object.
print(type(o)) 'object'>
f=open('[Link]','w')
open() Opens a file and returns a file object. [Link]('Hi') (Creates file)
[Link]()
Intensity
print() Outputs text to the console. print("Intensity Coding")
Coding
class A:
def __init__(self):
self._x=5
property() Returns a property object for a class. 5
x=property(lambda
self:self._x)
print(A().x)
[Link]
PYTHON BUILT-IN FUNCTIONS
Function Description Code Example Output
class A:
pass
setattr() Sets an attribute on an object. a=A(); 100
setattr(a,'x',100)
print(a.x)
s='Python';
slice() Creates a slice object for slicing sequences. 'Pyt'
print(s[slice(0,3)])
class A:
@staticmethod
Defines a static method (no access to
staticmethod() def show(): Static
class/instance).
print('Static')
[Link]()
class A:
def show(self):
print('A')
super() Returns proxy object for parent class. class B(A): A
def show(self):
super().show()
B().show()
class A:
{'__module__'
vars() Returns __dict__ of an object. x=1
:..., 'x':1, ...}
print(vars(A))
[Link]
Found this helpful ?
Follow on LinkedIn
Master AI/ML with Intensity Coding
@bhavdippatel2020
[Link]