Python More Built-in Functions

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

num = -4
abs_value = abs(num)
print(abs_value)
PYTHON

Output

4

Example 2:

Code

print(abs(5))
print(abs(-2.90))
print(abs(3 - 5))
PYTHON

Output

5
2.9
2

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

list_a = [True, True]
is_all_true = all(list_a)
print(is_all_true)
PYTHON

Output

True

Example 2:

Code

set_a = {}
is_all_true = all(set_a)
print(is_all_true)
 
PYTHON

Output

True

Example 3:

Code

is_true_in_list = all([1, 5, 0, 4])
is_true_in_set = all({True, "Teja", 7})
is_true_in_tuple = all(("", "hello", "world"))
print(is_true_in_list)
print(is_true_in_set)
print(is_true_in_tuple)
 
PYTHON

Output

False
True
False

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

dict_a = {
"name": "Teja",
"age": 15
}
is_all_true = all(dict_a)
print(is_all_true)
 
PYTHON

Output

True

Example 5:

Code

dict_a = {
0: "Hello",
1: "World"
}
is_all_true = all(dict_a)
print(is_all_true)
 
PYTHON

Output

False

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

list_a = [True, False]
is_any_true = any(list_a)
print(is_any_true)
 
PYTHON

Output

True

Example 2:

Code

set_a = {}
is_any_true = any(set_a)
print(is_any_true)
 
PYTHON

Output

False

Example 3:

Code

is_true_in_list = any([0, 0, 2, 0])
is_true_in_set = any({"", None, 0})
is_true_in_tuple = any(("hello", "world"))
print(is_true_in_list)
print(is_true_in_set)
print(is_true_in_tuple)
 
PYTHON

Output

True
False
True

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

dict_a = {
0: "Teja",
1: 15
}
is_any_true = any(dict_a)
print(is_any_true)
 
PYTHON

Output

True

Example 5:

Code

dict_a = {
0: "hello",
False: "world"
}
is_any_true = any(dict_a)
print(is_any_true)
 
PYTHON

Output

False

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

name = "Teja"
reversed_name = reversed(name)
print(list(reversed_name))
 
PYTHON

Output

['a', 'j', 'e', 'T']

Example 2:

Code

tuple_a = (1, 2, 3, 4)
list_a = ["A", "B", "C", "D"]
reversed_tuple = list(reversed(tuple_a))
reversed_list = list(reversed(list_a))
print(reversed_tuple)
print(reversed_list)
 
PYTHON

Output

[4, 3, 2, 1]
['D', 'C', 'B', 'A']

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 is 
    0
    .

The

enumerate()
function adds a counter to each item in a sequence and returns a sequence containing tuples.

Example 1:

Code

name = "Teja"
enumerate_name = enumerate(name)
print(list(enumerate_name))
 
PYTHON

Output

[(0, 'T'), (1, 'e'), (2, 'j'), (3, 'a')]

Example 2:

Code

set_a = {1, 2, 3, 4}
enumerate_set = list(enumerate(set_a, 10))
print(enumerate_set)
 
PYTHON

Output

[(10, 1), (11, 2), (12, 3), (13, 4)]

Looping Over an Enumerate

We can use the

for
loop to iterate over an enumerate.

Code

names = ["Jack", "John", "James"]
for each_name in enumerate(names):
print(each_name)
 
PYTHON

Output

(0, 'Jack')
(1, 'John')
(2, 'James')

We can unpack each tuple returned by the enumerate.

Code

names = ["Jack", "John", "James"]
for count, each_name in enumerate(names):
tuple_a = (count, each_name)
print(tuple_a)
 
PYTHON

Output

(0, 'Jack')
(1, 'John')
(2, 'James')

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

list_a = [1, 2, 3, 4]
list_b = list_a.copy()
print(list_b)
 
PYTHON

Output

[1, 2, 3, 4]

Example 2:

Code

list_a = ["a", "b", "c"]
list_b = list_a
list_a[0] = "z"
print(list_b)
list_1 = ["a", "b", "c"]
list_2 = list_1.copy()
list_1[0] = "z"
print(list_2)
 
PYTHON

Output

['z', 'b', 'c']
['a', 'b', 'c']

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

list_a = ["a", "b", "c"]
list_b = [1, 2, 3, list_a]
list_c = list_b.copy()
list_a[0] = "z"
print(list_c)
 
PYTHON

Output

[1, 2, 3, ['z', 'b', 'c']]

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

list_a = [1, 2, 3, 4]
list_a.reverse()
print(list_a)
PYTHON

Output

[4, 3, 2, 1]

Example 2:

Code

list_a = ["apple", "mango", "banana"]
list_a.reverse()
print(list_a)
 
PYTHON

Output

['banana', 'mango', 'apple']

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form