Python

Python oct() Function with Examples

In the previous article, we have discussed Python min() Function with Examples
oct() Function in Python:

The oct() function is used to convert an integer to an octal string.

Python prefixes octal strings with 0o.

Syntax:

oct(number)

Parameters

number: This is required. It is an integer number.

If the value is not an integer (binary, decimal, or hexadecimal), it should implement __index__() to return an integer.

Return Value:

The oct() function takes an integer number and returns an octal string.

Examples:

Example1:

Input:

Given Number = 18

Output:

The given number's{ 18 } Octal Value =  0o22
The oct() function Return Type = <class 'str'>

Example2:

Input:

Given number  = 13
Given binary number  = 0b10
Given hexadecimal number  = 0XB

Output:

The oct(13) value is =  0o15
The oct(0b10) value =  0o2
The oct(0XB) value =  0o13

oct() Function with Examples in Python

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

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the oct() function to get the Octal value of a given number.
  • Store it in another variable.
  • Print the given number’s OctalValue.
  • Print the Return Type of oct() Function using the type() method by passing the oct(given number) as an argument to it.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 18
# Pass the given number as an argument to the oct() function to get the
# Octal value of a given number.
# Store it in another variable.
Octal_valu = oct(gvn_numbr)
# Print the given number's Octal Value.
print("The given number's{", gvn_numbr,
      "} Octal Value = ", Octal_valu)
# Print the return Type of oct() Function using the type() method by passing
# the oct(given number) as an argument to it.
print("The oct() function Return Type =", type(oct(gvn_numbr)))

Output:

The given number's{ 18 } Octal Value =  0o22
The oct() function Return Type = <class 'str'>
oct() For binary and Hexadecimal numbers
# converting decimal to octal
print('The oct(13) value is = ', oct(13))

# converting binary to octal
print('The oct(0b10) value = ', oct(0b10))

# converting hexadecimal to octal
print('The oct(0XB) value = ', oct(0XB))

Output:

The oct(13) value is =  0o15
The oct(0b10) value =  0o2
The oct(0XB) value =  0o13

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

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the oct() function to get the Octal value of a given number.
  • Store it in another variable.
  • Print the given number’s OctalValue.
  • Print the Return Type of oct() Function using the type() method by passing the oct(given number) as an argument to it.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input("Enter some random number = "))
# Pass the given number as an argument to the oct() function to get the
# Octal value of a given number.
# Store it in another variable.
Octal_valu = oct(gvn_numbr)
# Print the given number's Octal Value.
print("The given number's{", gvn_numbr,
      "} Octal Value = ", Octal_valu)
# Print the return Type of oct() Function using the type() method by passing
# the oct(given number) as an argument to it.
print("The oct() function Return Type =", type(oct(gvn_numbr)))

Output:

Enter some random number = 10
The given number's{ 10 } Octal Value = 0o12
The oct() function Return Type = <class 'str'>

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python oct() Function with Examples Read More »

Python len() Function with Examples

In the previous article, we have discussed Python locals() Function with Examples
len() Function in Python:

The number of items in an object is returned by the len() function.

The len() function returns the number of characters in a string when the object is a string.

Syntax:

len(object)

Parameters

object: This is Required. It is an object. It should be a sequence or a collection.

Return Value:

The number of items in an object is returned by the len() function.

A TypeError exception will be thrown if an argument is not passed or if an invalid argument is passed.

Examples:

Example1:

Input:

Given List = [1, 20, 3, 40, 5]

Output:

The length of the given list =  5

Example2:

Input:

Given String = "hello btechgeeks"

Output:

The length of the given string =  16

len() Function with Examples in Python

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

1)For Lists

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the len() function to get the length of the given list.
  • Store it in another variable.
  • Print the length of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 20, 3, 40, 5]
# Pass the given list as an argument to the len() function to get the length
# of the given list.
# Store it in another variable.
lst_lengt = len(gvn_lst)
# Print the length of the given list.
print("The length of the given list = ", lst_lengt)

Output:

The length of the given list =  5
2)For Strings

Approach:

  • Give the string as static input and store it in a variable.
  • Pass the given string as an argument to the len() function to get the length of the given string.
  • Store it in another variable.
  • Print the length of the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello btechgeeks"
