Python

Python Built in Functions List with Syntax and Examples

Python is a Programming Language that has three types of functions namely user-defined functions, lambda functions, built-in functions. In this article, we will deal with the Python Built-in Functions that are ready to use along with their syntax and examples. So without further delay let’s get started with what are python built-in functions, everything you need to know about the python built-in functions list.

What are Python Built-In Functions?

Python Built-in Functions are the ones whose functionality is predefined in the language. Python interpreter provides us with a lot of functions already existing that are ready to use.

List of Built-in Functions in Python

Below is the list of Python Built-in Functions that you need to know. Learn about each one of them along with their respective descriptions through the following list. In order to use them all, you need to do is call them and pass the concerned argument as specified by us in the description of each built-in function in the list below. That’s it they perform the respective operation when executed.

Python String Methods Examples

Python List Methods Examples

Python Dictionary Methods Examples

Python Set Methods Examples

Python Mathematical Methods Examples

Python cmath Methods Examples

Python Statistics Methods Examples

Python Random Methods Examples

Python Calendar Module Examples

Python Itertools Module Examples

Python Collections Module Examples

Python Programs

We wish the information existing on the page with regards to Python Built-in Functions has been extremely useful to you. In case of any doubts do ask us or leave your suggestions through the comment section so that we can get back to you. Keep connected to our site to have latest updates on other Python Functions too in no time.

Python Built in Functions List with Syntax and Examples Read More »

Program for Python String swapcase() Function

In the previous article, we have discussed Program for Python String title() Function
Python String swapcase() Function:

The swapcase() method returns a string in which all upper case letters are converted to lower case and vice versa.

Syntax:

string.swapcase()

Parameters: This function doesn’t have any parameters.

Examples:

Example1:

Input:

Given first string = "welcome to PYTHON-Programs"
Given second string = "HELLO ALL GOOD MORNING"

Output:

The above given first string is : welcome to PYTHON-Programs
The given first string after applying swapcase() function:
WELCOME TO python-pROGRAMS
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying swapcase() function:
hello all good morning

Example2:

Input:

Given first string = "hello ALL gOOD MORNing"
Given second string = "HELLO this is BTECHGEEKS"

Output:

The above given first string is : hello ALL gOOD MORNing
The given first string after applying swapcase() function:
HELLO all Good mornING
The above given second string is : HELLO this is BTECHGEEKS
The given second string after applying swapcase() function:
hello THIS IS btechgeeks

