1. Write a note on modular design.
Modules refer to a file containing Python statements and definitions.
A module allows you to logically organize your Python code.
The file name is the module name with suffix.py appended.
Let us create a module. Type the following and save it as example.py
2. Define tuple. Write the syntax of tuple.
A tuple is a sequence of values. The values can be any type, and they are indexed by
integers, so in that respect tuples are a lot like lists. The important difference is that tuples
are immutable.
t1 = 'a',
>>> type(t1)
<class 'tuple'>
3. What is list comprehension?
List comprehensions are concise and easy to read, at least for simple expressions. And
theyare usually faster than the equivalent for loops, sometimes much faster.
4. List out the built in functions in dictionaries.
cmp(dict1, dict2)
len(dict)
str(dict)
type(variable)
5. List different modes in file opening.
MODES DESCRIPTION
r' Open a file for reading. (default)
Open a file for writing. Creates a new file if it does not exist or
'w'
truncates the file if it exists.
Open a file for exclusive creation. If the file already exists, the
'x'
operation fails.
Open for appending at the end of the file without truncating it. Creates
'a'
a new file if it does not exist.
't' Open in text mode. (default)
'b' Open in binary mode.
'+' Open a file for updating (reading and writing)
6. What is import statement in python?
Python code in one module gains access to the code in another module by the process
of importing it.
def add(a, b):
result = a + b
return result
We can import this module in the interactive python shell and call the functions by prefixing
them example as the name of the module is example.py.
>>> import example
>>> example.add(4,2)
6
7. How formatting is done in python?
"%" operator and format() method are used to formatting strings.
Example:
# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
8. Write a python program to copy content of one file to another file.
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write(line)
9. Write down the steps to create python packages.
1. Create a directory and give it your package's name.
2. Put your classes in it.
3. Create a __init__.py file in the directory
10. How to alias a python list?
If a refers to an object and you assign b = a, then both variables refer to the same object:
>>> a = [1, 2, 3]
>>> b = a
>>> b is a True