# Pass the given string as an argument to the len() function to get the length
# of the given string.
# Store it in another variable.
str_lengt = len(gvn_str)
# Print the length of the given string.
print("The length of the given string = ", str_lengt)

Output:

The length of the given string =  16
How does len() work with dictionaries and sets?
# set doesn't allows duplicates
gvn_set = {10, 20, 40, 50, 10}
print("The length of the given set", gvn_set, "=", len(gvn_set))

gvn_set2 = set()
print("The length of the given set", gvn_set2, "=", len(gvn_set2))

gvn_dictnry = {100: 'hello', 200: 'btechgeeks'}
print("The length of the given dictionary", gvn_dictnry, "=", len(gvn_dictnry))

Output:

The length of the given set {40, 10, 20, 50} = 4
The length of the given set set() = 0
The length of the given dictionary {100: 'hello', 200: 'btechgeeks'} = 2

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

1)For Lists

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the len() function to get the length of the given list.
  • Store it in another variable.
  • Print the length of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the len() function to get the length
# of the given list.
# Store it in another variable.
lst_lengt = len(gvn_lst)
# Print the length of the given list.
print("The length of the given list = ", lst_lengt)

Output:

Enter some random List Elements separated by spaces = -1 -2 7 8 9 0 3
The length of the given list = 7
2)For Strings

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Pass the given string as an argument to the len() function to get the length of the given string.
  • Store it in another variable.
  • Print the length of the given string.
  • 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 = ")
# Pass the given string as an argument to the len() function to get the length
# of the given string.
# Store it in another variable.
str_lengt = len(gvn_str)
# Print the length of the given string.
print("The length of the given string = ", str_lengt)

Output:

Enter some random string = good morning btechgeeks
The length of the given string = 23

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python len() Function with Examples Read More »

Python max() Function with Examples

In the previous article, we have discussed Python len() Function with Examples
max() Function in Python:

The max() function returns the item with the highest value or the highest value in an iterable.

If the values are strings, they are compared alphabetically.

Syntax:

max(iterable)

or

max(n1, n2, n3,........)

Parameters

iterable: An iterable that compares one or more items.

n1, n2, n3,……..: a single or multiple items to compare.

Return Value:

max() returns the iterable’s largest element.

Examples:

Example1:

Input:

Given List = [1, 2, 5, 6, 15, 7]

Output:

The greatest number in the given list = 15

Example2:

Input:

Given List = ["hello", "this", "is", "btechgeeks"]

Output:

The largest string in the given list (alphabetically ordered) is :
this

Note:

If the items in an iterable are strings, the largest (alphabetically ordered) 
item is returned.

max() Function with Examples in Python

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

1)For Number List

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the max() function to get the greatest or the largest element in the given list.
  • Store it in another variable.
  • Print the greatest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 5, 6, 15, 7]
# Pass the given list as an argument to the max() function to get the greatest
# or the largest element in the given list.
# Store it in another variable.
greatst_numb = max(gvn_lst)
# Print the greatest element(number) in the given list.
print("The greatest number in the given list =", greatst_numb)

Output:

The greatest number in the given list = 15
2)For String List

Similarly, do the same to get the largest string in the given list.

Note: If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.

Approach:

  • Give the list (string list)as static input and store it in a variable.
  • Pass the given list as an argument to the max() function to get the largest string in the given list. If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the largest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_strlst = ["hello", "this", "is", "btechgeeks"]
# Pass the given list as an argument to the max() function to get the largest
# string in the given list.If the items in an iterable are strings,
# the largest (alphabetically ordered) item is returned.
# Store it in another variable.
largst_str = max(gvn_strlst)
# Print the largest string in the given list (alphabetically ordered).
print("The largest string in the given list (alphabetically ordered) is :")
print(largst_str)

Output:

The largest string in the given list (alphabetically ordered) is :
this
3)Passing multiple arguments

To get the greatest number from the given numbers.

# Give some random numbers as arguments to the max() function.
largst_numb = max(5, 10, -2, 9, 6)
# Print the greatest number from the given numbers
print("The greatest number from the given numbers = ", largst_numb)

Output:

