Useful Functions in python

Useful functions:

1.len():

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Example:

Return the number of characters in a string:

mylist = "Hello"
x = 
len(mylist)

Output:

5

Example2:

Return the number of items in a list:

mylist = ["apple""banana""cherry"]
x = 
len(mylist)

Output:

3

 

2.Range():

The built-in function range() generates the integer numbers between the given start integer to the stop integer, i.e., It returns a range object. Using for loop, we can iterate over a sequence of numbers produced by the range() function. Let’s understand how to use a range() function of Python 3 with the help of a simple example.

Example:

for i in range(6):

    print(i, end=', ')

Output:

0, 1, 2, 3, 4, 5,

Syntax for range function is :-

range(start, stop[, step])

It takes three arguments. Out of the three 2 arguments are optional. I.e., start and step are the optional arguments.

A start argument is a starting number of the sequence. i.e., lower limit. By default, it starts with 0 if not specified.

A stop argument is an upper limit. i.e., generate numbers up to this number, The range() doesn’t include this number in the result.

The step is a difference between each number in the result. The default value of the step is 1 if not specified.

Example2:

for i in range(2, 10, 2):
    print(i, end=', ')

Output:

2, 4, 6, 8,

Much more information on range function can be found here:

https://pynative.com/python-range-function/

 

3.Map function():

Python supports functional programming through a number of inbuilt features. One of the most useful is the map() function — especially in combination with lambda functions.

x = [1, 2, 3]
y = map(lambda x : x + 1 , x)
# prints out [2,3,4]print(list(y))
 

In the example above, map() applies a simple lambda function to each element in x. It returns a map object, which can be converted to some iterable object such as a list or tuple.

Syntax:

map(function, iterables)

 

 

4.Split function():

split() method returns a list of strings after breaking the given string by the specified separator.

Syntax :

str.split(separator, maxsplit)

Example:

Split a string into a list where each word is a list item:

txt = "welcome to the jungle"

x = txt.split()

print(x)

Output:

['welcome', 'to', 'the', 'jungle'], 'ju

5.Sorted function():

The sorted() function returns a sorted list of the specified iterable object.

You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.

Example:

Sort a tuple:

a = ("b", "g", "a", "d", "f", "c", "h", "e")

x = sorted(a)

print(x)

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

More detailed information is available below in this page:

https://www.programiz.com/python-programming/methods/built-in/sorted

You can find many more useful functions below with examples:

https://www.w3schools.com/python/python_ref_functions.asp

 


Comments