Python Mid 1 Scheme
Python Mid 1 Scheme
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.
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().
While Loop:
(OR)
Using For Loop:
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 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()
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)
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