0% found this document useful (0 votes)
6 views5 pages

Python Retest

Uploaded by

pushpa latha
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)
6 views5 pages

Python Retest

Uploaded by

pushpa latha
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/ 5

SECOND SEMESTER – B.

E Electrical and Electronics Engineering


23CSI201 – Problem Solving using Python Programming

CCET-I (RETEST) ANSWER KEY

PART – A (6 x 2 = 12 marks)

1. Describe about python interpreter and Interactive mode.


 Python interpreter: To execute a program in a high-level language by translating it one line
at a time.
 Interactive mode: Interactive Mode, as the name suggests, allows us to interact with OS.
When we type Python statement, interpreter displays the result(s) immediately.
2. Define Keywords.
Python Keywords are some predefined and reserved words in Python that have special
meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used
as an identifier, function, or variable name.
3. Classify the Literals.
Literals in Python is defined as the raw data assigned to variables or constants while
programming. We mainly have five types of literals which includes string literals, numeric
literals, boolean literals, literal collections and a special literal None.
4. Differentiate the Built-in and user defined functions.
Built-in Functions: These functions are built into the Python language and can be used
without the need for additional code. Some examples of built-in functions are print(), len(),
sum(), min(), max(), etc.
User-defined Functions: You create these functions to perform a specific task. You can
define your functions using the def keyword followed by the function name, parameter(s),
and the code block that performs the desired operation.
5. Compare the scope of local and global variables.
Local Variable:
 Variables that are declared within or inside a function block are known as Local variables.
 These variables can only be accessed within the function in which they are declared.
Global Variable:
 Global variables can be accessed by all the functions present in the program.
 Only a single declaration is required.
 Very useful if all the functions are accessing the same data.

6. Illustrate the chained conditional statements.


x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x and y are equal")

Output:
Enter a number: 27
Enter another number: 35
x is less than y

PART – B (1 x 16 = 16 Marks)

7. Write a python program to rotate the list [23, 12, 5, 24, 23, 76, 86, 24, 86,24, 75] by two positions
to the right.
my_list = [23, 12, 5, 24, 23, 76, 86, 24, 86, 24, 75]
# Rotate the list by two positions to the right
rotated_list = my_list[-2:] + my_list[:-2]
print("Original list:", my_list)
print("Rotated list:", rotated_list)

Output:
Original list: [23, 12, 5, 24, 23, 76, 86, 24, 86, 24, 75]
Rotated list: [86, 24, 23, 12, 5, 24, 23, 76, 86, 24, 86]

PART – C (2 x 16 = 32 Marks)

8. Discuss briefly about the usage of operators in python with examples.


Operators in Python are special symbols or keywords that perform operations on values
and variables. They are used to build expressions that manipulate data and execute
computations. Here are the main categories of operators in Python:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

Arithmetic Operators: Used to perform mathematical operations.


- + (Addition): Adds two operands (a + b)
- - (Subtraction): Subtracts the second operand from the first (a - b)
- * (Multiplication): Multiplies two operands (a * b)
- / (Division): Divides the first operand by the second (a / b)
- % (Modulus): Returns the remainder of a division (a % b)
- ** (Exponentiation): Raises the first operand to the power of the second (a ** b)
- // (Floor Division): Divides and returns the integer value of the quotient (a // b)
Comparison Operators: Used to compare two values.
- == (Equal): Returns True if both operands are equal (a == b)
- != (Not Equal): Returns True if operands are not equal (a != b)
- > (Greater than): Returns True if the left operand is greater than the right (a > b)
- < (Less than): Returns True if the left operand is less than the right (a < b)
- >= (Greater than or equal to): Returns True if the left operand is greater than or equal
to the right (a >= b)
- <= (Less than or equal to): Returns True if the left operand is less than or equal to the
right (a <= b)

Logical Operators: Usedto combine conditional statements.


- and: Returns True if both statements are true (a and b)
- or: Returns True if at least one statement is true (a or b)
- not: Reverses the logical state of its operand (not a)

Bitwise Operators: Operate on binary numbers.


- & (AND): Sets each bit to 1 if both bits are 1 (a & b)
- | (OR): Sets each bit to 1 if one of two bits is 1 (a | b)
- ^ (XOR): Sets each bit to 1 if only one of two bits is 1 (a ^ b)
- ~ (NOT): Inverts all the bits (~a)
- << (Zero fill left shift): Shifts left by pushing zeros in from the right (a << b)
- >> (Signed right shift): Shifts right by pushing copies of the leftmost bit in from the
left (a >> b)

Assignment Operators: Used to assign values to variables.


- =: Assigns the value of the right operand to the left operand (a = b)
- +=: Adds the right operand to the left operand and assigns the result to the left
operand (a += b)
- -=: Subtracts the right operand from the left operand and assigns the result to the left
operand (a -= b)
- *=: Multiplies the right operand with the left operand and assigns the result to the left
operand (a *= b)
- /=: Divides the left operand by the right operand and assigns the result to the left
operand (a /= b)
- %=: Takes modulus using two operands and assigns the result to the left operand (a
%= b)
- **=: Performs exponential calculation and assigns the result to the left operand (a **=
b)
- //=: Performs floor division and assigns the result to the left operand (a //= b)

Membership Operators: Used to test if a sequence is present in an object.


- in: Returns True if a sequence with the specified value is present in the object (a in b)
- not in: Returns True if a sequence with the specified value is not present in the object
(a not in b)

Identity Operators: Used to compare the memory locations of two objects.


- is: Returns True if both variables are the same object (a is b)
- is not: Returns True if both variables are not the same object (a is not b)

These operators are fundamental in Python and are used to build expressions and control
the logic flow in programs.

9.
Write a python program to extract the individual list of odd and even numbers from any given
list.
def separate_odd_even(lst):
odd_numbers = [num for num in lst if num % 2 != 0]
even_numbers = [num for num in lst if num % 2 == 0]
return odd_numbers, even_numbers
numbers = [23, 12, 5, 24, 23, 76, 86, 24, 86, 24, 75]
odd, even = separate_odd_even(numbers)

print("Original list:", numbers)


print("Odd numbers:", odd)
print("Even numbers:", even)

Output:
Original list: [23, 12, 5, 24, 23, 76, 86, 24, 86, 24, 75]
Odd numbers: [23, 5, 23, 75]
Even numbers: [12, 24, 24, 24, 76, 86, 86]

10. Explain in detail about the data types in python with suitable example..

Numeric Data Types: Numeric data types in Python are used to represent numerical
values. Python provides three primary numeric data types:
 Integer (int): Integers are whole numbers without any decimal points. They can
be positive or negative.
 Floating-Point (float): Floating-point numbers represent decimal values. They
can be positive or negative and may contain a decimal point.
 Complex (complex): Complex numbers are used to represent numbers with a
real and imaginary part. They are written in the form of a + bj, where a is the
real part and b is the imaginary part.

String Data Type(str): Represents a sequence of characters enclosed in single quotes


(‘ ‘) or double quotes (” “), such as “Hello, World!”, ‘Python’.

Boolean Data Type(bool): Represents either True or False, used for logical
operations and conditions.

Sequences Data Types:


 list: Represents an ordered and mutable collection of items, enclosed in square
brackets ([]).
 tuple: Represents an ordered and immutable collection of items, enclosed in
parentheses ().
 dict: Represents a collection of key-value pairs enclosed in curly braces ({})
with unique keys.
 set: Represents an unordered and mutable collection of unique elements,
enclosed in curly braces ({}) or using the set() function.

Course Handling faculty Course Co-Ordinator

You might also like