0% found this document useful (0 votes)
3 views14 pages

2marks Question Bank For CIA 2

The document provides an overview of various Python programming concepts, including operator precedence, string operators, membership operators, and the usage of the range keyword. It also covers control flow statements like if-else and while loops, along with examples of functions for calculating GCD, checking prime numbers, and reversing strings. Additionally, it discusses list mutability, variable naming rules, and the differences between lists and tuples.

Uploaded by

priyaa16svv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

2marks Question Bank For CIA 2

The document provides an overview of various Python programming concepts, including operator precedence, string operators, membership operators, and the usage of the range keyword. It also covers control flow statements like if-else and while loops, along with examples of functions for calculating GCD, checking prime numbers, and reversing strings. Additionally, it discusses list mutability, variable naming rules, and the differences between lists and tuples.

Uploaded by

priyaa16svv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

List the precedence / order of operators

An expression in python consists of variables, operators, values, etc. When the


Python interpreter encounters any expression containing several operations, all
operators get evaluated according to an ordered hierarchy, called operator
precedence.

2. What are the string operators?

String is defined as sequence of characters represented in quotation


marks (either single quotes ( ‘ ) or double quotes ( “ ).
An individual character in a string is accessed using a index.
The index should always be an integer (positive or negative).
A index starts from 0 to n-1.
Strings are immutable i.e. the contents of the string cannot be changed after
it is created.
 Operations on string:
 1. Indexing
 2. Slicing
 3. Concatenation
 4. Repetitions
 5. Member ship

3. Give the characteristics of membership operator.

These operators are used to test whether a value or operand is there in


the sequence such as list, string, set, or dictionary. There are two
membership operators in python: in and not in. In the dictionary we
can use to find the presence of the key, not the value.

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

4. Mention the usage of range keyword in python.

The range() function returns a sequence of numbers, starting from 0 by


default, and increments by 1 (by default), and stops before a specified number.
Syntax: range(start, stop, step)

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

5. Write the syntax for if-else and Nested- if statements.

• conditional (if) is used to test a condition, if the condition is true the


statements inside if will be executed.
syntax:
if(condition 1):
Statement 1
 In conditional if Statement the additional block of code is merged as else
statement which is performed when if condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false

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}')

7. Examine the purpose of pass and continue statement.


continue pass

The continue statement is used to reject


Pass Statement is used
1 the remaining statements in the current
when a statement is
. iteration of the loop and moves the
required syntactically.
control back to the start of the loop.

When we execute the


2 It returns the control to the beginning of
pass statements then
. the loop.
nothing happens.

3 It can be used with while loop and for


It is a null Operation.
. loop.

4 Its syntax is -: Its syntax is -:


. Continue pass

The pass statement is


5 It is mainly used inside a condition in a
discarded during the
. loop.
byte-compile phase

8. Write a few methods that are used in Python Lists

Methods used in lists are used to manipulate the data quickly.

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

2 copy() It returns a shallow copy of a list

3 clear() This method is used for removing all items from the list.

4 count() These methods count the elements

extend(
5 Adds each element of the iterable to the end of the List
)

6 index() Returns the lowest index where the element appears.

7 insert() Inserts a given element at a given index in a 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.
()

11 sort() Sort a List in ascending, descending, or user-defined order

12 min() Calculates the minimum of all the elements of the List

13 max() Calculates the maximum of all the elements of the List


9. Illustrate negative indexing in list with an example

The process of indexing from the opposite end is called Negative Indexing. In
negative Indexing, the last element is represented by -1.

Example

my_list = [45, 5, 33, 1, -9, 8, 76]

1.print(my_list[-1])
2.print(my_list[-2])
3.print(my_list[-3])

Output:
1 76
2 8
3 -9

10. Write a python program to print the GCD of 2 numbers.

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"

12. What are the operators does python support?


Arithmetic Operators
o Comparison (Relational) Operators

o Assignment Operator

o Logical Operators

o Bitwise Operators

o Membership Operators

o Identity Operator
13. What is the difference between list and tuple.

Sr. Key List Tuple


No.

1 Type List is mutable. Tuple is immutable.

Iteration List iteration is slower and Tuple iteration is faster.


2
is time consuming.

Appropriate List is useful for insertion Tuple is useful for readonly


3 for and deletion operations. operations like accessing
elements.

Memory List consumes more Tuples consumes less memory.


4 Consumptio memory.
n

Methods List provides many in-built Tuples have less in-built


5
methods. methods.

Error prone List operations are more Tuples operations are safe.
6
error prone.

14. What is chained conditional statements?

Chained conditionals (if-elif-else)

• The elif is short for else if.


• This is used to check more than one condition.
• If the condition1 is False, it checks the condition2 of the elif block.
If all the conditions are False, then the else part is executed

Syntax:
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3

15. Write the syntax and usage of while loop.

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

16. Write a python program to accept two numbers, find the


greatest and print the result

greatest of two numbers

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.

Rules for Python variables


 A Python variable name must start with a letter or the underscore
character.
 A Python variable name cannot start with a number.
 A Python variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ ).
 Variable in Python names are case-sensitive (name, Name, and NAME
are three different variables).
 The reserved words(keywords) in Python cannot be used to name the
variable in Python.
.
19. Write a program to check given year is leap year or not.

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

20. Name the two types of iteration statement support by python.

Iteration (or) Looping Statement

An Iterative statement allows us to execute a statement or group of


statement multiple times. Repeated execution of a set of statements is called
iteration or looping.
Types of Iterative Statement

1. while loop

2. for loop

Syntax of the for loop is:

for value in sequence:

{ code block }

Syntax of the while loop is:

while <condition>:
{ code block }

21. Differentiate pass and comment statement

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.

22. Write a python program to reverse the string.


def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "python"
print("The original string is : ", end="")
print(s)
print("The reversed string(using loops) is : ", end="")
print(reverse(s))
23. Explain local and global scope (or) comment with an example
on the use of local and global variable with the same identifier
name.
The scope of a variable refers to the places that we can see or
access a variable. If we define a variable on the top of the script or
module, the variable is called global variable. The variables that are
defined inside a class or function is called local variable.
Example:
The function will print the local x, and then the code will print the
global x:

x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)

output:

200
300

24. Define cloning in list with example.


•Cloning creates a new list with same values under another name. Taking
any slice of list create
new list.
•Any change made with one object will not affect others. The easiest way to
clone a new list is to use "slice operators"
a = [5,10,50,100]
b= a[ : ]
b[0] = 80
Print (" original list", a) = [5,10,50,100]
Print (" cloning list", b) = [5,10,50,100]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy