Created
May 17, 2019 11:15
-
-
Save tanmaiaccion/fc4cb57f10da8ffab416dd1ba8dba94c to your computer and use it in GitHub Desktop.
python sql questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What will be the output of the code below? Explain your answer. | |
def extendList(val, list=[]): | |
list.append(val) | |
return list | |
list1 = extendList(10) | |
list2 = extendList(123,[]) | |
list3 = extendList('a') | |
print "list1 = %s" % list1 | |
print "list2 = %s" % list2 | |
print "list3 = %s" % list3 | |
How would you modify the definition of extendList to produce the presumably desired behavior? | |
Hide answer | |
The output of the above code will be: | |
list1 = [10, 'a'] | |
list2 = [123] | |
list3 = [10, 'a'] | |
Many will mistakenly expect list1 to be equal to [10] and list3 to be equal to ['a'], thinking that the list argument will be set to its default value of [] each time extendList is called. | |
However, what actually happens is that the new default list is created only once when the function is defined, and that same list is then used subsequently whenever extendList is invoked without a list argument being specified. This is because expressions in default arguments are calculated when the function is defined, not when it’s called. | |
list1 and list3 are therefore operating on the same default list, whereas list2 is operating on a separate list that it created (by passing its own empty list as the value for the list parameter). | |
The definition of the extendList function could be modified as follows, though, to always begin a new list when no list argument is specified, which is more likely to have been the desired behavior: | |
def extendList(val, list=None): | |
if list is None: | |
list = [] | |
list.append(val) | |
return list | |
Defaultdict (defaultdict objects support the following method in addition to the standard dict operations) | |
__missing__ | |
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] | |
>>> d = defaultdict(list) | |
>>> for k, v in s: | |
... d[k].append(v) | |
... | |
>>> d.items() | |
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] | |
What is Denormalization. | |
DeNormalization is a technique used to access the data from higher to lower normal forms of database. It is also process of introducing redundancy into a table by incorporating data from the related tables | |
How to find a duplicate record? | |
duplicate records with one field | |
duplicate records with more than one field | |
duplicate records with one field | |
SELECT name, COUNT(email) | |
FROM users | |
GROUP BY email | |
HAVING COUNT(email) > 1 | |
duplicate records with more than one field | |
SELECT name, email, COUNT(*) | |
FROM users | |
GROUP BY name, email | |
HAVING COUNT(*) > 1 | |
4 * 4 matrix - rows are in incrementing value, columns values are sorted - find max number. | |
Staticmethods vs classmethods ? | |
Python pass by assignment ? | |
Mutable and immutable types in python? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment