Python core basics Flashcards
How to check if element exists in list or not?
print(1 in L)
Adding new list to an existing list without creating a new temporary list
l.extend([1,2])
What is the difference between append and extend
In case of append : We will be adding that component as recieved in the input parameters
Whereas in case of extend , it will take the individual components from that list and then add it to the list
Example:
l.append([2,5])
This adds [2,5 ] aka [1,[2,5]]
l.extend([2,5])
The adds 2 and 5 to the list [1,2,5]
How do we delete a certain component in list
del L[0]
How to remove element by passing object itself instead of index of that object in the list
L.remove(“Physics”)
Syntax for removing the last element from a list
L.pop()
What happens if you do B= A.sort() where A and B are lists
A returns the new sorted list
A=[3,4,2] -> A=[2,3,4]
while B returns nothing .
Because its a worthless piece of shit. JK A.sort() is a void function and only changes A, it doesent return an object in return. Only changes the list which is calling the function .
Syntax to sort a list in descending order
L.sort(reverse=True)
What happens if you sort out list which contains words instead of numbers
It arranges the words inside the list in lexographic fashion
Syntax to sort a particular list and store within itself
L.sort()
Syntax to sort a particular list and return list to an object
sorted(L)
What is the difference between lists and tuples?
Lists are immutable , meaning you can access as well as change individual elements
tuples are immutable, meaning while you can access the individual elements , you cant change them.
How do we copy A to B WITHOUT both of them being linked by refence.
or rather
Copying only the elements and maintain them independent of each other… making sure that One is not affected by changing the other
A= [1,2,3]
B= A[:]
B[0]=3
A=[1,2,3] , B=[3,2,3]
If B=[1,2,3]
A = B
B[0] = 3
Then what will be A,B?
A , B will be the same because when we use A=B they are connected by references rather than different objects.
A= [3,2,3]
B= [3,2,3]
What is the syntax if I want to maintain a sorted list as a different object (aka different from the original list object)
C= sorted(A)
A -> Non sorted list Ex : [2,3,1]
C-> Sorted List Ex : [1,2,3]
What is difference between sets, tuples and lists
Sets are mutable BUT we can not index. (they also DO NOT store duplicates)
Lists are mutable and can be indexed
Tuples are immutable but can still be indexed.
Finding min of all elements in list or tuple
min(lis) or min(t)
Concatenating tuples
tup3 = tup2 + tup1
finding sum of all elements in a tuple or list
sum(lis) or sum(t) //List or tuple
Finding max of all elements in list or tuple
max(lis) or max(t)
What happens if I do this
t=(1,2,3)
y= t+4,
It will raise and error because by order precedence rule first it will try to add 4 .But it cant because its an individual element.
In order for the above statement to work , you need to do
y= t+ (4,)
Then by order precedence (4,) will instantiate tuple and then finally concatenate it
t= (2,3,4)
What happens if I print(t[-1:-2])
It wont raise an error , it will return back an empty tuple
()
The same goes for list
What happens if a tuple or list has both numbers and non numbers and you try to apply sum or min or max?
It returns me error
Can you add element to a tuple via append or extend()
No , you CAN add a tuple to a tuple using concatenation but you CANT add individual elements to a tuple using functions or operator.
What is packing and unpacking in tuples?
Instantiating vairables
a = (1,2,3) -> Comes under packing
(f,g,h) = (1,2,3) or (f,g,h)=a
The above line comes under unpacking because youre unpacking the elements and assigning values
Since tuples are immutable , can they still be sorted ?
Yes , they can be sorted via sorted(t) The returning object is list . And if we want to turn the list into tuple we can do y = sorted(t) ///y is list tuple(y) ///Which will return tuple
Syntax for converting list in to tuple
lis = [1,2,3] t= tuple(lis)
How to change list to sets
set(Grades)
How do we initiate sets?
s = {1,2,3,4,5,4}
which will store s= s = {1,2,3,4,5}
How do we add new elements in sets?
s.add(1)
How do we remove objects in sets via object itself?
a.remove(‘Hello’) //We are removing by passing object not index
How do we perform union of 2 sets?
A = {1,2,3,4}
B= {5,6,7,8}
print(A|B) (OR) print(A.union(B))
How do we perform intersection of 2 sets?
A = {1,2,3,4}
B= {5,6,7,8}
print(A & B) (Or) A.intersection(B)
Remove elements of B from A
A = {1,2,3,4}
B= {3,4,7,8}
A-B (or) A.difference(B)
Output : {1,2}
Print all the elements except the intersection of both elements
A = {1,2,3,4}
B= {3,4,7,8}
print(A^B)
Output : {1,2,7,8}
What is one of the major differences between sets and lists and tuples in terms of operation complexity?
Sets are the fastest means to serach through and fetch data compared to other 2