Python core basics Flashcards

1
Q

How to check if element exists in list or not?

A

print(1 in L)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Adding new list to an existing list without creating a new temporary list

A

l.extend([1,2])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between append and extend

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we delete a certain component in list

A

del L[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to remove element by passing object itself instead of index of that object in the list

A

L.remove(“Physics”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Syntax for removing the last element from a list

A

L.pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you do B= A.sort() where A and B are lists

A

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 .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Syntax to sort a list in descending order

A

L.sort(reverse=True)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you sort out list which contains words instead of numbers

A

It arranges the words inside the list in lexographic fashion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Syntax to sort a particular list and store within itself

A

L.sort()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Syntax to sort a particular list and return list to an object

A

sorted(L)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between lists and tuples?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A= [1,2,3]
B= A[:]
B[0]=3
A=[1,2,3] , B=[3,2,3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If B=[1,2,3]
A = B
B[0] = 3
Then what will be A,B?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax if I want to maintain a sorted list as a different object (aka different from the original list object)

A

C= sorted(A)
A -> Non sorted list Ex : [2,3,1]
C-> Sorted List Ex : [1,2,3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is difference between sets, tuples and lists

A

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.

17
Q

Finding min of all elements in list or tuple

A

min(lis) or min(t)

18
Q

Concatenating tuples

A

tup3 = tup2 + tup1

19
Q

finding sum of all elements in a tuple or list

A

sum(lis) or sum(t) //List or tuple

20
Q

Finding max of all elements in list or tuple

A

max(lis) or max(t)

21
Q

What happens if I do this
t=(1,2,3)
y= t+4,

A

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

22
Q

t= (2,3,4)

What happens if I print(t[-1:-2])

A

It wont raise an error , it will return back an empty tuple
()
The same goes for list

23
Q

What happens if a tuple or list has both numbers and non numbers and you try to apply sum or min or max?

A

It returns me error

24
Q

Can you add element to a tuple via append or extend()

A

No , you CAN add a tuple to a tuple using concatenation but you CANT add individual elements to a tuple using functions or operator.

25
Q

What is packing and unpacking in tuples?

A

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

26
Q

Since tuples are immutable , can they still be sorted ?

A
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
27
Q

Syntax for converting list in to tuple

A
lis = [1,2,3]
t= tuple(lis)
28
Q

How to change list to sets

A

set(Grades)

29
Q

How do we initiate sets?

A

s = {1,2,3,4,5,4}

which will store s= s = {1,2,3,4,5}

30
Q

How do we add new elements in sets?

A

s.add(1)

31
Q

How do we remove objects in sets via object itself?

A

a.remove(‘Hello’) //We are removing by passing object not index

32
Q

How do we perform union of 2 sets?

A

A = {1,2,3,4}
B= {5,6,7,8}
print(A|B) (OR) print(A.union(B))

33
Q

How do we perform intersection of 2 sets?

A

A = {1,2,3,4}
B= {5,6,7,8}
print(A & B) (Or) A.intersection(B)

34
Q

Remove elements of B from A

A

A = {1,2,3,4}
B= {3,4,7,8}
A-B (or) A.difference(B)
Output : {1,2}

35
Q

Print all the elements except the intersection of both elements

A

A = {1,2,3,4}
B= {3,4,7,8}
print(A^B)
Output : {1,2,7,8}

36
Q

What is one of the major differences between sets and lists and tuples in terms of operation complexity?

A

Sets are the fastest means to serach through and fetch data compared to other 2