[go: up one dir, main page]

0% found this document useful (0 votes)
5 views11 pages

EC212 Assignment 1 Answers

The document provides a series of Python programming tasks, including displaying list elements using iterators, generating Fibonacci numbers with generators, and explaining shallow vs deep copy. It also covers creating a function for counting letter frequency, the use of else clauses in loops, and details on identity and membership operators. Additionally, it includes examples of working with complex numbers and performing operations on matrices.

Uploaded by

abhi7416122
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)
5 views11 pages

EC212 Assignment 1 Answers

The document provides a series of Python programming tasks, including displaying list elements using iterators, generating Fibonacci numbers with generators, and explaining shallow vs deep copy. It also covers creating a function for counting letter frequency, the use of else clauses in loops, and details on identity and membership operators. Additionally, it includes examples of working with complex numbers and performing operations on matrices.

Uploaded by

abhi7416122
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/ 11

1.Write a program to display list elements using iter() and next().

Displaying list elements using next():

Explanation:

 Here, first we created an iterator from the list using the iter() method. And
then used the next() function to retrieve the elements of the iterator in
sequential order.
 When we reach the end and there is no more data to be returned, we will get
the StopIteration Exception.

Displaying list elements using iter():


Explanation:

 In this example, the for loop iterates over the elements of the iterator object.
 On each iteration, the loop assigns the value of the next element to the
variable element, and then executes the indented code block.

2.write a python program to display fibbonacci numbers using generators.


Explanation:

In the above example,

 The fibonacci() function is defined as a generator function. It initializes two


variables x and y with 0 and 1, respectively.
 The function enters an infinite loop using while True.
 Inside the loop, it uses the yield keyword to yield the current value of x. This
represents the Fibonacci number in the sequence.
 After yielding a, the function updates x and y by swapping their values: x
becomes y, and y becomes the sum of the previous values of x and y.
 The loop continues, generating the next Fibonacci number each time yield is
encountered.
 To use this generator and print the Fibonacci sequence, iterate over it using a
loop. Use the next() function to manually retrieve the next Fibonacci
number.

3. Explain shallow copy and deep copy with the help of example.

The difference between shallow and deep copying is only relevant for compound
objects (objects that contain other objects, like lists or class instances):

1. A shallow copy constructs a new compound object and then (to the extent
possible) inserts references into it to the objects found in the original.
2. A deep copy constructs a new compound object and then, recursively, inserts
copies into it of the objects found in the original.

Two problems often exist with deep copy operations that don’t exist with shallow
copy operations:

 Recursive objects (compound objects that, directly or indirectly, contain a


reference to themselves) may cause a recursive loop.
 Because deep copy copies everything it may copy too much, such as data
which is intended to be shared between copies.
Examples:

1. Using id’s

2. Changing the contents of copied list


Deepcopy example

Shallow copy example

4. Write a Python program to create a function called most_frequent that


takes a string and prints the letters in descending order of their frequency
using dictionary.
5. what is the purpose else clause for a loop?explaIN how else works with
while and for loops with examples.

Else with loop is used with both while and for loop. The else block is executed at
the end of loop means when the given loop condition is false then the else block
is executed.

Else with While loop


Explanation
 declare i=0
 we know then while loop is active until the given condition is true. and we
check i<5 it’s true till the value of i is 4.
 i+=1 increment of i because we don’t want to execute the while loop infinite
times.
 print the value of i
 else block execute when the value of i is 5.

Else with for loop:

Explanation
 declare a list l=[1,2,3,4,5]
 for loop print a.
 else block is execute when the for loop is read last element of list.

6. What are the operators in Python. Describe specifically about identity


operator and membership operator.

Types of Operators in Python:

1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators and Membership Operators

Membership operator

Python offers two membership operators to check or validate the membership of a


value. It tests for membership in a sequence, such as strings, lists, or tuples.

i. ‘in’ operator - It is used to check if a character/ substring/ element exists in a


sequence or not. Evaluate to True if it finds the specified element in a sequence
otherwise False. For example,

Examples:

'R' in 'RVR and JC' # Checking 'R' in String, returns True

'A' in 'RVR and JC' #Checking 'A' in string since Python is case-sensitive, returns
False

ii. ‘not in’ operator- Evaluates to true if it does not finds a variable in the specified
sequence and false otherwise.

Examples:

'R' not in 'RVR and JC' # Checking 'R' in String, returns False

'A' not in 'RVR and JC' #Checking 'A' in string since Python is case-sensitive,
returns True

Identity operators

Identity operators are used to compare the objects if both the objects are actually of
the same data type and share the same memory location.

There are different identity operators such as

i. ‘is’ operator – Evaluates to True if the variables on either side of the operator
point to the same object and false otherwise.

ii. ‘is not’ operator: Evaluates True if both variables are not the same object.
Examples:

x = 10

y = 10

print(x is y) # True

print(x is not y) # False

7.write a python program to create a complex with necessary attributes and


perform addition, subtraction and multiplication on two complex numbers.

You can create a Python program to work with complex numbers and perform
addition, subtraction, and multiplication on them. Python's built-in support for
complex numbers makes this quite straightforward:
In this program:

 We define a MyComplex class to represent complex numbers. It has


attributes for the real and imaginary parts and methods for addition,
subtraction, and multiplication.
 The __str__ method is defined to provide a user-friendly representation of
complex numbers when printing.
 The add, subtract, and multiply methods perform the respective operations
and return a new MyComplex object with the result.
 We create two complex numbers (complex1 and complex2) and then
perform addition, subtraction, and multiplication on them.
 Finally, we display the results.
8. write a python program to create a matrix and perform addition,
subtraction and multiplication on two matrices.

You might also like