Assignment 2.ipynb - Colab
Assignment 2.ipynb - Colab
a=10
type(a)
int
b=3.14
type(b)
float
c="Hello"
type(c)
str
d=True
type(d)
bool
e=[1,2,3]
type(e)
list
f={'name':"Alice",'age':25}
type(f)
dict
g=(10,20,30)
type(g)
tuple
Ques3: Convert z=100 to a float.
z=100
float(z)
100.0
a=0
bool(a)
False
Ques5: Create a list of the first 5 prime numbers. Add the number 13 to the list. Print the updated list.
l=[1,3,5,7,11]
l.append(13)
print(l)
[1, 3, 5, 7, 11, 13]
X=[1,2,3,4,5]
X.append(6)
print(X)
[1, 2, 3, 4, 5, 6]
X.insert(1,7)
print(X)
[1, 7, 2, 3, 4, 5, 6]
X.pop()
print(X)
[1, 7, 2, 3, 4, 5]
X.sort()
print(X)
[1, 2, 3, 4, 5, 7]
Ques7: Given a list numbers=[10,20,30,40,50], write a program to calculate and print the sum and average of the
numbers in the list
numbers=[10,20,30,40,50]
sum(numbers)
len(numbers)
average=sum(numbers)/len(numbers)
print(average)
30.0
Ques8: Explain the difference between = and == in python. Give examples where they would produce different
results.
'=' is an Assignment Operator used to assign a value to a variable. It does not compare values; instead, it stores the
value on the right side into the variable on the left side.
print(x)
Output will be 5
'==' is an Equal to Operator which is used to compare two values to check if they are equal. It returns True if they are
equal and False if they are not. it is a relational operator.
eg: x = 5
y=5
print(x == y)
b) Ask the user to enter a word and check if it exists in the list.
c) Print the index to the word if it is found , otherwise print a message saying "Word not found".
LIST=['apple','mango','grapes','banana','pear']
'mango' in LIST
True
LIST.index('grapes')
2
Ques 10: Write a python program to merge the following two lists into one:
list1=[1,2,3]
list2=[4,5,6]
list1+list2
[1, 2, 3, 4, 5, 6]