Program for String swapcase() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the swapcase() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "welcome to PYTHON-Programs"
# Apply swapcase() function to the given first string that returns a string
# in which all upper case letters are converted to lower case and vice versa.
# Store it in another variable.
rslt_str1 = gvn_fststr.swapcase()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying swapcase() function.
print("The given first string after applying swapcase() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "HELLO ALL GOOD MORNING"
rslt_str2 = gvn_scndstr.swapcase()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying swapcase() function:")
print(rslt_str2)

Output:

The above given first string is : welcome to PYTHON-Programs
The given first string after applying swapcase() function:
WELCOME TO python-pROGRAMS
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying swapcase() function:
hello all good morning

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the swapcase() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some Random String = ")
# Apply swapcase() function to the given first string that returns a string
# in which all upper case letters are converted to lower case and vice versa.
# Store it in another variable.
rslt_str1 = gvn_fststr.swapcase()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying swapcase() function.
print("The given first string after applying swapcase() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
rslt_str2 = gvn_scndstr.swapcase()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying swapcase() function:")
print(rslt_str2)

Output:

Enter some Random String = hello ALL gOOD MORNing
The above given first string is : hello ALL gOOD MORNing
The given first string after applying swapcase() function:
HELLO all Good mornING
Enter some Random String = HELLO this is BTECHGEEKS
The above given second string is : HELLO this is BTECHGEEKS
The given second string after applying swapcase() function:
hello THIS IS btechgeeks

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.

Program for Python String swapcase() Function Read More »

Python String title() Function with Examples

In the previous article, we have discussed Python String split() Method Examples
Python String title() Function:

The title() method returns a string in which the first character of each word is capitalized. As in a header or a title.

If the word contains a number or a symbol, the following letter will be converted to upper case.

Syntax:

string.title()

Parameters: This function doesn’t have any parameters.

Examples:

Example1:

Input:

Given first string =  "hello this is btechgeeks"
Given second string = "10345 good morning 2a2a btechgeeks"

Output:

The above given first string is : hello this is btechgeeks
The given first string after applying title() function:
Hello This Is Btechgeeks
The above given second string is : 10345 good morning 2a2a btechgeeks
The given second string after applying title() function:
10345 Good Morning 2A2A Btechgeeks

Note:

It is worth noting that the first letter after a non-alphabet 
letter is converted to an upper case letter.

Example2:

Input:

Given first string = "welcome to Python-Programs"
Given second string = "HELLO ALL GOOD MORNING"

Output:

The above given first string is : welcome to Python-Programs
The given first string after applying title() function:
Welcome To Python-Programs
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying title() function:
Hello All Good Morning

Program for String title() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the title() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "hello this is btechgeeks"
# Apply title() function to the given first string that returns a string in
# which the first character of each word is capitalized. As in a header or a title.
# Store it in another variable.
rslt_str1 = gvn_fststr.title()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying title() function.
print("The given first string after applying title() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "10345 good morning 2a2a btechgeeks"
rslt_str2 = gvn_scndstr.title()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying title() function:")
print(rslt_str2)

Output:

The above given first string is : hello this is btechgeeks
The given first string after applying title() function:
Hello This Is Btechgeeks
The above given second string is : 10345 good morning 2a2a btechgeeks
The given second string after applying title() function:
10345 Good Morning 2A2A Btechgeeks

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the title() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some Random String = ")
# Apply title() function to the given first string that returns a string in
# which the first character of each word is capitalized. As in a header or a title.
# Store it in another variable.
rslt_str1 = gvn_fststr.title()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying title() function.
print("The given first string after applying title() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
rslt_str2 = gvn_scndstr.title()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying title() function:")
print(rslt_str2)

Output:

Enter some Random String = welcome to Python-Programs
The above given first string is : welcome to Python-Programs
The given first string after applying title() function:
Welcome To Python-Programs
Enter some Random String = HELLO ALL GOOD MORNING
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying title() function:
Hello All Good Morning

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.

Python String title() Function with Examples Read More »

Python String split() Method Examples

In the previous article, we have discussed Python String strip() Method Examples
split() Method in Python:

Splitting a string into a list is accomplished by the split() method.

The separator can be specified; the default separator is any whitespace.

Note: When maxsplit is specified, the list will have the number of elements specified plus one.

Syntax:

string.split(separator, maxsplit)

Parameters

separator: This is Optional. This parameter specifies the separator to be used when splitting the string. By default, any whitespace serves as a separator.

maxsplit: This is Optional. Specifies the number of splits to perform. The default value is -1, which represents “all occurrences.”

Examples:

Example1:

Input:

Given string = "welcome,to,python,programs"
Given separator = ","
Given maxsplit value = 1

Output:

The above given string is : welcome,to,python,programs
The given string after applying split() function:
['welcome', 'to,python,programs']

Example2:

Input:

Given string = "good morning this is btechgeeks"

Output:

The above given string is : good morning this is btechgeeks
The given string after applying split() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

String split() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the separator as static input and store it in another variable.
  • Give the maxsplit as static input and store it in another variable.
  • Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the split() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "welcome,to,python,programs"
# Give the separator as static input and store it in another variable.
gvn_separtr = ","
# Give the maxsplit as static input and store it in another variable.
maxsplt_valu = 1
# Apply split() method to the given string for the given separator and
# maxsplit() values which split a string into a list.
# Store it in another variable.
rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying split() function.
print("The given first string after applying split() function:")
print(rslt_lst1)
# Similarly do the same for other string without giving the separator and
# maxsplit values and print the result list obtained.
gvn_scndstr = "good morning this is btechgeeks"
rslt_lst2 = gvn_scndstr.split()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying split() function:")
print(rslt_lst2)

Output:

The above given first string is : welcome,to,python,programs
The given first string after applying split() function:
['welcome', 'to,python,programs']
The above given second string is : good morning this is btechgeeks
The given second string after applying split() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the separator as user input using the input() function and store it in another variable.
  • Give the maxsplit as user input using the int(input()) function and store it in another variable.
  • Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying split() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some random string = ")
# Give the separator as user input using the input() function and store it in another variable.
gvn_separtr = input("Enter some random separator = ")
# Give the maxsplit as user input using the int(input()) function and store it in another variable. 
maxsplt_valu = int(input("Enter some random number = "))
# Apply split() method to the given string for the given separator and
# maxsplit() values which split a string into a list.
# Store it in another variable.
rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying split() function.
print("The given first string after applying split() function:")
print(rslt_lst1)
# Similarly do the same for other string without giving the separator and
# maxsplit values and print the result list obtained.
gvn_scndstr = input("Enter some random string = ")
rslt_lst2 = gvn_scndstr.split()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying split() function:")
print(rslt_lst2)

Output:

Enter some random string = hello#this is btechgeeks
Enter some random separator = #
Enter some random number = 1
The above given first string is : hello#this is btechgeeks
The given first string after applying split() function:
['hello', 'this is btechgeeks']
Enter some random string = hello all good morning
The above given second string is : hello all good morning
The given second string after applying split() function:
['hello', 'all', 'good', 'morning']

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.

Python String split() Method Examples Read More »

Python ord() Function with Examples

In the previous article, we have discussed Python Program for round() Function
ord() Function in Python:

The ord() function returns a number that represents the Unicode code of a given character.

Syntax:

ord(character)

Parameters

character: It is a Unicode character.

Return Value:

The ord() function returns the Unicode character as an integer.

Note: When the length of the string is not equal to one, a TypeError is thrown.

Examples:

Example1:

Input:

Given character = '4'
Given character = '$'
Given character = 'd'

Output:

The integer representing the given Unicode character 4 =  52
The integer representing the given Unicode character $ = 36
The integer representing the given Unicode character d = 100

Example2:

Input:

Given character = '0'
Given character = '@'
Given character = 'y'

Output:

The integer representing the given Unicode character 0 =  48
The integer representing the given Unicode character @ = 64
The integer representing the given Unicode character y = 121

Program for ord() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the character as static input and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as static input and store it in a variable.
gvn_charctr = '4'
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

The integer representing the given Unicode character 4 =  52

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the character as user input using the input() function and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as user input using the input() function and store it in a variable.
gvn_charctr = input("Enter some random character = ")
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

Enter some random character = B
The integer representing the given Unicode character B = 66

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python ord() Function with Examples Read More »

Python memoryview() Function with Examples

In the previous article, we have discussed Python Program for ord() Function
The memoryview() function in Python returns memory views objects. Before delving deeper into the memoryview() function, consider why we use it.

Why are we using the memoryview() function?
Because Memory view is a safe way to expose the buffer protocol in Python and a memoryview behaves like bytes in many useful contexts (for example, it supports the mapping protocol), it can serve as an adequate replacement if used correctly. The best part is that it uses the buffer protocol under the hood to avoid copies and simply juggle pointers to data. So, before we get into what memory views are available, we must first understand the Buffer Protocol.

What is buffer protocol?

The buffer protocol allows you to access an object’s internal data. This internal data is in the form of a memory array or a buffer. It enables one object to expose its internal data (buffers) and another to access those buffers without requiring intermediate copying. The buffer protocol is only available to us at the C-API level, not through our normal codebase. Memory views are used to expose the same protocol to a standard Python codebase.

Memoryview

Memoryview objects enable Python code to access the internal data of an object that supports the buffer protocol without the need for copying. The memoryview() function provides direct read and write access to byte-oriented data in an object without the need to copy it first. Because it does not create a copy when slicing, this can result in significant performance gains when working with large objects.

Syntax:

memoryview(object)

Parameters

object: The object whose internal data will be exposed.
buffer protocol support – str and bytearray (but not Unicode).

Return Value:

A memoryview object is returned.

Examples:

Example1:

Input:

Given String = 'EFG'

Output:

69
b'EFG'
[69, 70, 71]

Example2:

Input:

Given String = 'uvw'

Output:

117
b'uvw'
[117, 118, 119]

Program for memoryview() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Take a random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Take a random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray('EFG', 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

69
b'EFG'
[69, 70, 71]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take the above random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Take the above random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray(gvn_str, 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

Enter some random string = uvw
117
b'uvw'
[117, 118, 119]

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

 

Python memoryview() Function with Examples Read More »

Python Program to Find Index of a Tuple Item

In the previous article, we have discussed Python Program for memoryview() Function
The tuple index function in Python returns the index of a given tuple item. The index position of the first found value is returned by the tuple index function.

Syntax:

TupleName.index(tupleValue, start, end)

If you specify a start value, the index function will begin searching from that point. Similarly, setting the end position causes the tuple index function to stop looking at that number.

Examples:

Example1:

Input:

Given tuple = (10, 20, 30, 50, 70, 20)
Given first number = 50
Given second number = 20
Given start position = 2

Output:

The given number's 50 index position =  3
The given number's 20 index position after the given start position 2 =  5

Example2:

Input:

Given tuple = (1, 3, 5, 7, 8, 2, 5)
Given first number = 3
Given second number = 5
Given start position = 3

Output:

The given number's 3 index position =  1
The given number's 5 index position after the given start position 3 =  6

Program to Find Index of a Tuple Item in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Get the index position of the given number using the index() method and store it in another variable.
  • Print the index position of the given number.
  • Give the second number as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
  • Store it in another variable.
  • Print the index position of the given second number after the given start position.
  • The Exit of Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvn_tupl = (10, 20, 30, 50, 70, 20)
# Give the number as static input and store it in another variable.
gvn_numb1 = 50
# Get the index position of the given number using the index() method and
# store it in another variable.
indx_1 = gvn_tupl.index(gvn_numb1)
# Print the index position of the given number.
print("The given number's", gvn_numb1, "index position = ", indx_1)
# Give the second number as static input and store it in another variable.
gvn_numb2 = 20
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 2
# Pass the given second number, start position as arguments to the index()
# method to get the index value of the given second number after the given
# start position.
# Store it in another variable.
indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)
# Print the index position of the given second number after the given
# start position.
print("The given number's", gvn_numb2,
      "index position after the given start position", gvn_strtpositn, "= ", indx_2)

Output:

The given number's 50 index position =  3
The given number's 20 index position after the given start position 2 =  5

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Get the index position of the given number using the index() method and store it in another variable.
  • Print the index position of the given number.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
  • Store it in another variable.
  • Print the index position of the given second number after the given start position.
  • The Exit of Program.

Below is the implementation:

# Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.
gvn_tupl = tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb1 = int(input("Enter some random number = "))
# Get the index position of the given number using the index() method and
# store it in another variable.
indx_1 = gvn_tupl.index(gvn_numb1)
# Print the index position of the given number.
print("The given number's", gvn_numb1, "index position = ", indx_1)
# Give the second number as user input using the int(input()) function and
# store it in another variable.
gvn_numb2 =  int(input("Enter some random number = "))
# Give the start position as user input using the int(input()) function and
# store it in another variable.
gvn_strtpositn =  int(input("Enter some random number = "))
# Pass the given second number, start position as arguments to the index()
# method to get the index value of the given second number after the given
# start position.
# Store it in another variable.
indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)
# Print the index position of the given second number after the given
# start position.
print("The given number's", gvn_numb2,
      "index position after the given start position", gvn_strtpositn, "= ", indx_2)

Output:

Enter some random Tuple Elements separated by spaces = 1 3 5 7 8 2 5
Enter some random number = 3
The given number's 3 index position = 1
Enter some random number = 5
Enter some random number = 3
The given number's 5 index position after the given start position 3 = 6

Program to Find Index of a Tuple Item in Python without using index() Function

Below is the implementation:

gvn_tupl = (10, 40, 50, 30, 60, 40, 20, 40, 30)
print(gvn_tupl)

gvn_valu = 40
for itr in range(len(gvn_tupl)):
    if gvn_tupl[itr] == gvn_valu:
        print("The given number", gvn_valu, "index position = ", itr)
        break
print("The all Index Positions of given number:")
for k in range(len(gvn_tupl)):
    if gvn_tupl[k] == gvn_valu:
        print("Index Position of 20 = ", k)

print("The all the Index Positions of given number using enumerate function:")
for itor, p in enumerate(gvn_tupl):
    if p == gvn_valu:
        print("Index Position of 20 = ", itor)

Output:

(10, 40, 50, 30, 60, 40, 20, 40, 30)
The given number 40 index position =  1
The all Index Positions of given number:
Index Position of 20 =  1
Index Position of 20 =  5
Index Position of 20 =  7
The all the Index Positions of given number using enumerate function:
Index Position of 20 =  1
Index Position of 20 =  5
Index Position of 20 =  7

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python Program to Find Index of a Tuple Item Read More »

Python reversed() Function with Examples

In the previous article, we have discussed Python Program for chr() Function
reversed() Function in Python:

The reversed() function returns an iterator object that has been reversed.

Syntax:

reversed(sequence)

Parameters

sequence: This is required.Any object that is iterable. (list,tuple..etc)

A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, string, list, range, and so on.

In addition, we can use reversed() in any object that implements __reverse__ ().

Return Value:

The reversed() function returns an iterator that iterates through the given sequence in reverse order.

Examples:

Example1:

Input:

Given string = 'Python-programs'
Given tuple = ("h", "e", "l", "l", "o")

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']

Note: After applying the reverse function the result is converted into the list.Hence the result is in the list form.

Example2:

Input:

Given list =  [6, 7, 8, 9, 10]
Given range = (2, 12)

Output:

[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Program for reversed() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple as static input and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as static input and store it in another variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • Print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'Python-programs'
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple as static input and store it in another variable.
gvn_tupl = ("h", "e", "l", "l", "o")
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as static input and store it in another variable.
gvn_lst = [6, 7, 8, 9, 10]
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as arguments to the range()function and
# store it in a variable.
gvn_rangee = range(2, 12)
# Get the reverse order of the range by applying the reversed() function
# to the given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']
[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as user input using the int(input()) function and store them in two separate variables.
  • Pass the given lower and upper as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • Print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple user input using list(),map(),input(),and split() functions and
# store it in another variable.
gvn_tupl=tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as user input using list(),map(),input(),and split() functions and
# store it in a variable.
gvn_lst=list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as user input using the int(input()) function and
# store them in two separate variables.
gvn_lowrlmt=int(input("Enter some random number = "))
gvn_upprlmt=int(input("Enter some random number = "))
# Pass the given lower and upper as arguments to the range() function and store it in a variable.
gvn_rangee=range(gvn_lowrlmt, gvn_upprlmt)
# Get the numbers in reverse order by applying the reversed() function to the
# given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

Enter some random string = good morning
['g', 'n', 'i', 'n', 'r', 'o', 'm', ' ', 'd', 'o', 'o', 'g']
Enter some random Tuple Elements separated by spaces = 0 4 5 1 2
[2, 1, 5, 4, 0]
Enter some random List Elements separated by spaces = 7 4 2 6
[6, 2, 4, 7]
Enter some random number = 15
Enter some random number = 22
[21, 20, 19, 18, 17, 16, 15]

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python reversed() Function with Examples Read More »

Python round() Function with Examples

In the previous article, we have discussed Python Program for reversed() Function
round() Function in Python:

The round() function returns a floating-point number with the specified number of decimals that is a rounded version of the specified number.

The function will return the nearest integer if the number of decimals is set to 0.

Syntax:

round(number, digits)

Parameters

number: This is required. It is the number that should be rounded.

digits: This is Optional. When rounding a number, the number of decimals to use(up to how many digits to be rounded after decimal) The default value is 0.

Return Value:

The function round() returns the

If digits is not provided, the nearest integer to the given number is used; otherwise, the number is rounded off to digits.

Examples:

Example1:

Input:

Given number = 10.5678
Given no of digits = 2

Output:

The rounded value of the given number 10.5678 upto 2 digits =  10.57

Example2:

Input:

Given number = 7.8

Output:

The rounded value of the given number 7.8 = 8

Program for round() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the digits (up to how many digits to be rounded after the decimal)as static input and store it in another variable.
  • Get the rounded value of the given number by passing the given number and given digits as arguments to the round() function.
  • Store it in another variable.
  • Print the rounded value of the given number.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 10.5678
# Give the digits (up to how many digits to be rounded after the decimal)as
# static input and store it in another variable.
gvn_digitss = 2
# Get the rounded value of the given number by passing the given number
# and given digits as arguments to the round() function.
# Store it in another variable.
numbraftr_round = round(gvn_numb, gvn_digitss)
# Print the rounded value of the given number.
print("The rounded value of the given number", gvn_numb,
      "upto", gvn_digitss, "digits = ", numbraftr_round)

Output:

The rounded value of the given number 10.5678 upto 2 digits =  10.57

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Get the rounded value of the given number by passing the given number as an argument to the round() function.
  • Store it in another variable.
  • Print the rounded value of the given number.
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numb = float(input("Enter some random number = "))
# Get the rounded value of the given number by passing the given number
# as an argument to the round() function.
# Store it in another variable.
numbraftr_round = round(gvn_numb)
# Print the rounded value of the given number.
print("The rounded value of the given number", gvn_numb, "= ", numbraftr_round)

Output:

Enter some random number = 7.8
The rounded value of the given number 7.8 = 8

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python round() Function with Examples Read More »

Python all() Function with Examples

In the previous article, we have discussed Program for Python String swapcase() Function
all() Function in Python:

The function all() returns If all of the items in an iterable are true, it returns True; otherwise, it returns False.

The all() function returns True if the iterable object is empty.

Syntax:

all(iterable)

Parameters

iterable: It may be any iterable object like list, tuple, dictionary, set, etc.

Note: When applied to a dictionary, the all() function determines whether all of the keys are true, rather than the values.

Return value:

The function all() returns:

  • True if all of the elements in an iterable are true.
  • If any element in an iterable is false, then it returns false.

Note:

when 0 is given in the form as string, then it returns true
0 = False
'0' = True
for example: number = '00000'
# output = True

Examples:

Example1:

Input:

Given list =  [0, 1, 2, 3]
Given tuple = (10, 30, -10)
Given dictionary = {0: 'good', 1: 'morning'}

Output:

The result after applying all() function on the given list =  False
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  False

Example2:

Input:

Given list =  ['True', 'True', 'True']
Given tuple = ()
Given dictionary = {3: 'welcome', 2: 'to', 1: 'Python-programs'}

Output:

The result after applying all() function on the given list =  True
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  True

Program for all() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the tuple as static input and store it in another variable.
  • Give the dictionary as static input and store it in another variable.
  • Apply all() function on the given list that returns true if all of the items in an iterable are true, otherwise, it returns False.
  • Store it in another variable.
  • Apply all() function on the given tuple and store it in another variable.
  • Apply the all() function on the given dictionary and store it in another variable.
  • Print the result after applying all() the function on the given list.
  • Print the result after applying all() the function on the given tuple.
  • Print the result after applying all() the function on the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [0, 1, 2, 3]
# Give the tuple as static input and store it in another variable.
gvn_tupl = (10, 30, -10)
# Give the dictionary as static input and store it in another variable.
gvn_dictnry = {0: 'good', 1: 'morning'}
# Apply all() function on the given list that returns true if all of the
# items in an iterable are true, otherwise, it returns False.
# Store it in another variable.
rslt_lst = all(gvn_lst)
# Apply all() function on the given tuple and store it in another variable.
rslt_tupl = all(gvn_tupl)
# Apply all() function on the given dictionary and store it in another variable.
rslt_dictnry = all(gvn_dictnry)
# Print the result after applying all() function on the given list.
print("The result after applying all() function on the given list = ", rslt_lst)
# Print the result after applying all() function on the given tuple.
print("The result after applying all() function on the given tuple = ", rslt_tupl)
# Print the result after applying all() function on the given dictionary.
print("The result after applying all() function on the given dictionary = ", rslt_dictnry)

Output:

The result after applying all() function on the given list =  False
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Apply all() function on the given list that returns true if all of the items in an iterable are true, otherwise, it returns False.
  • Store it in another variable.
  • Apply all() function on the given tuple and store it in another variable.
  • Apply the all() function on the given dictionary and store it in another variable.
  • Print the result after applying all() the function on the given list.
  • Print the result after applying all() the function on the given tuple.
  • Print the result after applying all() the function on the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the list as User input using list(),map(),input(),and split() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the tuple as User input using list(),map(),input(),and split() functions
# and store it in another variable.
gvn_tupl = tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee = input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[int(keyy)] = valuee

# Apply all() function on the given list that returns true if all of the
# items in an iterable are true, otherwise, it returns False.
# Store it in another variable.
rslt_lst = all(gvn_lst)
# Apply all() function on the given tuple and store it in another variable.
rslt_tupl = all(gvn_tupl)
# Apply all() function on the given dictionary and store it in another variable.
rslt_dictnry = all(gvn_dict)
# Print the result after applying all() function on the given list.
print("The result after applying all() function on the given list = ", rslt_lst)
# Print the result after applying all() function on the given tuple.
print("The result after applying all() function on the given tuple = ", rslt_tupl)
# Print the result after applying all() function on the given dictionary.
print("The result after applying all() function on the given dictionary = ", rslt_dictnry)

Output:

Enter some random List Elements separated by spaces = 10 25 35 0
Enter some random Tuple Elements separated by spaces = 1 1 1 1
Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 3 welcome
Enter key and value separated by spaces = 2 to
Enter key and value separated by spaces = 1 Python-programs
The result after applying all() function on the given list = False
The result after applying all() function on the given tuple = True
The result after applying all() function on the given dictionary = True

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python all() Function with Examples Read More »