0% found this document useful (0 votes)
8 views12 pages

Python Mid 1 Scheme

Uploaded by

Mannered Zone
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views12 pages

Python Mid 1 Scheme

Uploaded by

Mannered Zone
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Python Mid-1 Scheme

PART A (5 X 2= 10 Marks) (Answer all the questions)


No. Question (s)
1 Write rules for creating identifier in python. Give an Example
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or
digits (0 to 9) or an underscore _.
2. An identifier cannot start with a digit.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
5. An identifier can be of any length.
6. Python is case sensitive
7. Multiple name in a variable could be underscore
Example: Python programming( Not Valid )
Example: Python_subject(Valid)
2 Distinguish the features of Python Programming.
• Readable: Syntax is similar to English and so the language is readable.
• Easy to learn: Python is expressive language and thus it is easy to learn.
• Cross-Platform: Python can run on different operating systems such as Windows, Mac, Linux,
etc.
• Open Source: This is an open-source language and free to all.
• Large Standard library: Python has a very big library of pre-defined functions.
• Free: Python is freely available to use over your applications.
• Supports Exception Handling: Python has an advanced feature of exception handling.
• Advanced Features: Python supports many advanced features.
• Automatic Memory Management: Python supports this feature which automatically allocates
and free the memory whenever used.
3 Define command line arguments. Give an Example
The arguments that are given after the name of the program in the command line shell of the operating
system are known as Command Line Arguments.
Python provides various ways of dealing with these types of arguments. The three most common are:
• Using sys.argv
• Using getopt module
• Using argparse module
4 What is REPL?
Python is in an interactive Read-Eval-Print Loop (REPL) environment. That simply means
starting up the interpreter and typing commands to it directly. The interpreter:
• Reads the command you enter
• Evaluates and executes the command
• Prints the output (if any) to the console
• Loops back and repeats the process
5 What iskeyword Arguments.
Keyword arguments (or named arguments) are values that, when passed into a
function, are identifiable by specific parameter names. A keyword argument is
preceded by a parameter and the assignment operator, = .
def team(name, project):
print(name, "is working on an", project)
team(project = "Edpresso", name = 'FemCode')
PART B (3 X 10= 30 Marks) Answer THREE questions
No Questions (6 to 11)
Describe types of operators in python. For each operator give a suitable example.
Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operators: Arithmetic Operators some basic arithmetic operators are +,-,*,/,%,**, and //.
You can apply these operators on numbers as well as variables to perform corresponding operations.

Comparison (Relational) Operators:


These operators compare the values on either sides of them and decide the relation among them.
They are also called Relational operators. Assume variable a holds 10 and variable b holds 20, then:
Assignment Operators:
Python Assignment operator is used for assigning the value of right operand to the left operand.

Logical operator
Logical operators in Python are used for conditional statements are true or false. Logical operators in
Python are AND, OR and NOT. For logical operators following condition are applied.
• For AND operator – It returns TRUE if both the operands (right side and left side) are true.
• For OR operator- It returns TRUE if either of the operand (right side or left side) is true.
• For NOT operator- returns TRUE if operand is false.
Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation.

Membership Operators:
Membership operators are operators used to validate the membership of a value. It tests for
membership in a sequence, such as strings, lists, or tuples. There are two membership operators.
• In Operator
• Not in Operator
Identity Operators:
Identity operators compare the memory locations of two objects. There are two Identity operators.
• Is Operator
• Not is Operator
‘is’ operator:
It evaluates to true if the variables on either side of the operator point to the same object and false
otherwise
‘isnot’operator –
Evaluates to false if the variables on either side of the operator point to the same object and true
otherwise
What is a Statement? Describe the different types of statement in python with suitable
7
examples
A statement is an instruction that a Python interpreter can execute. Python statement ends with the
token NEWLINE character. It means each line in a Python script is a statement.
Example: a = 10 is an assignment statement. where a is a variable name and 10 is its value. There
are other kinds of statements such as if statement, for statement, while statement, etc.,
There are mainly four types of statements in Python,
• Print statements,
• Assignment statements,
• Conditional statements,
• Looping statements.

More Python Statements in Brief


• Multi-Line Statements
• Python Compound Statements
• Simple Statements
• Expression statements
• The pass statement
• The del statement

Explain different set operations with suitable python programs


Set Operations:
Sets can be used to carry out mathematical set operations like union, intersection, difference and
symmetric difference. We can do this with operators or methods.
1. Set Union
2. Set Intersection
3. Set Difference
4. Set Symmetric Difference
8
1. Set Union: Union of A and B is a set of all elements from both sets. Union is performed
using ‘|’ operator.Same can be accomplished using the union() method.
2. Set Intersection: intersection of A and B is a set of elements that are common in both the
sets. Intersection is performed using ‘&’ operator. Some can be accomplished using the
intersection() method.

3. Set Difference: Differnce of set B from set A (A-B) is a set of elements what are only in A
but not in B. similarly, B-A is a set of elements in B but not in A.Difference is performing
using ‘-‘ operator. Same can be accomplished using the difference() method.
4. Set Symmetric Difference: symmetric difference of A and B is a set of elements in A and B
but not in both (excluding the intersection). Symmetric difference is performed using ‘^’
operator. Same can be accomplished using the method symmetric_difference().

