You are on page 1of 3

Q) List, tuple, dictionary.

List:
List is a collection of objects, enclosed in square brackets and separated by commas. A
sequence data type used to store the collection of data. Lists are the simplest containers.
Lists are heterogeneous which makes it the most powerful tool in python. A single list may
contain datatypes like Integers, Strings, as well as Objects. Lists are mutable, and hence,
they can be altered even after their creation. Lists can be created by just placing the
sequence inside the square brackets. Lists are one of the mostly used and versatile built in
types, allowing us to store multiple items in a single variable. We can access list elements
with index.
Var = ["Geeks", "for", "Geeks"]
print(Var)
Characteristics: ordered, mutable, allows duplicates.
Tuple:
Tuple is a collection of objects separated by commas. In some ways, a tuple is similar to a
list in terms of indexing, nested objects, and repetition. Tuples are immutable. Elements will
be stored in round brackets.
var = ("Geeks", "for", "Geeks")
print(var)
Characteristics: ordered, immutable, allows duplicates, cant be appended, cant remove
items, find items.

Q) Difference between function and method.


Method Function
Can’t be called by their names alone. Can be.
Can directly access attributes and properties of an object. Can’t.
Must have at least 1 arg. There can be 0 arg.
Dependent on class. Independent.
Always have self as first parameter. Doesn’t have.

Q) Anonymous function.
A lambda function is a small anonymous function. A lambda function can take any number of
arguments, but can only have one expression, which is evaluated and returned. It’s a
function without a name. As we already know the def keyword is used to define a normal
function in Python. Similarly, the lambda keyword is used to define an anonymous function in
python.
Syntax: lambda arguments: expression
lambda: print ('Hello World')
Here, we have created a lambda function that prints 'Hello World'.
x = lambda a: a + 10
print (x (5)) Output: 15
x = lambda a, b: a * b
print (x (5, 6)) Output: 30

Q) Exception and exception handling.


Error: Errors are problems in a program due to which the program will stop the execution.
Exception: Even if a statement or expression is syntactically correct, it may cause an error
when an attempt is made to execute it. Errors detected during execution are
called exceptions. When a Python code throws an exception, it has two options: handle the
exception immediately or stop and quit.
Here are some of the most common types of exceptions in Python:
SyntaxError: The interpreter encounters a syntax error in the code, such as a misspelled
keyword, a missing colon, or an unbalanced parenthesis.
TypeError: An operation or function is applied to an object of the wrong type, such as adding
a string to an integer.
NameError: A variable or function name is not found in the current scope.
IndexError: An index is out of range for a list, tuple, or other sequence types.
KeyError: Key isn’t found in a dictionary.
ValueError: Value isn’t found in a dictionary.
AttributeError: An attribute isn’t found on an object.
IOError: An I/O operation, such as reading or writing a file, fails.
ZeroDivisionError: An attempt is made to divide a number by zero.
ImportError: An import statement fails to find or load a module.

Exception handling: If you have some suspicious code that may raise an exception, you
can defend your program by placing the suspicious code in a try: block. After the try: block,
include an except: statement, followed by a block of code which handles the problem as
elegantly as possible.
The try: block contains statements which are susceptible for exception. If exception occurs,
the program jumps to the except: block. If no exception in the try: block, the except: block is
skipped.
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print ("Error: can\'t find file or read data")
else:
print ("Written content in the file successfully")
fh.close()
A single try statement can have multiple except statements. This is useful when the try block
contains statements that may throw different types of exceptions. You can also provide a
generic except clause, which handles any exception.

Q) Access specifiers.
Access modifiers are used by object-oriented programming languages like C++, java, python
etc. to restrict the access of the class member variable and methods from outside the class.
Encapsulation is an OOPs principle which protects the internal data of the class using
Access modifiers like Public, Private and Protected.

Python supports three types of access modifiers which are public, private and protected.
These access modifiers provide restrictions on the access of member variables and
methods of the class from any object outside the class.
Public: By-default the member variables and methods are public which means they can be
accessed from anywhere outside or inside the class. No public keyword is required to make
the class or methods and properties public.
Private: Class properties and methods with private access modifier can only be accessed
within the class where they are defined and cannot be accessed outside the class. In Python
private properties and methods are declared by adding a prefix with two underscores (‘__’)
before their declaration.
Protected: Class properties and methods with protected access modifier can be accessed
within the class and from the class that inherits the protected class. In python, protected
members and methods are declared using single underscore (‘_’) as prefix before their
names.

Q) Arguments in functions.
Q) OOPS concepts.
Q) Exception function handling.

You might also like