0% found this document useful (0 votes)
17 views4 pages

Python_PYQ_Assignment_Full_Solutions (1)

Uploaded by

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

Python_PYQ_Assignment_Full_Solutions (1)

Uploaded by

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

Comprehensive Solutions for Python PYQ Assignment

Q1. Answer any 5 questions. Each question carries 3 marks.

(a) Key features of Python programming language:

- Easy to learn and use

- Interpreted language

- Dynamically typed

- Extensive standard library

- Supports both object-oriented and functional programming

- Platform-independent

- Supports GUI applications

(b) Dynamic typing in Python:

In Python, variable types are determined at runtime, allowing flexibility. A variable can hold different

types of data at different times.

(c) Difference between indexing and slicing:

- Indexing retrieves a single item (e.g., list[0]).

- Slicing retrieves a subset of items (e.g., list[1:4]).

(d) Difference between sort() and sorted():

- sort(): Sorts the list in place and returns None.

- sorted(): Returns a new sorted list without modifying the original.

(e) Difference between copy() and deepcopy():

- copy(): Creates a shallow copy of an object.

- deepcopy(): Creates a deep copy, recursively copying all elements.

(f) Interactive mode vs script mode:

- Interactive mode: Immediate execution of statements in a Python shell.

- Script mode: Code is saved in a file and run as a script.

(g) Writing a multiline statement:

Use '\' to continue a statement on the next line.


Example:

total = 1 + 2 + 3 + \

4+5

Q2. (a) Explanation of list methods with examples:

- append(): Adds an element at the end of a list.

Example: list.append(5) # Adds 5 to the list

- extend(): Adds elements from another list to the end.

- clear(): Removes all items from the list.

- count(): Counts occurrences of a specified element.

- reverse(): Reverses the list in place.

(b) List comprehension and program example:

List comprehension is a concise way to create lists.

Example:

numbers = [1, 2, 3, 4, 5]

even_numbers = [x for x in numbers if x % 2 == 0]

Result: even_numbers = [2, 4]

(c) Difference between any() and all():

- any(): Returns True if any element is true.

- all(): Returns True only if all elements are true.

Example:

list = [True, False, True]

any(list) -> True, all(list) -> False

Q3. (a) Program to swap list halves based on sum:

numbers = [100, 200, 300, 40, 50, 60]

half = len(numbers) // 2

first_half, second_half = numbers[:half], numbers[half:]

if sum(first_half) > sum(second_half):


numbers = second_half + first_half

print(numbers) # Output: [40, 50, 60, 100, 200, 300]

(b) Explanation and code for continue, pass, and break:

- continue: Skips to the next iteration of the loop.

- pass: Does nothing, acts as a placeholder.

- break: Exits the loop entirely.

Example:

for i in range(5):

if i == 3:

continue

print(i)

(c) Program to sum even numbers until 0 is entered:

total = 0

while True:

num = int(input("Enter a number: "))

if num == 0:

break

if num % 2 == 0:

total += num

print("Sum of even numbers:", total)

Q4. Explanation of join() and split(), and immutability:

- join(): Combines list elements into a string.

- split(): Splits a string into a list.

Strings are immutable, meaning they cannot be altered once created.

(b) Palindrome checker program:

def is_palindrome(s):

return s == s[::-1]
string = input("Enter a string: ")

if is_palindrome(string):

print("The string is a palindrome.")

else:

print("The string is not a palindrome.")

(c) Difference between isalnum(), isalpha(), isascii():

- isalnum(): Checks if all characters are alphanumeric.

- isalpha(): Checks if all characters are alphabetic.

- isascii(): Checks if all characters are ASCII.

Q5. Explanation of Tuple, zip() and dictionary operations:

- Tuples are immutable, ordered collections used to store fixed data.

- zip(): Combines two iterables element-wise.

Example:

list1 = [1, 2]

list2 = ['a', 'b']

list(zip(list1, list2)) -> [(1, 'a'), (2, 'b')]

Dictionary merging and difference between del and clear():

- To merge two dictionaries, use dict.update() or {**dict1, **dict2}.

- del removes a specific key; clear() empties the entire dictionary.

You might also like