Python Day 2 R&D
set .union vs pipe operator(" | ") why are there multiple ways of achieving this
Both combine elements from two sets and return a new set with unique elements.
A = {1, 2, 3}
B = {3, 4, 5}
[Link](B)
A|B
Why multiple ways exist?
● Readability vs Conciseness
● Backward compatibility
● Different abstraction levels
● Mathematical representation (| mirrors set theory)
[Link]
[Link]
Difference between [Link] & [Link]
remove () removes element and raises error if missing
discord() removes element but doesn’t raise an error if missing
s = {1, 2, 3}
[Link](2) #No error
[Link](5) #Gives error
[Link](5) #No error
[Link]
[Link]
create a table and list down all the methods that a data type supports and what
does it do exactly(Give brief description)
Category Method Description
String (str) upper() Convert to uppercase
lower() Convert to lowercase
strip() Remove whitespace from start/end
split() Split string into a list
replace() Replace a substring with another
find() Find the first index of a substring
List (list) append() Add an element to the end
extend() Add multiple elements (from an iterable)
insert() Insert an element at a specific index
remove() Remove the first occurrence of a value
pop() Remove and return an element by index
sort() Sort the list in place
Tuple (tuple) count() Count the occurrences of a value
index() Find the first index of a value
Set (set) add() Add an element to the set
remove() Remove an element (raises error if
missing)
discard() Remove an element safely (no error if
missing)
union() Return a set containing all elements from
both
intersection() Return elements common to both sets
difference() Return elements in the first set but not
the second
Dictionary keys() Return a list of all keys
(dict)
values() Return a list of all values
items() Return key-value pairs as tuples
get() Access a value safely (returns None if
missing)
pop() Remove a specific key and return its
value
update() Update the dictionary with another
dictionary
[Link]
[Link]
[Link]
[Link]
[Link]
Explain Pass by reference and Pass by value with example & code snippet.
Pass by Value (Immutable Objects)
def update(x):
x = 10
a=5
update(a)
print(a) #outputs 5
Original value is unchanged
Pass by Reference (Mutable Objects)
def update(lst):
[Link](4)
numbers = [1, 2, 3]
update(numbers)
print(numbers) #Outputs [1,2,3,4]
Object will be modified
[Link]
[Link]
[Link]