All Question paper with solution mean Bachelorexam.com

Unit3-FUNCTION AND STRINGS | Python programming Important Question with solution

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: 

  1. Python represents all its data as objects. Mutability of object is determined by its type.
  2. Some of these objects like lists and dictionaries are mutable, meaning we can change their content without changing their identity.
  3. Other objects like integers, floats, strings and tuples are immutable, meaning we cannot change their contents.
  4. Dictionaries are mutable in Python :
    1. Dictionaries in Python are mutable.
    2. The values in a dictionary can be changed, added or deleted.
    3. 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:

  1. 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.
  2. Strings are immutable:
    1. String are immutable which means that we cannot change any element of a string.
    2. If we want to change an element of a string, we have to create a new string.
    3. For example:
    4. 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.

  1. Lists are mutable :
    1. Lists are mutable means that the value of any element inside the list can be changed at any point of time.
    2. The elements of the list are accessible with their index value.
    3. The index always starts with 0 and ends with n – 1, if the list contains n elements.
    4. 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.
    5. 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 : 



  1. List comprehension is used to create a new list from existing sequences. 
  2. It is a tool for transforming a given list into another list.
  3. Using list comprehension, we can replace the loop with a single expression that produces the same result.
  4. The syntax of list comprehension is based on set builder notation in mathematics.
  5. 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
    1. [<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”.
  6. In the given example, the output for both without list comprehension and using list comprehension is the same.
  7. The use of list comprehension requires lesser code and also runs faster. 
  8. From above example we can say that list comprehension contains:
    1. An input sequence
    2. A variable referencing the input sequence
    3. An optional expression
    4. 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.

  1. Reduce(), filter(), map() are higher order functions used in Python. 
  2. Lambda definition does not include a “return” statement, it always contains an expression which is returned.
  3. 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.
  4. Lambda functions can be used along with built-in higher order functions like filter(), map() and reduce().

Use of lambda with filter():

  1. The filter() function in Python takes in a function and a list as arguments. 
  2. This function helps to filter out all the elements of a sequence “sequence”, for which the function returns true.
    1. 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.

  1. Lambda expressions is used to create the anonymous function.
  2. The anonymous functions are the functions created using a lambda keyword.
  3. They are not defined by using the def keyword. For this reason, they are called anonymous functions.
  4. We can pass any number of arguments to a lambda form functions, but still they return only one value in the form of expression.
  5. An anonymous function cannot directly call print command as the lambda needs an expression.
  6. It cannot access the parameters that are not defined in its own namespace.
  7. An anonymous function is a single line statement function.
  8. Syntax :
  9. 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


bachelor exam preparation all question paper with solution important questions with solution

Python Programming Important Links:

LabelLink
Subject SyllabusSyllabus
Short QuestionsShort-question
Important Unit-1Unit-1
Important Unit-2Unit-2
Important Unit-3Unit-3
Important Unit-4Unit-4
Important Unit-5Unit-5
Question paper – 2021-222021-22

AKTU Important Links | Btech Syllabus

Link NameLinks
Btech AKTU CircularsLinks
Btech AKTU SyllabusLinks
Btech AKTU Student DashboardStudent Dashboard
AKTU RESULT (One VIew)Student Result

Important Links-Btech (AKTU) | Python Programming syllabus

LabelLinks
Btech InformationInfo Link
Btech CSECSE-LINK
Quantum-PageLink
Python Programming SyllabusPython Programming Syllabus

5 thoughts on “Unit3-FUNCTION AND STRINGS | Python programming Important Question with solution”

Leave a Comment