Basic Information On Python
Basic Information On Python
Python-I
1
Table of contents
1. Introduction---------------------------------------------------------------------------------4-5
a. Objective
b. Benefits
c. Problems
4. Variables--------------------------------------------------------------------------------------8-9
a. Objective
b. Benefits
c. Problems
5. Numbers---------------------------------------------------------------------------------------10
a. Objective
b. Benefits
c. Problems
7. Data structures--------------------------------------------------------------------------------14-17
a. Objective
b. Benefits
c. Problems
8. Modules----------------------------------------------------------------------------------------18-19
a. Objective
b. Benefits
c. Problems
2
10. Error and Exception-------------------------------------------------------------------------22
a. Objective
b. Benefits
c. Problems
3
1. Introduction
Ans: Yes
4. Is it true python is an ideal language for scripting and rapid application development?
6. Is it true python is easily extended with new functions and data types implemented in c or
c+ +?
Ans: Yes
Ans: /usr/local/bin/python3.5
Ans: Yes
Command: python -c command [arg]
4
11. By default python source files are encoded in which format?
Ans: UTF-8
12. Why is python 3.x interpreter not conflict with the simultaneously installed python 2.x?
5
2. Python Environment Setup
2. How to setup the path from where python fetch the code?
or
python -d scriptname
6
3. Python Programming Syntax
Objective: Below problems help you to understand about the syntax of Python
Ans: Line continuation character helps us to write in multiple lines without termination.
Denoted by (\).
4. What is the use of single quote , double and triple quote in python ?
Ans: python -h
7
4. Variables
Ans: Yes
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
Ans: Yes, it is possible to delete the single or multiple objects using del statement.
var1 = 1
String variable:
7. What is the difference between list and array? How to declare the list?
Ans: List: In case of the list we can assign the different data types values.
8
Syntax:
Array: In case of the array we can only assign same data types value.
8. What is the difference between list and tuple? How to declare tuple?
Ans: In case of list values are enclosed in brackets and their elements & size can be changed.
Whereas in case of tuple values are enclosed in parenthesis and their element cannot be changed.
Syntax :
9. How to declare the python dictionary? How to assign values and access?
Ans: Syntax :
9
5. Numbers
Ans: Yes
Example :
>>> 5 ** 2
3. Is it true operators with mixed type operands convert the integer operand to floating type?
Ans: Yes
Example :
>>> 4 * 3.75 -1
14.0
Ans: True
Example :
>>> 17 // 3
10
6. Control flow tools
Objective: Below problems help you to understand the functions and control flow tools
2. Suppose if you don't want to iterate over a sequence of numbers then which function will
you use?
Ans: No
Ans: Continue statement continues with the next iteration of the loop.
11
Ans: 1. First look at the Local symbol table.
2. In the local symbol tables of enclosing functions.
3. In the global symbol table
4. Built-in names
11. Is it true global variables cannot be directly assigned a value within a function, although
they may be referenced?
Ans: True
12. Is it true when a function calls another function, a new symbol table is created?
Ans: True
13. Is it true functions without a return statement do return a value ?if yes, then what it is
called?
Ans: True
Ans: True
Ans: True
Ans: True
12
20. Is it true keyword arguments are printed to match the order in which they were provided
in the function call?
Ans: True
Ans: True
Ans: Function annotations are completely optional metadata information about the types used by
user-defined functions.
Ans: Annotations are stored in the _annotations_ attribute of the function as a dictionary and have
no effect on any other part of the function.
Ans: parameter annotations are defined by a colon after the parameter name, followed by an
expression evaluating to the value of the annotations.
13
7. Data Structures
Objective: Below problems help you to understand the data structures of the python.
1. Suppose if you want to add an item at the end of the list. How will you add?
list.append(x)
2. Suppose if you want to extend the list. How will you extend?
list.extend(iterable)
3. Suppose if you want to insert the item at a given position. How will you insert?
list.insert(i,x)
The first argument is the index of the element before which to insert.
4. Suppose if you want to remove the item from the list whose value is x. How will you
remove?
list.remove(x)
5. Suppose if you want to remove the item at a given position from the list and return it.How
will you do?
list.pop([i])
6. Suppose if you want to remove all the items from the list. How will you do?
list.clear()
7. Suppose if you want to count the item in the list. How will you do?
14
list.count(x)
8. Suppose if you want to reverse the elements of the list. How will you do?
list.reverse()
9.In the list, how to add an item to the top of the stack?
10. In the list, how to retrieve an item from the top of the stack?
11. To implement a queue, use collection.deque which was designed to have fast appends and
pops from both ends.True or False?
Ans: True
12. List comprehensions provide a concise way to create lists. True or False?
Ans: True
13. List comprehensions can contain complex expressions and nested functions. True or
Fasle ?
Ans: True
Ans: True
15. The del statement can also be used to remove slices from a list or clear the entire list.True
or False?
Ans: True
16. List and string have many common properties, such as indexing and slicing
operations.True or False?
Ans: True
17. Tuples are immutable and usually contain a hetergeneous sequence of elements.True or
False ?
15
Ans: True
18. List are mutable and their elements are usually homogeneous and are accessed by
iterating over the list. True or False?
Ans: True
Ans: True
1. Membership testing
2. Eliminating duplicate entries.
Ans: True
22. The main operation on a dictionary is storing a value with some key and extracting the
value given the key. True or False?
Ans: True
24. When looping through dictionaries, the key and corresponding value can be retrieved at
the same time using the items() method. True or false?
Ans: True
25. When looping through a sequence, the position index and corresponding value can be
retrieved at the same time using the enumerate() function. True or False?
Ans: True
16
Example : >>>for i,v in enumerate(['a', 'b' , 'c'])
.... print(i,v)
0a
1b
2c
26. To loop over two or more sequences at the same time the entries can be paired with the
zip() function. True or False?
Ans: True
27. To loop over a sequence in reverse, first, specify the sequence in a forward direction and
then call the reversed() function.True or False?
Ans: True
28. To loop over a sequence in sorted order, use the sorted () function which returns a new
sorted list while leaving the source unaltered. True or False?
Ans: True
Example:
17
8. Modules
1. If you quit from the python interpreter and enter it again, the definitions you have made
(functions and variables) are lost.True or False?
Ans: True
2. Python has a way to put definitions in a file and use them in a script, False ?such a file is
called a module. True or false?
Ans: True
3. A module can contain executable definitions as well as function definitions. True or False?
Ans: True
4. For efficiency reasons, each module is only imported once per interpreter session.True or
False?
Ans: True
5. If you change your modules, you must restart the interpreter. True or False?
Ans: True
7. To speed up loading modules, python caches the compiled version of each version in the
pycache directory under the name module.version.pyc.True or False?
Ans: True
8. What are the two circumstances when python does not check the cache?
Ans: 1. It always recompiles and does not store the result for the module that's loaded directly from
the command line.
9. What are the two switches on the python command to reduce the size of a compiled
module?
18
2. -00: remove both assert statements and doc strings.
10. The module compileall can create .pyc files for all modules in a directory.True or False ?
Ans: True
Ans: dir()
12. dir() does not list the names of built-in functions and variables.True or False ?
Ans: True
13. Packages are a way structuring python's module namespace by using “dotted modules
names”.True or False.
Ans: True
19
9. Input and Output
Objective: Below problems help you to understand the different types of functions.
Example:
>>> s = 'Hello World'
>>>repr(s)
“'Hello,world.'”
>>>str(s)
“'Hello, world.'”
Example:
>>> '12' .zfill(5)
Ans: True
Example:
>>>import math
>>>print('The value of PI is approximately %5.3f.' % math.pi)
Ans:
>>> f = open('workfile' , 'w' )
6. What is the advantage of using the with keyword when dealing with file objects ?
20
Ans: File is properly closed after its suite finishes, even if an exception is raised at some point.
Example:
>>>with open('workfile') as f:
... read_data = f.read()
>>>f.closed
True
21
10. Errors and Exception:
Objective: Below problems help you to understand the Error and Exception.
Ans: True
Ans: True.
4. A try statement may have more than one except clause, to specify handlers for different
exceptions.True or False
Ans: True
5. The raise statement allows the programmer to force a specified exception to occur.True or
False.
Ans: True.
6. A finally clause is always executed before leaving the try statement, whether an exception
has occurred or not.True or False
Ans: True
Ans: The with statement allows objects like files to be used in a way that ensures they are always
cleaned up promptly and correctly.
22
11. Standard Library
Objective: Below problems help you to understand the different types of modules.
Ans: It provides dozens of functions for interacting with the operating system.
Command: python3.6
Command: import os
Command: os.getcwd()
Ans: >>>dir(os)
6.For daily file and directory management task, which module is used?
7. Which module provides a function for making file lists from directory wildcard searches?
23
>>> glob.glob('*.py')
['primes.py', 'random.py','quote.py']
8. Which is the most powerful and flexible command line processing module?
9. Which module processes sys.argv using the conventions of the Unix getopt() function?
11. The sys module also has attributes for stdin, stdout, and stderr.True or False?Please give
an example.
Ans: True
Example: >>>sys.stderr.write('warning, log file not found starting a new one\n')
Ans: sys.exit()
13. Which module provides regular expression tools advanced string processing?
Ans: re module
14. Which module gives access to the underlying C library functions for floating point math?
>>>import random
>>>random.choice(['apple', 'pear' , 'banana'])
24
>>>random.randrange(6)
4
Ans: statistics
>>> statistics.mean(data)
>>>statistics.median(data)
>>>statistics.variance(data)
17. What are the two modules which provide access the internet and processing internet
protocols?
18.Which is the module used to supplies classes for manipulating dates and times in both
simple and complex ways?
Example:
>>> from datetime import date
>>> now = date.today()
>>> now
25
>>>birthday = date(1991, 8, 30)
>>>age = now – birthday
>>>age.days
19. What are the common data archiving and compression formats modules?
Example:
20. What is the name of the module which quickly demonstrates a modest performance
advantage?
21. What is the name of the module which provides a tool for scanning a module and
validating tests embedded in a program's docstrings?
26
27