Rev Comp
Rev Comp
Data Types: Data Type specifies which type of value a variable can
store. type() function is
used to determine a variable's type in Python
Data Types In Python
1. Number
2. String
3. Boolean
4. List
5. Tuple
6. Set
7. Dictionary
Python tokens :
(1) keyword :
Keywords are reserved words. Each keyword has a specific meaning to
the Python interpreter, and we can use a keyword in our program only
for the purpose for which it has been defined. As Python is case
sensitive, keywords must be written exactly.
(2) Identifier: Identifiers are names used to identify a variable, function,
or other entities in a program. The rules for naming an identifier in
Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or
an underscore sign (_). This may be followed by any combination of
characters a–z, A–Z, 0–9 or underscore (_). Thus, an identifier cannot
start with a digit.
• It can be of any length. (However, it is preferred to keep it short and
meaningful).
It should not be a keyword or reserved word
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
(3) Variables: A variable in a program is uniquely identified by a name
(identifier). Variable
in Python refers to an object — an item or element that is stored in the
memory.
Comments: Comments are used to add a remark or a note in the
source code. Comments are not executed by interpreter. a comment
starts with # (hash sign). Everything following the # till the end of that
line is treated as a comment and the interpreter simply ignores it while
executing the statement.
Mutable and immutable data types : Variables whose values can be
changed after they are created and assigned are called mutable.
Variables whose values cannot be changed after they are created and
assigned are called immutable.
2 MARK QUESTIONS
Q1. Find the following python expressions:
a) (3-10**2+99/11)
b) not 12 > 6 and 7 < 17 or not 12 < 4
c) 2 ** 3 ** 2
d) 7 // 5 + 8 * 2 / 4 – 3
Q2. i) Convert the following for loop into while loop
for i in range(10,20,5):
print(i)
ii) Evaluate:- not false and true or false and true
Q3. What are advantages of using local and global variables ?
Q4. Remove the errors from the following code Rewrite the code by
underlining the errors .
x = int((“enter the value”)
for i in range [0,11]:
if x = y
print x+y
else:
print x-y
Q5. Rewrite the following code in python after removing all syntax errors.
Underline each correction done in the code:
def func(x):
for i in (0,x):
if i%2 =0:
p=p+1
else if i%5= =0
q=q+2
else:
r=r+i
print(p,q,r)
func(15)
Q6. Write the output of the following code:-
String1="Coronavirus Disease"
print(String1.lstrip("Covid"))
print(String1.rstrip("sea"))
Q7. Write the ouput of the following code:-
Text = "gmail@com"
L=len(Text)
Ntext=""
for i in range(0,L):
if Text[i].isupper():
Ntext=Ntext+Text[i].lower()
elif Text[i].isalpha():
Ntext= Ntext+Text[i].upper()
else:
Ntext=Ntext+'bb'
print(Ntext)
Q8. L = [“abc”,[6,7,8],3,”mouse”]
Perform following operations on the above list L.
i)L[3:] ii) L[: : 2] iii)L[1:2] iv) L[1][1]
Q9.Write the output of the following:
word = 'green vegetables'
print(word.find('g',2))
print(word.find('veg',2))
print(word.find('tab',4,15))
3 MARK QUESTIONS
Q1. Write the python program to print the index of the character in a
string.
Example of string : “pythonProgram”
Expected output:
Current character p position at 0
Current character y position at 1
Current character t position at 2
Q2. Find and write the output of the following python code:
string1 = "Augmented Reality"
(i) print(string1[0:3]) (ii) print(string1[3:6]) (iii) print(string1[:7])
(iv) print(string1[-10:-3]) (v) print(string1[-7: :3]*3) (vi)
print(string1[1:12:2])
Q3. Find the output of the give program :
x = "abcdef"
j = "a"
for i in x:
print(j, end = " ")
Q4. Find output generated by the following code:
i=3
while i >= 0:
j=1
while j <= i:
print(j,end = ' ')
j=j+1
print()
i=i-1
Q5. Find output generated by the following code:
i=1
y = 65
while i<=5:
j=i
while j<=I:
print(chr(y),end=’ ‘)
j= j+1
y = y+1
print()
i=i+1
4 MARK QUESTIONS
Q1.Differentiate between break and continue statement used in python.
Q2What is comment in python ? Explain its significance.
Q3.Explain the types of errors occurring in python programming
language.
5 MARK QUESTIONS
Q1.Differentiate between type conversion and type casting in python
with examples.
Q2.Explain mutable and immutable objects in python with examples.
Q3. What is the use of else statement in for loop and in while loop ?
Explain.
ANSWERS
ANSWER OF 1 MARK QUESTIONS
1) (iv)
2) (iv)
3) (ii)
4) (iii)
5) (iii)
6) (iii)
7) (iii)
8) (iv)
9) (iii)
10) (ii)
ANSWER OF 2 MARK QUESTIONS
1) a) -88.0 b) True c)512 d)2.0
2) (i) i=10
while(i<20):
print(i)
i+=5
(i) true
3. Advantages of Local Variable
o The same name of a local variable can be used in different functions
as it is only recognized by the function in which it is declared.
o Local variables use memory only for the limited time when the function
is executed; after that same memory location can be reused.
Advantages of Global Variable
o Global variables can be accessed by all the functions present in the
program.
o Only a single declaration is required.
o Very useful if all the functions are accessing the same data.
4.
x = int(input(“enter the value”))
for y in range(0,11):
if x = = y:
print(x+y)
else:
print(x - y)
5. def func(x):
for i in range(0,x): Error 1
if i%2 ==0: Error 2
p=p+1
elif i%5= =0 Error 3
q=q+2
else:
r=r+i
print(p,q,r)
func(15)
6. ronavirus Disease
Coronavirus Di
7. GMAILbbCOM
8. I) [‘mouse’] ii) [‘abc’,3] iii)[ [ 6,7,8] ] iv) 7
9. 8
6
10
STRINGS
A sequence of characters is called a string. Strings are used by
programming languages to manipulate text such as words and
sentences.
Strings literal in Python are enclosed by double quotes or single quotes.
String literals can span multiple lines, to write these strings triple quotes
are used.
>>> a = ‘’’ Python
Programming
Language’’’
Empty string can also be created in Python .
>>> str = ‘ ‘
Accessing values in Strings:
Each individual character in a string can be assessed using a technique
called indexing .
Python allows both positive and negative indexing.
S = “Python Language”
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
PythonLanguage
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
>>> S[7]
L
Deleting a String
>>>S[-10]
n
As we know, strings are immutable, so we cannot delete or remove the
characters from the string but we can delete entire string using del
keyword.
>>> strl = " WELCOME "
>>> del strl
>>> print ( str1 )
NameError : name ' strl ' is not defined .
String Slicing
To access some part of a string or substring, we use a method called
slicing.
Syntax: string_name[start : stop]
>>> str1 = " Python Program "
>>> print ( str1[ 3: 8])
hon P
>>> print ( str1 [ : -4 ] )
Python Pro
>>> print ( strl [ 5 : ] )
n Program
Strings also provide slice steps which used to extract characters from
string that are not consecutive. Syntax string_name [ start : stop : step ]
>>> print ( stri [ 2 : 12 : 3 ] )
tnrr
We can also print all characters of string in reverse order using [ ::-1 ]
>>> print ( strl [ :: - 1 ] )
margorP nohtyP
Traversing a String
1. Using ' for’ loop: for loop can iterate over the elements of a sequence
or string . It is used when you want to traverse all characters of a string.
eg.
>>> sub = " GOOD "
>>> for i in subs:
print ( i )
Output:
G
O
O
D
3 MARKS QUESTIONS
Q1. Which of the string built in methods are used in following conditions?
ii)Returns the length of a string
iii)Removes all leading whitespaces in string
iv) Returns the minimum alphabetic character from a string
Q2. Write a program to remove all the characters of odd index value in a
string
Q3. Write a python program to count the frequencies of each elements
of a list using dictionary
Q4. what will be the output of the following python code
L = [10,20]
L1 = [30,40]
L2 = [50,60]
L.append(L1)
print(L)
L.extend(L2)
print(L)
print(len(L)
Q5. Find the output of the given question
t = (4,0,’hello’,90,’two’,(‘one’,45),34,2)
i) t[5]
ii) t[3:7]
iii) t[1] + t[-2]
4 marks questions
Q1. Find the output
i) 'python'.capitalize()
ii) max('12321')
iii) 'python'.index('ho')
iv) 'python'.endswith('thon')
Q2. Consider the following code and answer the question that follows.
book = {1:'Thriller',2:'Mystery',3:'Crime',4:'Children Stories'}
library = {5:'Madras Diaries',6:'Malgudi Days'}
v)Ramesh wants to change the book ‘Crime’ to ‘Crime Thriller’. He has
written the following code:
book['Crime'] = 'Crime Thriller'
but he is not getting the answer. Help him to write the correct command.
vi)Ramesh wants to merge the dictionary book with the dictionary library.
Help him to write the command.
Q3. Write the suitable method names for the conditions given below:
i) Add an element at the end of the list
ii) Return the index of first occurrence of an element
iii) Add the content of list2 at the end of list1
iv) Arrange the elements of a list1 in descending order
5 MARKS QUESTIONS
Q1. Find the output of the following code:
a = (5,(7,5,(1,2)),5,4)
print(a.count(5))
print(a[1][2])
print(a * 3)
print(len(a))
b = (7,8,(4,5))
print(a + b)
Q2. Explain the following string functions with examples.
i) title() ii) count( ) iii) find() iv) index() v) join()