-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeory_questions
196 lines (156 loc) · 6.42 KB
/
theory_questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Difference between list and tuple
1) list is mutable , it can't be used as a key in a dictionary
tuple is immutable , it can be used as a key in the dictionary
e.g:- Your many cats' names, you can add new cats there
2) lists are homogeneous
tuples are heterogeneous
e.x:- the names of the months of the year, no need to modify the elements
Why we need comma at the end of the tuples element:
Creating a tuple with one element is a bit tricky.
Having one element within parentheses is not enough. We will need a trailing comma to indicate that it is in fact a tuple
my_tuple = ("hello",)
print(type(my_tuple))
Dictioary:
e.g:- Telephone book
Python function arguments:
Required arguments[ def printme( str): ]
Keyword arguments [def printme( str ):]
Default arguments [Should be written at the end of the arguments] [def printinfo( name, age = 35 ):]
Variable-length arguments [def printinfo( arg1, *vartuple ):]
Data types in python:
Numbers
list
tuples
strings
dictionary
#Python details
=========================
Lambda Functions:
anonymous functions, i.e. functions without a name
The general syntax of a lambda function is quite simple:
lambda argument_list: expression
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression
using these arguments. You can assign the function to a variable to give it a name.
The following example of a lambda function returns the sum of its two arguments:
>>> f = lambda x, y : x + y
>>> f(1,1)
2
Example2:
>>> f = lambda x, y : (x * y) + (x*y)
>>> f(2,3)
12
>>>
=======================
FILETR:
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
filter() calls our lambda function for each element of the list, and returns a new list that
contains only those elements for which the function returned "True".
=========================
MAP:
map() is used to convert our list. The given function is called for every element in the original list,
and a new list is created which contains the return values from our lambda function
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
============================
REDUCE:
>>>
>>> print reduce(lambda x, y: x + y, foo)
139
reduce() is somewhat special. The "worker function" for this one must accept two arguments
(we've called them x and y here), not just one. The function is called with the first two
elements from the list, then with the result of that call and the third element, and so on,
until all of the list elements have been handled. This means that our function is called n-1
times if the list contains n elements. The return value of the last call is the result of the
reduce() construct. In the above example, it simply adds the arguments, so we get the sum of all elements
=================================
*Who creates *.pyc file and why?*
A *.pyc file is created for imported modules, and they are placed in the same
directory containing the .py file. However... no .pyc file is created for the main
script for your program. In other words... if you call "python myscript.py" on the command line,
there will be no .pyc file for myscript.py.
=====================================
The globals() and locals() Functions
==========================================
If locals() is called from within a function, it will return all the names that can be accessed locally from that function.
If globals() is called from within a function, it will return all the names that can be accessed globally from that function.
The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.
============================================
The reload() Function:
When the module is imported into a script, the code in the top-level portion of a module is executed only once.
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function.
The reload() function imports a previously imported module again
reload(module_name)
=========================================
polymorphism:
one item having multiple forms:
two types of polymorphism:
1) Static polymorphism
2) Dynamic polymorphism
static polymorphism: (function overloading):
implement multiple methods within the same class that use the same name but a different set of parameters
he parameter sets have to differ in at least one of the following three criteria:
They need to have a different number of parameters, e.g. one method accepts 2 and another one 3 parameters.
The types of the parameters need to be different, e.g. one method accepts a String and another one a Long.
They need to expect the parameters in a different order, e.g. one method accepts a String and a
Long and another one accepts a Long and a String.
This kind of overloading is not recommended because it makes the API difficult to understand
public class BasicCoffeeMachine {
// ...
public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {
}
public List brewCoffee(CoffeeSelection selection, int number) throws CoffeeException {
}
// ...
}
1) BasicCoffeeMachine coffeeMachine = createCoffeeMachine();
coffeeMachine.brewCoffee(CoffeeSelection.FILTER_COFFEE);
2) BasicCoffeeMachine coffeeMachine = createCoffeeMachine();
List coffees = coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO, 2);
Dynamic polymorphism (Method overriding):
is when a method defined in a superclass or interface is re-defined by one of its subclasses,
thus modifying/replacing the behavior the superclass provides
class Parent
{
void show() { System.out.println("Parent's show()"); }
}
// Inherited class
class Child extends Parent
{
// This method overrides show() of Parent
@Override
void show() { System.out.println("Child's show()"); }
}
// Driver class
class Main
{
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
Run on IDE
Output:
Parent's show()
Child's show()
===============================
#!/usr/bin/python
import re
phone = "2004-959-559 # This is Phone Number for Somnath Dange"
# Delete Python-style comments
num = re.sub(r'#.*$', "",phone)
print num
#Delete all characters other than digita
dig = re.sub(r'\D',"",phone)
print dig
# Delete all digits
alp = re.sub(r'\d',"",phone)
print alp