Concepts
1. Built-in Functions
1.1 Abs
Syntax:
abs(number)
The
abs()
function gives the absolute value (positive representation) of a number. For example, the absolute value of -3
is 3
.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
1.2 All
Syntax:
all(sequence)
The
all()
function returns True
if all the items in the sequence are true (or if the sequence is empty). Otherwise, it returns False
.For each item in a sequence, the
all()
function evaluates to false, for which the bool()
function returns False
. For example, 0
, None
, False
, etc.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
Example 3:
Code
PYTHON
Output
In the case of dictionaries, the
all()
function returns True
if all the keys in the dictionary are true. Otherwise, it returns False
.Example 4:
Code
PYTHON
Output
Example 5:
Code
PYTHON
Output
1.3 Any
Syntax:
any(sequence)
The
any()
function returns True
if any of the items in the sequence is true. Otherwise, it returns False
.If the sequence is empty, it returns
False
.For each item in a sequence, the
any()
function evaluates to false, for which the bool()
function returns False
. For example, 0
, None
, False
, etc.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
Example 3:
Code
PYTHON
Output
In the case of dictionaries, the
any()
function returns True
if any of the keys in the dictionary is true. Otherwise, it returns False
.Example 4:
Code
PYTHON
Output
Example 5:
Code
PYTHON
Output
1.4 Reversed
Syntax:
reversed(sequence)
Here, the
sequence
does not include sets and dictionaries as they are unordered collections of items.The
reversed()
function returns the reverse of a sequence.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
1.5 Enumerate
Syntax:
enumerate(sequence, start)
Here,
- sequence: is any sequence like a string, list, set, etc.
- start(Optional): it indicates the start point of the counter. Its default value is0.
The
enumerate()
function adds a counter to each item in a sequence and returns a sequence containing tuples.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
Looping Over an Enumerate
We can use the
for
loop to iterate over an enumerate.Code
PYTHON
Output
We can unpack each tuple returned by the enumerate.
Code
PYTHON
Output
2. List Methods
2.1 Copy
Syntax:
list.copy()
The
copy()
method returns a copy of the specified list. After copying, any changes made to the original list do not affect the items in the copied list.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON
Output
In the above code, both the variables
list_a
and list_b
will be referring to the same object. So, when an item in list_a
is updated it affected list_b
also.Whereas, when we copy
list_1
using the copy()
method, the variables list_1
and list_2
will refer to different objects. So, when an item in list_1
is updated it does not affect list_2
.However, updating mutable objects will affect the values in the copied list also, as the reference is changed.
Example 3:
Code
PYTHON
Output
2.2 Reverse
Syntax:
list.reverse()
The
reverse()
method reverses the items of the list. It doesn't return any value but updates the existing list.Example 1:
Code
PYTHON
Output
Example 2:
Code
PYTHON