Python Interviews: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 26:
10.0//7 = 1.0
 
*= List vs. Tuple.=
List is mutable while the Tuple is not.
A tuple is allowed to be hashed. E.g: using it as a key for dictionaries.
Line 33:
Tuple: b = (1,4,8)
 
*= Lambda vs. def. =
Def can hold multiple expressions while lambda is a uni-expression function.
Def generates a function and designates a name to call it later. Lambda forms a function object and returns it.
Line 39:
Lambda supports to get used inside a list and dictionary.
 
= Optional statements in try-except block =
* Optional statements possible inside a try-except block:
“else” clause: It is useful if you want to run a piece of code when the try block doesn’t create an exception.
“finally” clause: It is useful when you want to execute some steps which run, irrespective of whether there occurs an exception or not.
 
*= List Operations: =
 
Insert integer at position 5 at position 0:
Line 72 ⟶ 73:
print(max(b))
 
*= List to Tuple Conv & Hashing: =
>>> a = [1,2,3,45,6,7]
>>> a
Line 83 ⟶ 84:
1409902629973635913
 
*= Eval Function: =
 
You are given a polynomial of a single indeterminate (or variable), .
Line 109 ⟶ 110:
</pre>
 
*= Swap Case =
<pre>
def swap_case(s):
Line 147 ⟶ 148:
</pre>
 
*= Arrays & Reverse them:it =
<pre>
>>> input ="1 2 3 4 -8 -10"
 
>>> arr =arr input.split(' ')
>>> arr
['1', '2', '3', '4', '-8', '-10']