In this session, we are discussing Unit3-FUNCTION AND STRINGS | Python programming Important Question with solution Hope you enjoy that session and that will help in your upcoming exams.
Dudes 🤔.. You want more useful details regarding this subject. Please keep in mind this as well. Important Questions For Python Programming: *Unit-01 *Unit-02 *Unit-03 *Unit-04 *Unit-05 *Short-Q/Ans *Question-Paper with solution 21-22
Q1. Explain unpacking sequences, and mutable sequences, and list comprehension with examples. Write a program to sort the list of dictionaries by values in Python – Using the lambda function.
Ans.
Unpacking sequences:
- Unpacking allows us to extract the components of the sequence into individual variables.
- Several different assignments can be performed simultaneously.
- We have multiple assignments in Python where we can have multiple LHS assigned from corresponding values at the RHS. This is an example of unpacking sequences.
- There is one restriction, the LHS and RHS must have equal length. That is, every value that is created at RHS should be assigned to LHS.
- Strings and tuples are examples of sequences. Operations applicable on sequences are: Indexing, repetition, concatenation.
- Since strings are also sequences, we can also get individual characters in the string using unpacking operation.
Mutable sequences:
- Python represents all its data as objects. Mutability of object is determined by its type.
- Some of these objects like lists and dictionaries are mutable, meaning we can change their content without changing their identity.
- Other objects like integers, floats, strings and tuples are immutable, meaning we cannot change their contents.
- Dictionaries are mutable in Python :
- Dictionaries in Python are mutable.
- The values in a dictionary can be changed, added or deleted.
- If the key is present in the dictionary, then the associated value with that key is updated or changed; otherwise a new key: value pair is added.
For example:
- In the given example, we tried to reassign the value ’30’ to the key ‘age’, Python interpreter first searches the key in the dictionary and then updates it. Hence, the value of ‘age’ is updated to 30. However, in the next statement, it does not find the key ‘address’; hence, the key: value ‘address’: ‘Alaska’ is added to the dictionary.
- Strings are immutable:
- String are immutable which means that we cannot change any element of a string.
- If we want to change an element of a string, we have to create a new string.
- For example:
- Output:
Type error: ‘str’ object does not support item assignment
Here, we try to change the 0th index of the string to a character p, but the python interpreter generates an error.
Now, the solution to this problem is to generator a new string rather than change the old string.
In the given example, we cut the slice from the original string and concatenate it with the character we want to insert in the string. It does not have any effect on the original string.
- Lists are mutable :
- Lists are mutable means that the value of any element inside the list can be changed at any point of time.
- The elements of the list are accessible with their index value.
- The index always starts with 0 and ends with n – 1, if the list contains n elements.
- The syntax for accessing the elements of a list is the same as in the case of a string. We use square brackets around the variable and index number.
- For example:
In the given example, we access the 2nd element of the list that has 1 as index number and the interpreter prints 20.
Now, if we want to change a value in the list given in the example:
List comprehension :
- List comprehension is used to create a new list from existing sequences.
- It is a tool for transforming a given list into another list.
- Using list comprehension, we can replace the loop with a single expression that produces the same result.
- The syntax of list comprehension is based on set builder notation in mathematics.
- Set builder notation is a notation is a mathematical notation for describing a set by stating the property that its members should satisfy. The syntax is
- [<expression> for <element> in <sequence> if <conditional>]
The syntax is read as “Compute the expression for each element in the sequence, if the conditional is true”.
- [<expression> for <element> in <sequence> if <conditional>]
- In the given example, the output for both without list comprehension and using list comprehension is the same.
- The use of list comprehension requires lesser code and also runs faster.
- From above example we can say that list comprehension contains:
- An input sequence
- A variable referencing the input sequence
- An optional expression
- An output expression or output variable
Q2. Explain higher order function with respect to lambda expression. Write a Python code to count occurrences of an element in a list
Ans.
- Reduce(), filter(), map() are higher order functions used in Python.
- Lambda definition does not include a “return” statement, it always contains an expression which is returned.
- We can also put a lambda definition anywhere a function is expected, and we do not have to assign it to a variable at all.
- Lambda functions can be used along with built-in higher order functions like filter(), map() and reduce().
Use of lambda with filter():
- The filter() function in Python takes in a function and a list as arguments.
- This function helps to filter out all the elements of a sequence “sequence”, for which the function returns true.
- For example: Python program that returns the odd numbers from an input list:
- For example: Python program that returns the odd numbers from an input list:
Use of lambda() with reduce():
1. The reduce() function in Python takes in a function and a list as arguments.
2. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list.
3.This is a part of the functools module.
For example:
Here the results of previous two elements are added to the next element and this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).
Program to count occurrences of an element in a list :
Q3. Explain Lambda Expression.
Ans.
- Lambda expressions is used to create the anonymous function.
- The anonymous functions are the functions created using a lambda keyword.
- They are not defined by using the def keyword. For this reason, they are called anonymous functions.
- We can pass any number of arguments to a lambda form functions, but still they return only one value in the form of expression.
- An anonymous function cannot directly call print command as the lambda needs an expression.
- It cannot access the parameters that are not defined in its own namespace.
- An anonymous function is a single line statement function.
- Syntax :
- In the given example, the lambda function is defined with two arguments vall and val2. The expression val1*va12 does the multiplication of the two values. Now, in function call, we can directly call the mult function with two valid values as arguments and produce the output as shown in given example.
Q4. What do you mean by sets? Explain the operations performed on sets.
Ans.
- 1. In Python we also have one data type which is an unordered collection of data known as set.
- 2.A set does not contain any duplicate values or elements.
Operations performed on sets are:
- i.Union: Union operation performed on two sets returns all the elements from both the sets. It is performed by using and operator.
- ii. Intersection: Intersection operation performed on two sets returns all the elements which are common or in both the sets. It is performed by using ‘|’ operator.
- iii. Difference: Difference operation performed on two sets set1 and set2 returns the elements which are present on set1 but not in set2. It is performed by using the ‘-‘ operator.
- iv. Symmetric difference: Symmetric difference operation performed on two sets returns the element which are present in either set1 or set2 but not in both. It is performed by using an operator.
Q5. Define tuples. How are tuples created in Python?
Ans.
- 1. Tuples are the sequence or series values of different types separated by commas (,).
- 2. Values in tuples can also be accessed by their index values, which are integers starting from 0.
For example:
The names of the months in a year can be defined in a tuple:
>>> months = (‘January’, February’, March’, April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’)
Creating tuples in Python :
1. To create a tuple, all the items or elements are placed inside parentheses separated by commas and assigned to a variable.
2. Tuples can have any number of different data items (that is, integer, float, string, list, etc.).
For examples:
1. A tuple with integer data items:
>>> tuple = (4, 2, 9, 1)
>>> print tuple
(4, 2, 9, 1) # Output
2. A tuple with items of different data types:
>>>tuple_mix = (2, 30, “Python”, 5.8, “Program”)
>>>print tuple_mix
(2, 30, ‘Python’, 5.8, ‘Program’) # Output
3. Nested tuple:
>>>nested_tuple = (“Python”, [1, 4, 2], [“john”, 3.9])
>>> print nested_tuple
(‘Python’, [1, 4, 2], [‘john’, 3.9]) # Output
4. Tuple can also be created without parenthesis:
>>>tuple = 4.9, 6, ‘house’
>>>print tuple
(4.9, 6, ‘house’) # Output
All Question Paper | AKTU Quantum | solution of question paper
Python Programming Important Links:
Label | Link |
---|---|
Subject Syllabus | Syllabus |
Short Questions | Short-question |
Important Unit-1 | Unit-1 |
Important Unit-2 | Unit-2 |
Important Unit-3 | Unit-3 |
Important Unit-4 | Unit-4 |
Important Unit-5 | Unit-5 |
Question paper – 2021-22 | 2021-22 |
AKTU Important Links | Btech Syllabus
Link Name | Links |
---|---|
Btech AKTU Circulars | Links |
Btech AKTU Syllabus | Links |
Btech AKTU Student Dashboard | Student Dashboard |
AKTU RESULT (One VIew) | Student Result |
Important Links-Btech (AKTU) | Python Programming syllabus
Label | Links |
---|---|
Btech Information | Info Link |
Btech CSE | CSE-LINK |
Quantum-Page | Link |
Python Programming Syllabus | Python Programming Syllabus |
5 thoughts on “Unit3-FUNCTION AND STRINGS | Python programming Important Question with solution”