Python

Interview Questions on Python

1. What do you know about python?

Python is a general-purpose, object-oriented, and high-level programming language. It is simple and easy to learn
The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C).

2. Can we use python as interpreter?

Yes. Simply enter ‘python’ in shell and execute python code line by line.

3. How to declare local and global variables?

If you declare variables inside function then they are local.
If you declare out side function they are called global variables.

4. How to create and initialize array?

Create an Array :- 
    Syntax :- array_name = [];
Inialize values :-
    Syntax :- array_name[0] = “value”

5. How to create and initialize tuple?

Create an tuple:- 
    Syntax :- tuple_name = ();
Inialize tuple:-
    Syntax :- tup1 = ('physics', 'chemistry', 1997, 2000);

6. How to create and initialize dictionary?

Create an dictionary:- 
    Syntax :- dictionary_name = {};
Inialize dictionary:-
    Syntax :- dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

7. How to share common variables in multiple files?

Write all common variables in one file and import that file. To use the common variables.
Ex :- 
For example I want a, b & c to share between modules.
config.py :
a=0
b=0
c=0
module1.py: 
import config 
config.a = 1
config.b =2
config.c=3
print “ a, b & resp. are : “ , config.a, config.b, config.c

8. Explain about lamda in python?

Lamda is a  one line function. 
Ex :- 
k= lambda y: y + y
k(30) // returns 60

9. What is pickling and unpickling in python?

Pickling :- converts object  into a string representation writes it into a file.
UnPlickling :- it  is process of retrieving original python object from the stored string representation for use.

10. How to create an object in python?

Use classname followed by parenthesis
Ex :- 
class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'
x = MyClass() //creates object

www.000webhost.com