The greatest number from the given numbers =  10

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

1)For Number List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the max() function to get the greatest or the largest element in the given list.
  • Store it in another variable.
  • Print the greatest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the max() function to get the greatest
# or the largest element in the given list.
# Store it in another variable.
greatst_numb = max(gvn_lst)
# Print the greatest element(number) in the given list.
print("The greatest number in the given list =", greatst_numb)

Output:

Enter some random List Elements separated by spaces = 34 50 68 12 100
The greatest number in the given list = 100
2)For String List

Approach:

  • Give the list (string list) as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the max() function to get the largest string in the given list. If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the largest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_strlst = list(input(
   'Enter some random List Elements separated by spaces = ').split())
# Pass the given list as an argument to the max() function to get the largest
# string in the given list.If the items in an iterable are strings,
# the largest (alphabetically ordered) item is returned.
# Store it in another variable.
largst_str = max(gvn_strlst)
# Print the largest string in the given list (alphabetically ordered).
print("The largest string in the given list (alphabetically ordered) is :")
print(largst_str)

Output:

Enter some random List Elements separated by spaces = good morning btechgeeks
The largest string in the given list (alphabetically ordered) is :
morning

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python max() Function with Examples Read More »

Python hasattr() Method with Examples

In the previous article, we have discussed Python globals() Function with Examples
hasattr() Method in Python:

If the specified object has the specified attribute, the hasattr() function returns true; otherwise, it returns False.

Syntax:

hasattr(object, attribute)

Parameter Values

object: This is required. It is an object.

attribute: The name of the attribute to be checked whether exists or not.

Return Value:

The method hasattr() returns:

  • True if the object has the specified named attribute.
  • False if the object lacks the specified named attribute.

hasattr() Method with Examples in Python

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

Approach:

  • Create a class say Employdetails.
  • Take a variable and initialize it with some random number(id).
  • Take another variable and initialize it with some random name(ename).
  • Create an object for the class and store it in a variable.
  • Check if the above class has an attribute id by passing arguments like the above object and attribute name using the hasattr() function and print the result.
  • Similarly, do the same for the other attribute(jobrole) and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails:
    # Take a variable and initialize it with some random number(id).
    id = 10
    # Take another variable and initialize it with some random name(ename).
    ename = 'Hitler'


# Create an object for the class and store it in a variable.
Employdetailsobj = Employdetails()
# Check if the above class has an attribute id by passing arguments like the
# above object and attribute name using the hasattr() function and print the result.
print('Does Employdetails has id?:', hasattr(Employdetailsobj, 'id'))
# Similarly, do the same for the other attribute(jobrole) and print the result.
print('Does Employdetails has jobrole?:', hasattr(Employdetailsobj, 'jobrole'))

Output:

Does Employdetails has id?: True
Does Employdetails has jobrole?: False

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

Approach:

  • Create a class say Employdetails.
  • Take a variable and initialize it with some random number(id).
  • Take another variable and initialize it with some random name(ename).
  • Create an object for the class and store it in a variable.
  • Give some random attribute name as user input using the input() function and store it in a variable.
  • Give the other attribute name as user input using the input() function and store it in another variable.
  • Check if the above class has the above attribute by passing arguments like the above object and above attribute name using the hasattr() function and print the result.
  • Similarly, do the same for the other attribute and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails:
    # Take a variable and initialize it with some random number(id).
    id = 10
    # Take another variable and initialize it with some random name(ename).
    ename = 'Hitler'


# Create an object for the class and store it in a variable.
Employdetailsobj = Employdetails()
# Give some random attribute name as user input using the input() function
# and store it in a variable.
atrname1 = input("Enter some random attribute name = ")
# Give the other attribute name as user input using the input() function
# and store it in another variable.
atrname2 = input("Enter some random attribute name = ")
# Check if the above class has the above attribute by passing arguments like the above object
# and above attribute name using the hasattr() function and print the result.
print('Does Employdetails has', atrname1, '?:',
      hasattr(Employdetailsobj, atrname1))
# Similarly, do the same for the other attribute and print the result.
print('Does Employdetails has', atrname2, '?:',
      hasattr(Employdetailsobj, atrname2))

Output:

