2marks Question Bank For CIA 2
2marks Question Bank For CIA 2
in: The in operator returns True if the value is found in the sequence.
not in: The not in operator returns True if the value is not found in the
sequence
Parameter :
start: [ optional ] start value of the sequence
stop: next value after the end value of the sequence
step: [ optional ] integer value, denoting the difference
between any two numbers in the sequence
6. Write the Python Program to print the sum of cubes of first n natural
numbers
def sumOfCubes(n) :
if n < 0:
return
sum = 0
for i in range(n+1):
sum += pow(i, 3)
return sum
n = int(input('Enter n : '))
sum = sumOfCubes(n)
print(f'Sum : {sum}')
S.n Metho
o d Description
append
1 Used for appending and adding elements to the end of the List.
()
S.n Metho
o d Description
3 clear() This method is used for removing all items from the list.
extend(
5 Adds each element of the iterable to the end of the List
)
Removes and returns the last value from the List or the given
8 pop()
index value.
remove
9 Removes a given object from the List.
()
reverse
10 Reverses objects of the List in place.
()
The process of indexing from the opposite end is called Negative Indexing. In
negative Indexing, the last element is represented by -1.
Example
1.print(my_list[-1])
2.print(my_list[-2])
3.print(my_list[-3])
Output:
1 76
2 8
3 -9
n1=int(input("Enter a number1:"))
n2=int(input("Enter a number2:"))
for i in range(1,n1+1):
if(n1%i==0 and n2%i==0):
gcd=i
print(gcd)
output:
Enter a number1:8
Enter a number2:24
8
11. Write the program to check whether the given number is prime
or not.
num = 11
# If given number is greater than 1
if num > 1:
# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number"
o Assignment Operator
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operator
13. What is the difference between list and tuple.
Error prone List operations are more Tuples operations are safe.
6
error prone.
Syntax:
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
While Loop is used to execute number of statements or body till the condition
passed in while is true. Once the condition is false, the control will come out
of the loop.
Syntax:
while<expression>:
Body
a=eval(input("enter a value:"))
b=eval(input("enter b value:"))
if(a>b):
print("greatest:",a)
else:
print("greatest:",b)
Output
enter a value:4
enter b value:7
greatest: 7
17. What is List mutability in Python? Give an example.
Lists are mutable in Python. We can add or remove elements from the list.
In Python, mutability refers to the capability of an object to be changed or
modified after its creation.
example:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
my_list.insert(1, 5)
print(my_list)
my_list.remove(2)
print(my_list)
popped_element = my_list.pop(0)
print(my_list)
print(popped_element)
18. Define variable and write down the rules for naming a variable
Python Variable is containers that store values. A variable is a location in
memory used to store some data (value). The assignment operator (=) to
assign values to a variable.
y=eval(input("enter a year"))
if(y%4==0):
year print("leap year")
else:
print("not leap year")
output:
enter a year2000
leap year
1. while loop
2. for loop
{ code block }
while <condition>:
{ code block }
The Python pass statement is a null statement. But the difference between
pass and comment is that comment is ignored by the interpreter whereas
pass is not ignored.
x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)
output:
200
300