0% found this document useful (0 votes)
13 views

Assignment 2.ipynb - Colab

Uploaded by

yadavkirti2936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 2.ipynb - Colab

Uploaded by

yadavkirti2936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ques1 : What are the differnt data types available in python? Give an example of each type.

Answer: There are 4


classifications of data types:

1. Numeric data type


2. Boolean
3. Sequence and
4. Collection and futher they are divided into subparts as:
5. NUMERIC DATA TYPE : a) Integer: eg- a=7 b) Complex: eg- p=10+5j c) float: eg- x=9.6
6. BOOLEAN DATA TYPE : eg- 9(Decimal form ), 0b111(Binary form ), 0a763(Octal form), 0xface(Hexa-Decimal
form)
7. SEQUENCE DATA TYPE : a) String: eg- a="7" b) List: eg- p=[1,4,8] c) Tuple: eg- x=(1,4,8)
8. COLLECTION DATA TYPE : a) Set: eg- a={'neha',1,'raj',4,'khushi',8} b) Dictionary: eg- p={'neha':1 ,'raj':4 ,'khushi':8}

Ques2: Given the variables below, identify their data type:

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

Ques4: Convert a=0 to a boolean.

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]

Ques6: Write a python program to create a list of numbers and:

a) Append a new number to the end of the lest

X=[1,2,3,4,5]
X.append(6)
print(X)
[1, 2, 3, 4, 5, 6]

b) Insert a number at the second position of the list.

X.insert(1,7)
print(X)
[1, 7, 2, 3, 4, 5, 6]

c) Remove the last number from the list

X.pop()
print(X)
[1, 7, 2, 3, 4, 5]

d) Sort the list in ascending order.

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.

Answer: In Python, the operators = and == serve very different purposes.

'=' 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.

eg: x = 5 (Here, 5 is assigned to variable x)

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)

Output will be true as both x and y are equal.

Ques9: Write a python program to:

a) Create a list of 5 random words.

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]

You might also like