Enter some random attribute name = age
Enter some random attribute name = ename
Does Employdetails has age ?: False
Does Employdetails has ename ?: True

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python hasattr() Method with Examples Read More »

Python globals() Function with Examples

In the previous article, we have discussed Python filter() Function with Examples
globals() Function in Python:

The globals() function returns a dictionary containing the global symbol table.

A symbol table contains information about the current program that is required.

Variable names, methods, classes, and so on are examples of this.

There are two types of symbol tables.

  1. Local symbol Table
  2. Global symbol Table

The local symbol table stores all information related to the program’s local scope and is accessed in Python via the locals() method.

The local scope could be within a function, a class, or something else.

Similarly, a Global symbol table stores all information related to the program’s global scope and is accessed in Python via the globals() method.

All functions and variables that are not associated with any class or function are included in the global scope.

Syntax:

globals()

Parameter Values: The globals() method does not accept any parameters.

Return Value:

This method returns the dictionary containing the current global symbol table.

globals() Function with Examples in Python

1)Using global variables, make changes to global variables ()

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

Approach:

  • Give the number(id) as static input and store it in a variable.
  • Modify the given number to some random number by using the globals() function.
  • Print the new number after modification.
  • The Exit of the Program.

Below is the implementation:

# Give the number(id) as static input and store it in a variable.
gvn_id = 10
# Modify the given number to some random number by using the globals() function.
globals()['gvn_id'] = 5
# Print the new number after modification.
print('The modified Id is:', gvn_id)

Output:

The modified Id is: 5
How does Python’s globals() method work?

Below is the implementation:

# Take a variable and initialize it with the globals() function.
m = globals()
# Print the above result.
print(m)

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f0e651f0cc0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/78f96300568d7239be1e5414d328ce86.py', '__cached__': None, 'm': {...}}

All global variables and other symbols for the current program are displayed in the output.

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

Approach:

  • Give the number(id) as user input using the int(input()) function and store it in a variable.
  • Give the new number as user input using the int(input()) function and store it in another variable.
  • Modify the given number to the above given new number by using the globals() function.
  • Print the new number after modification.
  • The Exit of the Program.

Below is the implementation:

# Give the number(id) as user input using the int(input()) function and store it in a variable.
gvn_id = int(input("Enter some random number = "))
# Give the new number as user input using the int(input()) function and store it in another variable.
new_id = int(input("Enter some random number = "))
# Modify the given number to the above given new number by using the globals() function.
globals()['gvn_id'] = new_id
# Print the new number after modification.
print('The modified Id is:', gvn_id)

Output:

Enter some random number = 20
Enter some random number = 50
The modified Id is: 50

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

 

Python globals() Function with Examples Read More »

Python filter() Function with Examples

In the previous article, we have discussed Python enumerate() Function with Examples
filter() Function in Python:

The filter() function returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.

Syntax:

filter(function, iterable)

Parameter Values:

function: It is a function that will be executed for each item in the iterable.

iterable: The iterable that will be filtered.

Return Value:

The iterator is returned by the filter() function.

Examples:

Example1:

Input:

Given List = [3, 10, 12, 13, 20, 7, 1, 16]

Output:

The Even numbers in a given list :
10
12
20
16

Example2:

Input:

Given List = [4, 6, 7, 9, 10, 12, 11]

Output:

The Even numbers in a given list :
4
6
10
12

filter() Function with Examples in Python

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

Approach:

  • Give the list as static input and store it in a variable.
  • Create a function say Even_numbers which accepts a number as an argument and returns true if the argument is an even number else it returns false.
  • Check if the given number is even using the if conditional statement.
  • If it is true, then return True.
  • Else return False.
  • Pass the above Function(Even_numbers),  given list as arguments to the filter function that returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.
  • Store it in another variable.
  • Loop in the above result using the for loop.
  • Print the iterator value of the for loop.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [3, 10, 12, 13, 20, 7, 1, 16]

# Create a function say Even_numbers which accepts a number as an argument and returns
# true if the argument is even number else it returns false.


def Even_numbers(numb):
  # Check if the given number is even using the if conditional statement.
    if numb % 2 == 0:
      # If it is true, then return True.
        return True
    # Else return False.
    else:
        return False