Write a Python program to print 1 to n numbers using loop statements

While Loop:

(OR)
Using For Loop:

Write a Python program to find the transpose of the matrix


# Program to transpose a matrix using a nested loop
X = [[12,7],[4 ,5],[3 ,8]] ''' Program to transpose a matrix
result = [[0,0,0],[0,0,0]] using list comprehension'''
# iterate through rows X = [[12,7],[4 ,5],[3 ,8]]
for i in range(len(X)): result = [[X[j][i] for j in
# iterate through columns range(len(X))] for i in
for j in range(len(X[0])): range(len(X[0]))]
result[j][i] = X[i][j]
for r in result: for r in result:
print(r) print(r)
10 Define tuple. Interpret any four tuple handling methods with suitable examples
Tuple:
• Tuple is exactly same as list except that it is immutable. i.e oncetuple object is created,
then any specific changes in that object couldn’t be done. Hence Tuple is read only
version of List.
• If our data is static, then Tuple is used.
• Heterogeneous objects are allowed.
• We can preserve insertion order and we can differentiate duplicate objects by using
index. Hence index will play very important role in tuple also.
• Tuple support both +ve and -ve index. +ve index means forward direction (from left to
right) and -ve index means backward direction (from right to left).
• We can represent Tuple elements within Parenthesis and with comma separator
Handling Methods:
• Len()
• Count()
• Index()
• Sorted()
• Max() and Min()
• Cmp()
Tuple creation:
t=()
Creation of empty tuple
t=(10,)
t=10, creation of single valued tuple ,parenthesis are optional, should ends with comma
t=10,20,30
t=(10,20,30) creation of multi values tuples & parenthesis are optional
By using tuple() function:
list=[10,20,30]
t=tuple(list)
print(t)
t=tuple(range(10,20,2))
print(t)

Accessing elements of tuple:


We can access either by index or by slice operator
By using index:
t=(10,20,30,40,50,60)
print(t[0]) #10
print(t[-1]) #60
print(t[100]) Index Error: tuple index out of range
By using slice operator:
t=(10,20,30,40,50,60)
print(t[2:5])
print(t[2:100])
print(t[::2])
Output:
(30, 40, 50)
(30, 40, 50, 60)
(10, 30, 50)
Tuple Operations:
(+):
The + operator returns a tuple containing all the elements of the first and the second tuple
object.
>>>t1=(10,20,30)
>>>t2=(40,50,60)
>>>t3=t1+t2
>>>print(t3) # (10,20,30,40,50,60)
(*):
The * operator Concatenates multiple copies of the same tuple.
. >>>t1=(10,20,30)
>>>t2=t1*3
>>>print(t2) # (10,20,30,10,20,30,10,20,30)
([]):
The [] operator returns the item at the given index. A negative index counts the position from
the right side.
>>>t1=(1,2,3,4,5,6)
>>>t1[3]
4
>>>t1[-2]
5
([:]):

The [:] operator returns the items in the range specified by two index operands separated by
the: symbol.
If the first operand is omitted, the range starts from zero. If the second operand is omitted, the
range goes up to the end of the tuple.

>>>t1=(1,2,3,4,5,6)
>>>t1[1:3]
(2, 3)
>>>t1[3:]
(4, 5, 6)
>>>t1[:3]
(1, 2, 3)
in:

The in operator returns true if an item exists in the given tuple.


>>> t1=(1,2,3,4,5,6)
>>> 5 in t1
True
>>> 10 in t1
False
not in:

The not in operator returns true if an item does not exist in the given tuple.
>>> t1=(1,2,3,4,5,6)
>>> 4 not in t1
False
>>> 10 not in t1
True
Functions of Tuple:

1. len()

To return number of elements present in the tuple


Eg: t=(10,20,30,40)
print(len(t)) #4
2. count()

To return number of occurrences of given element in the tuple


Eg: t=(10,20,10,10,20)
print(t.count(10)) #3

3. index()

Returns index of first occurrence of the given element. If the specified element is not available
then we will get Value Error.
Eg: t=(10,20,10,10,20)
print(t.index(10)) #0
print(t.index(30)) Value Error: tuple .index(x): x not in tuple

4. sorted()
To sort elements based on default natural sorting order
t=(40,10,30,20)
t1=sorted(t)
print(t1)
print(t)
Output:
[10, 20, 30, 40]
(40, 10, 30, 20)

5. min() and max() functions:


These functions return min and max values according to default natural sorting order.
t=(40,10,30,20)
print(min(t)) #10
print(max(t)) #40

6. cmp():
It compares the elements of both tuples. If both tuples are equal then returns 0 If the first tuple
is less than second tuple then it returns -1 If the first tuple is greater than second tuple then it
returns +1
t1=(10,20,30)
t2=(40,50,60)
t3=(10,20,30)
print(cmp(t1,t2)) # -1
print(cmp(t1,t3)) # 0 6.
print(cmp(t2,t3)) # +1
Note: cmp() function is available only in Python2 but not in Python 3.
Write a Python program to find the vowels in a given string

11
Write a Python program to check the given list contains duplicates elements or not

OR
Course Coordinator/Instructor Head of the Department

You might also like