Most Important Question:-
- 1. What is Python?
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.
- 2. Name some of the features of Python.
- • It supports functional and structured programming methods as well as OOP.
- • It can be used as a scripting language or can be compiled to byte-code for building large applications.
- • It provides very high-level dynamic data types and supports dynamic type checking.
- • It supports automatic garbage collection.
- • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
- 3. What is the purpose of PYTHONPATH environment variable?
PYTHONPATH – It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.
- 4. What is the purpose of PYTHONSTARTUP environment variable?
PYTHONSTARTUP – It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.
- 5. What is the purpose of PYTHONCASEOK environment variable?
PYTHONCASEOK − It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
- 6. What is the purpose of PYTHONHOME environment variable?
PYTHONHOME − It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.
- 7. Is python a case sensitive language?
Yes! Python is a case sensitive programming language.
- 8. What are the supported data types in Python?
Python has five standard data types −
- • Numbers
- • String
- • List
- • Tuple
- • Dictionary
- 9. What are Python’s dictionaries?
Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
- 10. How will you get all the keys from the dictionary?
Using dictionary.keys() function, we can get all the keys from the dictionary object.
- 11. How will you get all the values from the dictionary?
Using dictionary.values() function, we can get all the values from the dictionary object.
- 12. Explain Inheritance in Python with an example.
Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.
They are different types of inheritance supported by Python:
- 1. Single Inheritance – where a derived class acquires the members of a single super class.
- 2. Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
- 3. Hierarchical inheritance – from one base class you can inherit any number of child classes
- 4. Multiple inheritance – a derived class is inherited from more than one base class.
- 13. Explain split(), sub(), subn() methods of “re” module in Python.
To modify the strings, Python’s “re” module is providing 3 methods. They are:
- • split() – uses a regex pattern to “split” a given string into a list.
- • sub() – finds all substrings where the regex pattern matches and then replace them with a different string
- • subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
- 14. What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
- 15. What is map function in Python?
map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions
- 16. What is TkInter?
TkInter is Python library. It is a toolkit for GUI development. It provides support for various GUI tools or widgets (such as buttons, labels, text boxes, radio buttons, etc) that are used in GUI applications. The common attributes of them include Dimensions, Colors, Fonts, Cursors, etc.
- 17. Is Python object oriented? what is object oriented programming?
Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:
Encapsulation, Data Abstraction, Inheritance, Polymorphism.
- 18. What is multithreading? Give an example.
It means running several different programs at the same time concurrently by invoking multiple threads. Multiple threads within a process refer the data space with main thread and they can communicate with each other to share information more easily.Threads are light-weight processes and have less memory overhead. Threads can be used just for quick task like calculating results and also running other processes in the background while the main program is running.
- 19. Does Python supports interfaces like in Java? Discuss.
Python does not provide interfaces like in Java. Abstract Base Class (ABC) and its feature are provided by the Python’s “abc” module. Abstract Base Class is a mechanism for specifying what methods must be implemented by its implementation subclasses. The use of ABC’c provides a sort of “understanding” about methods and their expected behaviour. This module was made available from Python 2.7 version onwards.
- 20. Differentiate between append() and extend() methods.?
Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.
- append(element) – adds the given element at the end of the list which has called this method.
- extend(another-list) – adds the elements of another-list at the end of the list which is called the extend method.
- 21. Name few methods that are used to implement Functionally Oriented Programming in Python?
Python supports methods (called iterators in Python3), such as filter(), map(), and reduce(), that are very useful when you need to iterate over the items in a list, create a dictionary, or extract a subset of a list.
- filter() – enables you to extract a subset of values based on conditional logic.
- map() – it is a built-in function that applies the function to each item in an iterable.
- reduce() – repeatedly performs a pair-wise reduction on a sequence until a single value is computed.
- 22. What is the output of the following?
- x = [‘ab’, ‘cd’]
- print(len(map(list, x)))
- A TypeError occurs as map has no len().
- 23. What is the output of the following?
- x = [‘ab’, ‘cd’]
- print(len(list(map(list, x))))
- Explanation: The length of each string is 2.
- 24. Which of the following is not the correct syntax for creating a set?
Answer : a
Explanation : The argument given for the set must be an iterable.
- 25. How to retrieve data from a table in MySQL database through Python code?
- • import MySQLdb module as : import MySQLdb
- • establish a connection to the database.
- • db = MySQLdb.connect(“host”=”local host”, “database-user”=”user-name”, “password”=”password”, “database-name”=”database”)
- • initialize the cursor variable upon the established connection: c1 = db.cursor()
- • retrieve the information by defining a required query string. s = “Select * from dept”
- • fetch the data using fetch() methods and print it. data = c1.fetch(s)
- • close the database connection. db.close().
- 26. What are the built-in type does python provides?
There are mutable and Immutable types of Pythons built in types Mutable built-in types
- • List
- • Sets
- • Dictionaries
Immutable built-in types
- • Strings
- • Tuples
- • Numbers
- 27. What is lambda in Python?
It is a single expression anonymous function often used as inline function.
- 28. In Python what are iterators?
In Python, iterators are used to iterate a group of elements, containers like list.
- 29. In Python what is slicing?
A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.
- 30. What are generators in Python?
The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.
- 31. How can you copy an object in Python?
To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.
- 32. Mention what are the rules for local and global variables in Python?
- Local variables:If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.
- Global variables:Those variables that are only referenced inside a function are implicitly global.
- 33. Explain how can you generate random numbers in Python?
To generate random numbers in Python, you need to import command as
- import random
- This returns a random floating point number in the range [0,1)