# Pass the above Function(Even_numbers),
# given list as arguments to the filter function that
# returns an iterator in which the items are filtered
# through a function to determine whether or not the item is accepted.
# Store it in another variable.
evn_numbrs = filter(Even_numbers, gvn_lst)
print("The Even numbers in a given list :")
# Loop in the above result using the for loop.
for n in evn_numbrs:
  # Print the iterator value of the for loop.
    print(n)

Output:

The Even numbers in a given list :
10
12
20
16
Using the Lambda Function Within a filter ()
gvn_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# The lambda function returns True for odd numbers
oddnumbrs_itrtor = filter(lambda n: (n % 2 != 0), gvn_lst)

# converting to list
odd_numbrs = list(oddnumbrs_itrtor)
print("The Odd numbers in a given list:")
print(odd_numbrs)

Output:

The Odd numbers in a given list:
[1, 3, 5, 7, 9]

Inside the filter, we have directly passed a lambda function ().

For Odd numbers, our lambda function returns True. As a result, the filter() function returns an iterator that only contains Odd numbers.

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

Approach:

  • Give the list as user input using map(),list(),input(),int() functions and store it in a variable.
  • Create a function say Even_numbers which accepts a number as an argument and returns true if the argument is an even number else it returns false.
  • Check if the given number is even using the if conditional statement.
  • If it is true, then return True.
  • Else return False.
  • Pass the above Function(Even_numbers),  given list as arguments to the filter function that returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.
  • Store it in another variable.
  • Loop in the above result using the for loop.
  • Print the iterator value of the for loop.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Create a function say Even_numbers which accepts a number as an argument and returns
# true if the argument is even number else it returns false.


def Even_numbers(numb):
  # Check if the given number is even using the if conditional statement.
    if numb % 2 == 0:
      # If it is true, then return True.
        return True
    # Else return False.
    else:
        return False


# Pass the above Function(Even_numbers),
# given list as arguments to the filter function that
# returns an iterator in which the items are filtered
# through a function to determine whether or not the item is accepted.
# Store it in another variable.
evn_numbrs = filter(Even_numbers, gvn_lst)
print("The Even numbers in a given list :")
# Loop in the above result using the for loop.
for n in evn_numbrs:
  # Print the iterator value of the for loop.
    print(n)

Output:

Enter some random List Elements separated by spaces = 4 6 7 9 10 12 11
The Even numbers in a given list :
4
6
10
12

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python filter() Function with Examples Read More »

Python divmod() Method with Examples

In the previous article, we have discussed Python int() Method with Examples
divmod() Method in Python:

When argument1 (dividend) is divided by argument2, the divmod() function returns a tuple containing the quotient and the remainder (divisor).

Syntax:

divmod(dividend, divisor)

Parameter Values:

dividend: It is a number. The number by which you wish to divide

divisor: It is a number. The number you wish to divide by.

Return Value:

The divmod() Function returns

  • (q, r) – a pair of numbers (a tuple) made up of the quotient q and the remainder r.
  • If dividend and divisor are integers, the result of divmod() is (q// r, dividend %divisor ).
  • If either dividend or divisor is a float, the resulting expression is (q, dividend %divisor ). In this case, q represents the entire quotient.

Examples:

Example1:

Input:

Given first number(dividend)= 3
Given second number(divisor) = 5

Output:

A Tuple containing the quotient and the remainder = (0, 3)

Example2:

Input:

Given first number(dividend) = 12.5
Given second number(divisor) = 3.5

Output:

A Tuple containing the quotient and the remainder = (3.0, 2.0)

divmod() Method with Examples in Python

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

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given first and second numbers as arguments to the divmod() function that returns a tuple containing the quotient and the remainder (divisor).
  • Print the above result i.e, a tuple containing the quotient and the remainder (divisor).
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
gvn_numb1 = 3
# Give the second number as static input and store it in another variable.
gvn_numb2 = 5
# Pass the given first and second numbers as arguments to the divmod() function
# that returns a tuple containing the quotient and the remainder (divisor).
rslt = divmod(gvn_numb1, gvn_numb2)
# Print the above result i.e, a tuple containing the quotient and the remainder
# (divisor).
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

A Tuple containing the quotient and the remainder = (0, 3)

Similarly, Check it out for other numbers

gvn_numb1 = 12.5
gvn_numb2 = 3.5
rslt = divmod(gvn_numb1, gvn_numb2)
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

A Tuple containing the quotient and the remainder = (3.0, 2.0)

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

Approach:

  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Give the second number as user input using the float(input()) function and store it in another variable.
  • Pass the given first and second numbers as arguments to the divmod() function that returns a tuple containing the quotient and the remainder (divisor).
  • Print the above result i.e, a tuple containing the quotient and the remainder (divisor).
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the float(input()) function 
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Give the second number as user input using the float(input()) function 
# and store it in another variable.
gvn_numb2 = float(input("Enter some random number = "))
# Pass the given first and second numbers as arguments to the divmod() function
# that returns a tuple containing the quotient and the remainder (divisor).
rslt = divmod(gvn_numb1, gvn_numb2)
# Print the above result i.e, a tuple containing the quotient and the remainder
# (divisor).
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

Enter some random number = 45.5
Enter some random number = 5
A Tuple containing the quotient and the remainder = (9.0, 0.5)

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python divmod() Method with Examples Read More »

Python enumerate() Function with Examples

In the previous article, we have discussed Python divmod() Method with Examples
enumerate() Function in Python:

The enumerate() function accepts a collection (for example, a tuple) and returns an enumerate object.

The enumerate() function adds a counter as the enumerate object’s key.

Syntax:

enumerate(iterable, start)

Parameter Values

iterable: It is an iterable object.

start: This is optional. It is a number. Specifying the enumerate object’s starting number. 0 is the default.

Return Value:

The method enumerate() adds a counter to an iterable and returns it. The object returned is an enumerate object.

Enumerate objects can be converted to list and tuple using the list() and tuple() methods, respectively.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']

Output:

The given list after applying enumerate() function :
[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]

Example2:

Input:

Given List = ['good', 'morning', 'btechgeeks']
Given start value = 5

Output:

The given list after applying enumerate() function from the given start :
[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]

enumerate() Function with Examples in Python

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

1)Without giving start value

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list to the enumerate() function as an argument that returns an enumerate object. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks']
# Pass the given list to the enumerate() function as an argument that returns
# an enumerate object. The enumerate() function adds a counter as the
# enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst)
print("The given list after applying enumerate() function :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

The given list after applying enumerate() function :
[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]
2)With giving start value

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number(start value) as static input and store it in another variable.
  • Pass the given list, start value to the enumerate() function as arguments that returns an enumerate object from the given start value. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['good', 'morning', 'btechgeeks']
# Give the number(start value) as static input and store it in another variable.
gvn_strt = 5
# Pass the given list, start value to the enumerate() function as the arguments
# that returns an enumerate object from the given start value. The enumerate()
# function adds a counter as the enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst, gvn_strt)
print("The given list after applying enumerate() function from the given start :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

The given list after applying enumerate() function from the given start :
[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]

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

Approach:

  • Give the list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Pass the given list to the enumerate() function as an argument that returns an enumerate object. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list to the enumerate() function as an argument that returns
# an enumerate object. The enumerate() function adds a counter as the
# enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst)
print("The given list after applying enumerate() function :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

Enter some random List Elements separated by spaces = 1000 2000 3000 4000
The given list after applying enumerate() function :
[(0, 1000), (1, 2000), (2, 3000), (3, 4000)]
2)With giving start value

Approach:

  • Give the list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Give the number(start value) as user input using the int(input()) function and store it in another variable.
  • Pass the given list, start value to the enumerate() function as arguments that returns an enumerate object from the given start value. The enumerate() function adds a counter as the enumerate object’s key.
  • Store it in another variable.
  • Convert the above result to a list using the list() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),int(),split() and map() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number(start value) as user input using the int(input()) function 
# and store it in another variable.
gvn_strt = int(input("Enter some random number = "))
# Pass the given list, start value to the enumerate() function as the arguments
# that returns an enumerate object from the given start value. The enumerate()
# function adds a counter as the enumerate object's key.
# Store it in another variable.
enumerte_lst = enumerate(gvn_lst, gvn_strt)
print("The given list after applying enumerate() function from the given start :")
# Convert the above result to a list using the list() function and print the result.
print(list(enumerte_lst))

