Python Important Methods and Functions
1. String Methods
Method Purpose Example Output
split(sep) Splits a string into "a,b,c".split(",") ['a','b','c']
a list
join(iterable) Joins elements into "-".join(['a','b']) "a-b"
one string
replace(old, new) Replaces text "cat".replace("c","b") "bat"
strip() Removes spaces " hi ".strip() "hi"
(or specified chars)
from both ends
find(sub) Returns first index "hello".find("l") 2
of substring or -1
startswith(prefix) Checks if string "hello".startswith("he") True
starts with prefix
endswith(suffix) Checks if string "hello".endswith("lo") True
ends with suffix
lower() Converts to "Hello".lower() "hello"
lowercase
upper() Converts to "Hello".upper() "HELLO"
uppercase
2. List Methods
Method Purpose Example Result
append(x) Adds one item to end [1,2].append(3) [1,2,3]
extend(iterable) Adds multiple items [1].extend([2,3]) [1,2,3]
insert(i,x) Inserts at index [Link](1,10) Item inserted
pop([i]) Removes and returns item [Link]() Last item
removed
remove(x) Removes first matching [Link](3) Removes first 3
value
sort() Sorts list in place [Link]() Ascending order
reverse() Reverses list in place [Link]() Reversed list
copy() Returns shallow copy new = [Link]() New list
3. Dictionary Methods
Method Purpose Example Result
get(key, Safely gets [Link]("age") Value or None
default)
value
keys() Returns all [Link]() dict_keys([...])
keys
values() Returns all [Link]() dict_values([...])
values
items() Returns key- [Link]() dict_items([...])
value pairs
update(dict) Adds/updates [Link]({"a":10}) Dictionary updated
entries
pop(key) Removes key [Link]("age") Removed value
and returns
value
setdefault(key, Gets value or [Link]("grade","A") Existing/new value
default) inserts default
4. Set Methods
Method Purpose Example Result
add(x) Adds one element [Link](5) {...,5}
update(iterable) Adds many elements [Link]([4,5]) Updated set
remove(x) Removes element (error if [Link](3) Removed
absent)
discard(x) Removes element safely [Link](3) No error if
missing
union(other) Combines sets [Link](b) All unique
elements
intersection(other) Common elements [Link](b) Shared values
5. Built-in Functions
Function Purpose Example Output
len(obj) Number of items len([1,2,3]) 3
range() Generates sequence range(5) 0,1,2,3,4
of numbers
enumerate(iterable) Adds index while enumerate(['a','b']) (0,'a'),
iterating (1,'b')
zip(a,b) Combines iterables zip([1,2],['a','b']) (1,'a'),
(2,'b')
sum(iterable) Sum of numbers sum([1,2,3]) 6
max(iterable) Largest item max([3,8,2]) 8
min(iterable) Smallest item min([3,8,2]) 2
sorted(iterable) Returns sorted copy sorted([3,1,2]) [1,2,3]
abs(x) Absolute value abs(-10) 10
round(x,n) Rounds number round(3.14159,2) 3.14
type(obj) Returns object's type type(5) <class
'int'>
isinstance(obj,type) Checks object's type isinstance(5,int) True
all(iterable) True if all elements all([1,2,3]) True
are truthy
any(iterable) True if any element any([0,0,1]) True
is truthy
6. Core Python Concepts
Feature Purpose Example Result
List Create lists in one [x*x for x in range(5)] [0,1,4,9,16]
Comprehension line
Dictionary Create dictionaries in {x:x*x for x in {0:0,1:1,2:4}
range(3)}
Comprehension one line
Lambda Anonymous function lambda x:x*x Function object
map(func, Apply function to list(map(str,[1,2])) ['1','2']
iterable)
every element
filter(func, Keep elements where list(filter(lambda [3]
iterable) x:x>2,[1,2,3]))
function returns True
with open() Opens a file safely with open("[Link]") as File handled
and closes it f: safely
automatically
f-strings Embed variables in name="Ali"; f"Hi "Hi Ali"
{name}"
strings