Assignment 2
Assignment 2
Q1. Write python code to repeat the following string 9 times using string operator ‘*’. i.)Python
ii.)Mathematics
>>> A="Python"
>>> B="Mathematics"
>>> print(A*9)
PythonPythonPythonPythonPythonPythonPythonPythonPython
>>> print(B*9)
MathematicsMathematicsMathematicsMathematicsMathematicsMathematicsMathematicsMathematicsMath
ematics
Q2. Use python code to construct string operation ‘+’ below string. i.)string1=Hello,string2=World! ii.)
string1=Good,string2=Morning.
>>> string1="Hello"
>>> string2="World!"
>>> print(string1+string2)
HelloWorld!
>>> string1="Good"
>>> string2="Morning"
>>> print(string1+string2)
GoodMorning
Q3. Repeat the following string 11 times using the string operator ’*’ on python. i.)LATEX ii.)MATLAB
>>> A="LATEX"
>>> B="MATLAB"
>>> print(A*11)
LATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEX
>>> print(B*11)
MATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLAB
Q4. Write python program which deals with concatenation and repetition of lists. LIST1=[15,20,25,30,35,40]
LIST2=[7,14,21,28,35,42] i.)Find List1+List2 ii.)Find 9*List1 iii.)Find 7*List2
>>> List1=[15,20,25,30,35,40]
>>> List2=[7,14,21,28,35,42]
>>> List1+List2
[15, 20, 25, 30, 35, 40, 7, 14, 21, 28, 35, 42]
>>> 9*List1
[15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40,
15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40, 15, 20, 25, 30, 35, 40]
>>> 7*List2
[7, 14, 21, 28, 35, 42, 7, 14, 21, 28, 35, 42, 7, 14, 21, 28, 35, 42, 7, 14, 21, 28, 35, 42, 7, 14, 21, 28, 35, 42, 7, 14,
21, 28, 35, 42, 7, 14, 21, 28, 35, 42]
Q5. Using python code sort the tuple in ascending and decending order 5,-3,0,1,6,-6,2.
>>> tuple1=(5,-3,0,1,6,-6,2)
>>> tuple(sorted(list(tuple1)))
(-6, -3, 0, 1, 2, 5, 6)
>>> tuple(sorted(tuple1,reverse=True))
>>> tuple1=(49,17,23,54,36,72)
>>> tuple(sorted(list(tuple1)))
>>> M=[1,2,3,4]
>>> print(len(M))
>>> L1="XYZ"
>>> L2="pqr"
>>> L=L1+L2
>>> print(L)
XYZpqr
>>> s[:7]
'Make In'
>>> s[:9]
'Make In I'
Q8. Write python code to list name and roll number of 5 students in B.Sc.(computer science).
>>>
S=[{"name":"Raj","roll_no":1},{"name":"shaam","roll_no":2},{"name":"Rohit","roll_no":3},{"name":"karan","ro
ll_no":4},{"name":"tejas","roll_no":5}]
>>> for student in S:
...
Q9. Repeate the following string 7 times using the string operator ’*’ on python. i.)Complex number ii.)Real
number.
>>> print(A)
Complex number
>>> print(B)
Real number
Q10. The following two statements using the ‘+’string operation on python. i.)string1=India Won,
string2=World Cup ii.)string1=God, string2=is Great
>>> print(String1+String2)
>>> String1="God"
>>> print(String1+String2)
>>> String1="God"
>>> print(String1+String2)
God is Great
Q11. Use python code to generate second,fifth,eight characters from string ‘MATHEMATICS’.
>>> s="MATHEMATICS"
>>> print(s[:2])
MA
>>> print(s[:5])
MATHE
>>> print(s[:8])
MATHEMAT
Q12. Use python code to find minimum value from the given numbers 16,3,5,48,2,4,5,6,78,12,5,6,24.
>>> min(A)
Q13. Write a python program using tuple that swap the values of two variables.
... x,y=y,x
...
>>> x=4
>>> y=5
>>> swap(x,y)
(5, 4)
>>> tuple1=[1,2,3,4,5,6,7,8,9]
>>> print(tuple)
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Q15. Write python code to display tuple ‘I am Indian’ and the second letter in this tuple.
>>> t1=['I', ' ', 'a' , 'm', ' ' , ' I' , 'n' , 'd' , 'i' , 'a' , 'n']
>>> print("tuple:",''.join(t1))
tuple: I am Indian
>>> second_letter=t1[1]
second letter:
Q16. Write python code to find the tuple ‘MATHEMATICS’ from range 3 to 9.
>>> tuple=("MATHEMATICS")
>>> print(tuple)
MATHEMATICS
>>> tuple[3:9]
'HEMATI'
Q17. Write python code to list name and birth date of 5 students in your class.
>>>
S=[{"name":"Raj","birth_date":1/9/2004},{"name":"shaam","birth_date":2/8/2003},{"name":"Rohit","birth_da
te":3/7/2004},{"name":"karan","birth_date":4/6/2004},{"name":"tejas","birth_date":5/4/2003}]
...
>>> numbers=[-10,10,-1,1,0]
...
Modulus of-10:10
Modulus of10:10
Modulus of-1:1
Modulus of1:1
Modulus of0:0
Q19. Using python code to list Name of 5 teacher in your college with their subject.
>>>
S=[{"teacher_name":"Raj","subject":"HISTORY"},{"teacher_name":"shaam","subject":"COMPUTER"},{"teacher
_name":"Rohit","subject":"SCIENCE"},{"teacher_name":"karan","subject":"MARATHI"},{"teacher_name":"Teja
s","subject":"MATHS"}]
...