Output:

Enter some random List Elements separated by spaces = 1 2 3 4 5
Enter some random number = 100
The given list after applying enumerate() function from the given start :
[(100, 1), (101, 2), (102, 3), (103, 4), (104, 5)]

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python enumerate() Function with Examples Read More »

Python int() Method with Examples

In the previous article, we have discussed Python id() Method with Examples
int() Method in Python:

The int() function converts the given value to an integer number.

Syntax:

int(value, base)

Parameter Values:

value: It is a number or string that can be converted into an integer number.

base: The number format is represented by a number. 10 is the default value.

Return Value:

The int() method gives:

  • An integer object created from a given number or string uses the default base of 10.
  • (No parameters) returns 0
  • (If base is specified) treats the string in the specified base (0, 2, 8, 10, 16)

Examples:

Example1:

Input:

Given Number = 35.5

Output:

The given number's{ 35.5 } Integer number =  35

Example2:

Input:

Given Value = 'D'
Given base = 16

Output:

The given value's { D } Integer number with given base{ 16 } = 13

int() Method with Examples in Python

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

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the int() function to get the given number’s Integer number.
  • Store it in another variable.
  • Print the given number’s Integer Number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 35.5
# Pass the given number as an argument to the int() function to get the
# the given numbers's Integer number.
# Store it in another variable.
rslt = int(gvn_numbr)
# Print the given number's Integer Number.
print("The given number's{", gvn_numbr,
      "} Integer number = ", rslt)

Output:

The given number's{ 35.5 } Integer number =  35
How does int() work with decimal, octal, and hexadecimal values?

Approach:

  • Give the value as static input and store it in a variable.
  • Give the base as static input and store it in another variable.
  • Pass the given value, base as the arguments to the int() function to get the Integer number of a given value.
  • Store it in another variable.
  • Print the Integer number of a given value.
  • The Exit of the program.

Below is the implementation:

# Give the value as static input and store it in a variable.
gvn_valu = '16'
# Give the base as static input and store it in another variable.
gvn_base = 8
# Pass the given value, base as the arguments to the int() function to get the
# Integer number of a given value.
# Store it in another variable.
rslt = int(gvn_valu, gvn_base)
# Print the Integer number of a given value.
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

The given value's { 16 } Integer number =  14

Similarly, check it out for other values

gvn_valu = 'D'
gvn_base = 16
rslt = int(gvn_valu, gvn_base)
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

The given value's { D } Integer number =  13

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.
  • Pass the given number as an argument to the int() function to get the given number’s Integer number.
  • Store it in another variable.
  • Print the given number’s Integer Number.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numbr = float(input("Enter some random number = "))
# Pass the given number as an argument to the int() function to get the
# the given numbers's Integer number.
# Store it in another variable.
rslt = int(gvn_numbr)
# Print the given number's Integer Number.
print("The given number's{", gvn_numbr,
      "} Integer number = ", rslt)

Output:

Enter some random number = 250
The given number's{ 250.0 } Integer number = 250
How does int() work with decimal, octal, and hexadecimal values?

Approach:

  • Give the value as user input using the input() function and store it in a variable.
  • Give the base as user input using the int(input()) function and store it in another variable.
  • Pass the given value, base as the arguments to the int() function to get the Integer number of a given value.
  • Store it in another variable.
  • Print the Integer number of a given value.
  • The Exit of the program.

Below is the implementation:

# Give the value as user input using the input() function and store it in a variable.
gvn_valu = input("Enter some random value = ")
# Give the base as user input using the int(input()) function and store it in another variable.
gvn_base = int(input("Enter some random number = "))
# Pass the given value, base as the arguments to the int() function to get the
# Integer number of a given value.
# Store it in another variable.
rslt = int(gvn_valu, gvn_base)
# Print the Integer number of a given value.
print("The given value's {", gvn_valu,
      "} Integer number = ", rslt)

Output:

Enter some random value = B
Enter some random number = 16
The given value's { B } Integer number = 11

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python int() Method with Examples Read More »

Python id() Method with Examples

