[go: up one dir, main page]

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

Temp

The document discusses different types of objects in Python - mutable and immutable objects. It provides examples of immutable objects like integers, floats, strings, tuples; and mutable objects like lists, dictionaries, sets. It also summarizes that immutable objects are faster to access but expensive to change, while mutable objects are easy to change but slower than immutable objects. The document recommends using mutable objects when there is a need to change the size or content of an object.

Uploaded by

ARIFULLA SHAIK
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)
32 views5 pages

Temp

The document discusses different types of objects in Python - mutable and immutable objects. It provides examples of immutable objects like integers, floats, strings, tuples; and mutable objects like lists, dictionaries, sets. It also summarizes that immutable objects are faster to access but expensive to change, while mutable objects are easy to change but slower than immutable objects. The document recommends using mutable objects when there is a need to change the size or content of an object.

Uploaded by

ARIFULLA SHAIK
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/ 5

ANSWERS

2. Every variable in python holds an instance of an object. There are two


types of objects in python i.e., Mutable and Immutable objects. Whenever
an object is instantiated, it is assigned a unique object id.
To summarise the difference, mutable objects can change their state or
contents and immutable objects can’t change their state or content.
• Immutable Objects : These are of in-built types like int, float, bool,
string, unicode, tuple. In simple words, an immutable object can’t be
changed after it is created.
Mutable Objects : These are of type list, dict, set . Custom classes
are generally mutable.

• Mutable and immutable objects are handled differently in python.


Immutable objects are quicker to access and are expensive to change
because it involves the creation of a copy. Whereas mutable objects
are easy to change.

• Use of mutable objects is recommended when there is a need to


change the size or content of the object.
3. Dictionary in Python is an unordered collection of data values, used to
store data values like a map, which, unlike other Data Types that hold only
a single value as an element, Dictionary holds key : value pair. Key-value
is provided in the dictionary to make it more optimized. Keys in a dictionary
don’t allow Polymorphism.
Methods Description (ANY 5)
copy() They copy() method returns a shallow copy of the dictionary.
clear() The clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictionary having the
given key.
popitem() Removes the arbitrary key-value pair from the dictionary and
returns it as tuple.
get() It is a conventional method to access a value for a key.
str() Produces a printable string representation of a dictionary.
update() Adds dictionary dict2’s key-values pairs to dict
setdefault() Set dict[key]=default if key is not already in dict
keys() Returns list of dictionary dict’s keys
items() Returns a list of dict’s (key, value) tuple pairs

4. Operators are used to perform operations on variables and values. These


are the special symbols that carry out arithmetic and logical computations.
The value the operator operates on is known as Operand.
There are many classifications of operators, a couple of them being bitwise
and logical operators.
Bitwise: In Python, bitwise operators are used to performing bitwise
calculations on integers. The integers are first converted into binary and
then operations are performed on bit by bit, hence the name bitwise
operators. Then the result is returned in decimal format.
Bitwise AND operator: Returns 1 if both the bits are 1 else 0.
Bitwise or operator: Returns 1 if either of the bit is 1 else 0.
Bitwise not operator: Returns one’s complement of the number.
Bitwise xor operator: Returns 1 if one of the bits is 1 and the other is 0 else
returns false.
Logical: In Python, Logical operators are used on conditional statements
(either True or False). They perform Logical AND, Logical OR and Logical
NOT operations.
Logical AND operator: Logical operator returns True if both the operands
are True else it returns False.
Logical OR operator : Logical or operator returns True if either of the
operands is True.
Logical not operator : Logical not operator work with the single boolean
value. If the boolean value is True it returns False and vice-versa.

5. Python Lambda Functions are anonymous function means that the


function is without a name. As we already know that the def keyword is
used to define a normal function in Python. Similarly, the lambda keyword
is used to define an anonymous function in Python.
Python Lambda Function Syntax:
lambda arguments: expression
ADVANTAGES:
• We use lambda functions when we require a nameless function for a
short period of time.
• In Python, we generally use it as an argument to a higher-order
function (a function that takes in other functions as arguments).
• This function can have any number of arguments but only one
expression, which is evaluated and returned.
• One is free to use lambda functions wherever function objects are
required.

filter():
The filter() function in Python takes in a function and a list as arguments.
This offers an elegant way to filter out all the elements of a sequence
“sequence”, for which the function returns True. Here is a small program
that returns the odd numbers from an input list:
Example 1:
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]

final_list = list(filter(lambda x: (x%2 != 0) , li))


print(final_list)

map():
The map() function in Python takes in a function and a list as an
argument. The function is called with a lambda function and a list and a
new list is returned which contains all the lambda modified items
returned by that function for each item. Example:
Example 1:
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]

final_list = list(map(lambda x: x*2, li))


print(final_list)

reduce():
The reduce() function in Python takes in a function and a list as an
argument. The function is called with a lambda function and an iterable
and a new reduced result is returned. This performs a repetitive
operation over the pairs of the iterable. The reduce() function belongs
to the functools module.
Example 1:
from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print (sum)

6. Parameters are arguments that are passed via a function attribute.


Formal parameters are mentioned in the function definition. Actual
parameters(arguments) are passed during a function call. We can define a
function with a variable number of arguments.
There are 5 types of parameters :

1. default arguments
2. keyword arguments
3. positional arguments
4. arbitrary positional arguments
5. arbitrary keyword arguments

The special syntax *args in function definitions in python is used to pass a


variable number of arguments to a function. It is used to pass a non-key
worded, variable-length argument list. For example : we want to make a
multiply function that takes any number of arguments and able to multiply
them all together. It can be done using *args.
def myFun(*argv):
for arg in argv:
print (arg)

myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')

The special syntax **kwargs in function definitions in python is used to


pass a keyworded, variable-length argument list. We use the name kwargs
with the double star. The reason is because the double star allows us to
pass through keyword arguments (and any number of them).
def myFun(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))

# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')

7. In Python identity operators are used to determine whether a value is of


a certain class or type. They are usually used to determine the type of data
a certain variable contains. There are different identity operators such as
• ‘is’ operator – Evaluates to true if the variables on either side of the
operator point to the same object and false otherwise.
• ‘is not’ operator – Evaluates to false if the variables on either side of the
operator point to the same object and true otherwise.

8. Membership operators are operators used to validate the membership


of a value. It tests for membership in a sequence, such as strings, lists, or
tuples.
• in operator : The ‘in’ operator is used to check if a value exists in a
sequence or not. Evaluate to true if it finds a variable in the specified
sequence and false otherwise.
• ‘not in’ operator : Evaluates to true if it does not finds a variable in the
specified sequence and false otherwise.
9. Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were created in
that scope are destroyed. Python supports the following control statements.
• Break statement
• Continue statement
• Pass statement
The break statement is used to terminate the loop or statement in which it is
present. After that, the control will pass to the statements that are present
after the break statement, if available.
The continue is also a loop control statement just like the break statement.
continue statement is opposite to that of break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests pass statement simply does nothing. The pass
statement in Python is used when a statement is required syntactically but
you do not want any command or code to execute. It is like null operation,
as nothing will happen is it is executed.

You might also like