Unit 1st Revised Python 12 2024-2025
Unit 1st Revised Python 12 2024-2025
Keywords in Python
There are 33 keywords in Python 3.7. This number can vary slightly in the course of time. All the keywords except True, False and
None are in lowercase and they must be written as it is. The list of all the keywords is given below.
and del from not while as elif global or with assert else if pass yield
break except import print class exec in raise continue finally is return def for
lambda try
Operators in Python
Python language supports the following types of operators-
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Operators in Python: Arithmetic
Assume a=10 and b=20
Relational Operators are used to show relationship between two values or variables. Following are the relational operators
< (less than), >(greater than) , <= (less than equal to), >= (greater than equal too) ,!= (Not equal to) and = = (equality check
operator)
Logical Operator in Python
Operators in Python: Assignment
Bitwise operator works on bit and performs bit by bit operation. Assume if –
A=60 and B=13; now in binary they will be as follows
A(60)=00111100
B(13)=00001101
list=[1, 2, 3, 4, 5] Output
print("Elements in the list: ",list) if(3 in list): Elements in the list: [1, 2, 3, 4, 5] 3 available in the list
print(3, "available in the list") else:
print(3, "Not available in the list")
List in Python
Creating a list and accessing its elements Output
a=[10,20,'abc',30,3.14,40,50] [10, 20, 'abc', 30, 3.14, 40, 50]
print(a) 10 20 abc 30 3.14 40 50
for i in range(0,len(a)): 50 40 3.14 30 abc 20 10
print(a[i], end=' ') 50 40 3.14 30 abc 20 10
print('\n') 50 40 3.14 30 abc 20 10
for i in range(len(a)-1,-1,-1):
print(a[i], end=' ')
print('\n')
for i in a[::-1]:
print(i, end=' ')
print('\n')
for i in reversed(a):
print(i, end=' ')
Tuple in Python
It is a sequence of immutable objects. It is just like a list. Difference between a tuple and a list is that the tuple cannot be
changed like a list. List uses square bracket whereas tuple use parentheses.
L=[1,2,3,4,5] Mutable Elements of list can be changed
T=(1,2,3,4,5) Immutable Elements of tuple can not be
changed
Creating a tuple and accessing its elements Output
a=(10,20,'abc',30,3.14,40,50) (10, 20, 'abc', 30, 3.14, 40, 50)
print(a) 10 20 abc 30 3.14 40 50
for i in range(0,len(a)): 50 40 3.14 30 abc 20 10
print(a[i], end=' ') 50 40 3.14 30 abc 20 10
print('\n') 50 40 3.14 30 abc 20 10
for i in range(len(a)-1,-1,-1):
print(a[i], end=' ')
print('\n')
for i in a[::-1]:
print(i, end=' ')
print('\n')
for i in reversed(a):
print(i, end=' ')
Function Description
Dictionary in Python
Dictionary in Python is an unordered collection of data values, used to store data values along with the keys. Dictionary holds key:
value pair. Key value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by
a colon:, whereas each key is separated by a ‘comma’.
dict={ “a": “alpha", “o": “omega", “g": ”gamma” }
Q1. What is a python variable? Identify the variables that are invalid and state the reason
Class, do, while, 4d, a+
Ans: - A variable in python is a container to store data values.
a) do, while are invalid because they are python keyword
b) 4d is invalid because the name can’t be started with a digit.
c) a+ is also not validas no special symbol can be used in name except underscore ( _ ).
Ans: - if N>=0:
print(“odd”)
else:
print("even")
Q.4 What problem occurs with the following code
X=40
while X< 50 :
print(X)
Ans: - The given code does not have the incrementing value of X, thus the loop becomes endless.
Q5. What will be the output of the following code snippet?
values =[ ]
for i in range (1,4):
values.append(i)
print(values)
Ans: [1]
[1 ,2 ]
[1,2,3]
Q6. Find the error in following code. State the reason of the error.
aLst = { ‘a’:1 ,' b':2, ‘c’:3 }
print (aLst[‘a’,’b’])
Ans: The above code will produce KeyError, the reason being that there is no key same as the list [‘a’,’b’] in dictionary
aLst.
Application Based Questions ( 3 Marks)
Ans:
f=20 #( as for is a keyword in python)
for1=50 #(: can not be used here)
for3=f+for1 #(for2 not defined)
print(for3)