In the previous article, we have discussed Python hex() Method with Examples
id() Method in Python:

The id() function generates a unique id for the specified object.

In Python, each object has its own unique id.

When an object is created, it is given an id.

The id is the memory address of the object, and it will be different each time you run the program. (Except for objects with a fixed unique id, such as integers ranging from -5 to 256)

Note: The integer has a unique id. Throughout the lifetime, the integer id remains constant.

Syntax:

id(object)

Parameters

object: Any object, such as a string, number, list, or class.

Return Value:

The id() function returns the object’s identity. This is a unique integer for the given object that remains constant throughout its lifetime.

Examples:

Example1:

Input:

Given number = 9
Given string = "btechgeeks"
Given list = ["hello", 123, "this", "is", "btechgeeks"]

Output:

The id of the given number =  11094560
The id of the given string =  140697796571568
The id of the given list =  140697796536328

Example2:

Input:

Given number = 10.5
Given string = "good morning btechgeeks"
Given list =  [1, 3, 5, 2]

Output:

The id of the given number =  140002720203304
The id of the given string =  140002691658064
The id of the given list =  140002691627016

id() Method with Examples 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 list as static input and store it in another variable.
  • Give the string as static input and store it in another variable.
  • Pass the given number as an argument to the id() function that generates a unique id for the given number.
  • Store it in another variable.
  • Pass the given string as an argument to the id() function that generates a unique id for the given string.
  • Store it in another variable.
  • Pass the given list as an argument to the id() function that generates a unique id for the given list.
  • Store it in another variable.
  • Print the id of the given number.
  • Print the id of the given string.
  • Print the id of the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 9
# Give the list as static input and store it in another variable.
gvn_lst = ["hello", 123, "this", "is", "btechgeeks"]
# Give the string as static input and store it in another variable.
gvn_str = "btechgeeks"
# Pass the given number as an argument to the id() function that generates a
# unique id for the given number.
# Store it in another variable.
numbr_id = id(gvn_numbr)
# Pass the given string as an argument to the id() function that generates a
# unique id for the given string.
# Store it in another variable.
str_id = id(gvn_str)
# Pass the given list as an argument to the id() function that generates a
# unique id for the given list.
# Store it in another variable.
lst_id = id(gvn_lst)
# Print the id of the given number.
print("The id of the given number = ", numbr_id)
# Print the id of the given string.
print("The id of the given string = ", str_id)
# Print the id of the given list.
print("The id of the given list = ", lst_id)

Output:

The id of the given number =  11094560
The id of the given string =  139634246537648
The id of the given list =  139634246502408
Example of id() for a class:
class number:
    numb = 6


numb_id = number()
print('The id of given number =', id(numb_id))

Output:

The id of given number = 140125486982872

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.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Pass the given number as an argument to the id() function that generates a unique id for the given number.
  • Store it in another variable.
  • Pass the given string as an argument to the id() function that generates a unique id for the given string.
  • Store it in another variable.
  • Pass the given list as an argument to the id() function that generates a unique id for the given list.
  • Store it in another variable.
  • Print the id of the given number.
  • Print the id of the given string.
  • Print the id of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numbr = float(input("Enter some random number = "))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the string as user input using the input() function and store it in another variable.
gvn_str = input("Enter some random string = ")
# Pass the given number as an argument to the id() function that generates a
# unique id for the given number.
# Store it in another variable.
numbr_id = id(gvn_numbr)
# Pass the given string as an argument to the id() function that generates a
# unique id for the given string.
# Store it in another variable.
str_id = id(gvn_str)
# Pass the given list as an argument to the id() function that generates a
# unique id for the given list.
# Store it in another variable.
lst_id = id(gvn_lst)
# Print the id of the given number.
print("The id of the given number = ", numbr_id)
# Print the id of the given string.
print("The id of the given string = ", str_id)
# Print the id of the given list.
print("The id of the given list = ", lst_id)

Output:

Enter some random number = 10.52
Enter some random List Elements separated by spaces = 12 45 6 9 1
Enter some random string = good morning btechgeeks
The id of the given number = 139861816847568
The id of the given string = 139861816051792
The id of the given list = 139861816050144

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python id() Method with Examples Read More »