Author name: Vikram Chiluka

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 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 »

Python hex() Method with Examples

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

The hex() function converts the given number to a hexadecimal value.

The prefix 0x is always present in the returned string.

Syntax:

hex(number)

Parameters

The hex() function only accepts one argument.

x – integer number (either an int object or a __index__() method that returns an integer)

Return Value:

The hex() function converts an integer to its corresponding hexadecimal number and returns it as a string.

The hexadecimal string returned begins with the prefix 0x, indicating that it is in hexadecimal form.

Examples:

Example1:

Input:

Given Number = 20

Output:

The given number's{ 20 } Hexadecimal Value =  0x14
The hex() function Return Type = <class 'str'>

Note:

The prefix 0x denotes that the outcome is a Hexadecimal string.

Example2:

Input:

Given Number = 4.5

Output:

The given float number's{ 4.5 } Hexadecimal Value =  0x1.2000000000000p+2

hex() Method with Examples in Python

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

1)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the hex() function to get the hexadecimal value of a given number.
  • Store it in another variable.
  • Print the given number’s Hexadecimal Value.
  • Print the Return Type of hex() Function using the type() method by passing the hex(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 = 20
# Pass the given number as an argument to the hex() function to get the
# hexadecimal value of a given number.
# Store it in another variable.
hexadeciml_valu = hex(gvn_numbr)
# Print the given number's Hexadecimal Value.
print("The given number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)
# Print the return Type of hex() Function using the type() method by passing
# the hex(given number) as an argument to it.
print("The hex() function Return Type =", type(hex(gvn_numbr)))

Output:

The given number's{ 20 } Hexadecimal Value =  0x14
The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the hex() function and apply float to the result to get the hexadecimal value of a given float number.
  • Store it in another variable.
  • Print the given float number’s Hexadecimal Value.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 4.5
# Pass the given number as an argument to the hex() function and apply float
# to the result to get the hexadecimal value of a given float number.
# Store it in another variable.
hexadeciml_valu = float.hex(gvn_numbr)
# Print the given float number's Hexadecimal Value.
print("The given float number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)

Output:

The given float number's{ 4.5 } Hexadecimal Value =  0x1.2000000000000p+2

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 hex() function to get the hexadecimal value of a given number.
  • Store it in another variable.
  • Print the given number’s Hexadecimal Value.
  • Print the Return Type of hex() Function using the type() method by passing the hex(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 hex() function to get the
# hexadecimal value of a given number.
# Store it in another variable.
hexadeciml_valu = hex(gvn_numbr)
# Print the given number's Hexadecimal Value.
print("The given number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)
# Print the return Type of hex() Function using the type() method by passing
# the hex(given number) as an argument to it.
print("The hex() function Return Type =", type(hex(gvn_numbr)))

Output:

Enter some random number = 50
The given number's{ 50 } Hexadecimal Value = 0x32
The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number

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 hex() function and apply float to the result to get the hexadecimal value of a given float number.
  • Store it in another variable.
  • Print the given float number’s Hexadecimal Value.
  • 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 hex() function and apply float
# to the result to get the hexadecimal value of a given float number.
# Store it in another variable.
hexadeciml_valu = float.hex(gvn_numbr)
# Print the given float number's Hexadecimal Value.
print("The given float number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)

Output:

Enter some random number = 7.5
The given float number's{ 7.5 } Hexadecimal Value = 0x1.e000000000000p+2

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 hex() Method with Examples Read More »

Python float() Method with Examples

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

The float() function returns a floating-point number from the specified value.

Syntax:

float(value)

Parameter Values:

value: A number or string that can be converted to a floating-point number.

Return Value:

The float() method yields:

  • If an argument is passed, the equivalent floating-point number is returned.
  • If no arguments are passed, the value is 0.0;
  • Otherwise, an OverflowError exception is thrown if the argument is outside the range of Python float.

Examples:

Example1:

Input:

Given Number = 14

Output:

The given number's{ 14 } Floating-point number =  14.0

Example2:

Input:

Given Number = "-25.431" (# given in string format)

Output:

The given number's{ -25.431 } Floating-point number =  -25.431

float() 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 float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 14
# Pass the given number as an argument to the float() function to get the
# the floating-point number of a given number.
# Store it in another variable.
rslt = float(gvn_numbr)
# Print the floating-point number of a given number.
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ 14 } Floating-point number =  14.0

Similarly, check it out for other values and print the result

gvn_numbr = "-25.456"
rslt = float(gvn_numbr)
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ -25.456 } Floating-point number =  -25.456
float() for infinity and Nan(Not a number) values
print(float("InFiNiTy"))
print(float("infinity"))
print(float("nan"))
print(float("NaN"))
print(float("inf"))
print(float("InF"))

Output:

inf
inf
nan
nan
inf
inf

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

Approach:

  • Give the number as a string as user input using the input() function and store it in a variable.
  • Pass the given number as an argument to the float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

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

Output:

Enter some random number = -19
The given number's{ -19 } Floating-point number = -19.0

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 float() Method with Examples Read More »

Python complex() Method with Examples

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

By specifying a real number and an imaginary number, the complex() function returns a complex number.

Syntax:

complex(real, imaginary)

Parameters

real: This is Required. A number that represents the complex number’s real part. 0 is the default. The real number can also be a String, such as ‘2+6j’; in this case, the second parameter should be omitted.

imaginary: This is Optional. A number that represents the complex number’s imaginary part. 0 is the default.

Return Value:

The complex() method, as the name implies, returns a complex number.

The ValueError exception is thrown if the string passed to this method is not a valid complex number.

Note: Complex() always expects a string of the form real+imaginaryj or real+imaginaryJ.

Examples:

Example1:

Input:

real number = 2
imaginary number = 4

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Example2:

Input:

real number = 3

Output:

The complex number for the given real part{ 3 }=  (3+0j)

complex() Method with Examples in Python

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

Approach:

  • Give the number(real part) as static input and store it in a variable.
  • Give the other number (imaginary part) as static input and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as static input and store it in a variable.
real_numbr = 2
# Give the other number (imaginary part) as static input and store it in
# another variable.
imaginry_numbr = 4
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Similarly, check for the other values

without giving imaginary part

real_numbr = 3
rslt = complex(real_numbr)
print(
    "The complex number for the given real part{", real_numbr, "}= ", rslt)

Output:

The complex number for the given real part{ 3 }=  (3+0j)

without giving any parameters

rslt = complex()
print("The complex number without giving any parameters = ", rslt)

Output:

The complex number without giving any parameters =  0j

When a number is given as a string?

gvn_numbr = "7+8j"
rslt = complex(gvn_numbr)
print(
    "The complex number for the given number {", gvn_numbr, "} = ", rslt)

Output:

The complex number for the given number { 7+8j } =  (7+8j)

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

Approach:

  • Give the number(real part) as user input using the int(input()) function and store it in a variable.
  • Give the other number (imaginary part) as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as user input using the int(input()) function and store it in a variable.
real_numbr = int(input("Enter some random number = "))
# Give the other number (imaginary part) as user input using the int(input()) function 
# and store it in another variable.
imaginry_numbr = int(input("Enter some random number = "))
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

Enter some random number = 5
Enter some random number = 6
The complex number for the given real and imaginary numbers{ 5 , 6 }= (5+6j)

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 complex() Method with Examples Read More »

Python List index() Method with Examples

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

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List index() Method in Python:

The index() method returns the position in the given list of the given element.

Syntax:

gvnlist.index(element, start, end)

Parameter Values: 

The list index() method can accept up to three arguments:

element – the to-be-searched element

start (optional) – Begin searching from this index

end (optional) – Search the element all the way up to this index

Return Value:

The index() method returns the index of the specified list element.
A ValueError exception is thrown if the element is not found.

Note:

Only the first occurrence of the matching element is returned by the index() method.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'python', 'programs']
Given Element = 'this'

Output:

The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
1

Example2:

Input:

Given List = ['hello', 'this', 'is', 'python', 'programs']
Given Element = 'btechgeeks'

Output:

Traceback (most recent call last):
  File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module>
    resltind = gvnlst.index(gvnele)
ValueError: 'btechgeeks' is not in list

List index() Method with Examples in Python

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

Example-1: If the element is present in the given list

Approach:

  • Give the list as the static input and store it in a variable.
  • Give the element whose index must be located in the given list and store it in another variable.
  • Pass the given element to the index function for the given list and store the result in resltind variable.
  • Print the resltind value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'this'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
1
Example-2: If the element is not present in the given list

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'btechgeeks'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module>
    resltind = gvnlst.index(gvnele)
ValueError: 'btechgeeks' is not in list
Example-3: Giving Start Index

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'python'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
# searching the element after 1 st index by passing second argument as 1
resltind = gvnlst.index(gvnele, 1)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

The given element { python } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
3
Example-4: Giving Start and End Index

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'python'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
# searching the element after 0 st index to 2 index  by passing
# second argument as 0 and third argument as 2
resltind = gvnlst.index(gvnele, 0, 2)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/e3f1e4ebf5b32326c6525257b0b59752.py", line 10, in <module>
    resltind = gvnlst.index(gvnele, 0, 2)
ValueError: 'python' is not in list

Explanation:

The element python is present at the third index but not between the indices, resulting in an error.

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

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element whose index must be located in the given list as user input using the input() function and store it in another variable.
  • Pass the given element to the index function for the given list and store the result in resltind variable.
  • Print the resltind value.
  • 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.
gvnlst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = int(input('Enter some random element = '))
# Pass the given element to the index function for the given list
# and store the result in resltind variable.

resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Enter some random List Elements separated by spaces = 6 2 8 9 1 8 4
Enter some random element = 8
The given element { 8 } in the given list [6, 2, 8, 9, 1, 8, 4] is present at the index :
2

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.

Python List index() Method with Examples Read More »

Python bin() Method with Examples

In the previous article, we have discussed Python String startswith() Method with Examples
Binary Number System: The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).

bin() Method in Python:

The bin() method converts and returns a given integer’s binary equivalent string. If the parameter is not an integer, the __index__() method must be used to return an integer.

Syntax:

bin(number)

Parameters

The bin() method accepts a single parameter:

number – An integer number for which the binary equivalent must be calculated.
If the value is not an integer, the __index__() method should be used to return an integer.

Return Value: 

The binary string equivalent to the given integer is returned by the bin() method.

If an integer is not specified, a TypeError exception is thrown, indicating that the type cannot be interpreted as an integer.

Examples:

Example1:

Input:

Given Number = 2

Output:

The given number's{ 2 } binary equiavalent string =  0b10

Note:

The prefix 0b denotes that the outcome is a binary string.

Example2:

Input:

Given Number = 10

Output:

The given number's{ 10 } binary equiavalent string =  0b1010

bin() 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 bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 2
# Pass the given number as an argument to the bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

The given number's{ 2 } binary equiavalent string =  0b10

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 bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • 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 bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

Enter some random number = 12
The given number's{ 12 } binary equiavalent string = 0b1100

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 bin() Method with Examples Read More »

Python String startswith() Method with Examples

In the previous article, we have discussed Python String replace() Method with Examples
String startswith() Method in Python:

If the string begins with the specified value, the startswith() method returns True; otherwise, it returns False.

Syntax:

string.startswith(value, start, end)

Parameters

value: This is Required. The value used to determine whether the string starts with

start: This is optional. It is an integer. The starting point for the search.

end: This is optional. It is an integer. The ending point for the search.

Return Value:

The method startswith() returns a boolean value.

  • If the string begins with the specified value, it returns True.
  • If the string does not begin with the specified value, it returns False.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks"
Given value = "Hello"

Output:

Checking if the given string startswith{ Hello } or not =  False

Example2:

Input:

Given string = "welcome to python programs"
Given value = "t"
start position = 8
end position = 13

Output:

Checking if the given string startswith{ t } or not =  True

String startswith() Method with Examples in Python

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

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"
# Give the value as static input and store it in another variable.
gvn_valu = "hello"
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ hello } or not =  True
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "t"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 8
# Give the end position as static input and store it in another variable.
gvn_endpositn = 13
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ t } or not =  True

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

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • 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 = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = hello this is btechgeeks
Enter some Random String(value) = Hello
Checking if the given string startswith{ Hello } or not = False
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the 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.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • 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 = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# 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 = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = welcome to python programs
Enter some Random String(value) = to
Enter some random number = 8
Enter some random number = 14
Checking if the given string startswith{ to } or not = True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String startswith() Method with Examples Read More »

Python String replace() Method with Examples

In the previous article, we have discussed Python String upper() Method with Examples
String replace() Method in Python:

The replace() method replaces a phrase specified with another phrase specified.

Syntax:

string.replace(oldvalue, newvalue, count)

Note: If nothing else is specified, all occurrences of the specified phrase will be replaced.

Parameters

oldvalue: This is Required. The string to look for.

newvalue: This is Required. The string will be used to replace the old value.

count: This is optional. A number indicating how many occurrences of the old value should be replaced. The default value is all occurrences.

Return Value:

The replace() method returns a copy of the string that replaces the old value with the new value. The original string remains unaltered.

If the old substring cannot be found, the copy of the original string is returned.

Examples:

Example1:

Input:

Given String = "hello this is hello btechgeeks hello "
Given old value = "hello"
Given new value = "hai"

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hai

Example2:

Input:

Given String = "welcome to to python to to programs"
Given old value = "to"
Given new value = "all"
Given count = 3

Output:

The given string after replacing oldvalue{ to } with new value{ all } is:
welcome all all python all to programs

String replace() Method with Examples in Python

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

1)Without giving count Value

Approach:

  • Give the string as static input and store it in a variable.
  • Give the old value as static input and store it in another variable.
  • Give the new value as static input and store it in another variable.
  • Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_fststr = "hello this is hello btechgeeks hello "
# Give the old value as static input and store it in another variable.
old_valu = "hello"
# Give the new value as static input and store it in another variable.
new_valu = "hai"
# Pass the given old value, new value as arguments to the replace() function
# for the given string to replace the old value with a new value
# in a given string.
# Store it in another variable.
rslt = gvn_fststr.replace(old_valu, new_valu)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hai
2)With giving count Value

Approach:

  • Give the string as static input and store it in a variable.
  • Give the old value as static input and store it in another variable.
  • Give the new value as static input and store it in another variable.
  • Give the count as static input and store it in another variable.
  • Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_fststr = "hello this is hello btechgeeks hello "
# Give the old value as static input and store it in another variable.
old_valu = "hello"
# Give the new value as static input and store it in another variable.
new_valu = "hai"
# Give the count as static input and store it in another variable.
gvn_cnt = 2
# Pass the given old value, new value, count as arguments to the replace()
# function for the given string to replace the old value with a new value
# in a given string. Here count is a number indicating how many occurrences
# of the old value should be replaced.
# Store it in another variable.
rslt = gvn_fststr.replace(old_valu, new_valu, gvn_cnt)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hello

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

1)Without giving count Value

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the old value as user input using the input() function and store it in another variable.
  • Give the new value as user input using the input() function and store it in another variable.
  • Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • 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 = ")
# Give the old value as user input using the input() function and store it in another variable.
old_valu = input("Enter some Random String(oldvalue) = ")
# Give the new value as user input using the input() function and store it in another variable.
new_valu = input("Enter some Random String(newvalue) = ")
# Pass the given old value, new value as arguments to the replace() function
# for the given string to replace the old value with a new value
# in a given string.
# Store it in another variable.
rslt = gvn_str.replace(old_valu, new_valu)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

Enter some Random String = good morning btechgeeks good good morning
Enter some Random String(oldvalue) = good
Enter some Random String(newvalue) = bad
The given string after replacing oldvalue{ good } with new value{ bad } is:
bad morning btechgeeks bad bad morning
2)With giving count Value

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the old value as user input using the input() function and store it in another variable.
  • Give the new value as user input using the input() function and store it in another variable.
  • Give the count as user input using the int(input()) function and store it in another variable.
  • Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • 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 = ")
# Give the old value as user input using the input() function and store it in another variable.
old_valu = input("Enter some Random String(oldvalue) = ")
# Give the new value as user input using the input() function and store it in another variable.
new_valu = input("Enter some Random String(newvalue) = ")
# Give count as user input using the int(input()) function and 
# store it in another variable.
gvn_cnt = int(input("Enter some random number = "))
# Pass the given old value, new value, count as arguments to the replace()
# function for the given string to replace the old value with a new value
# in a given string. Here count is a number indicating how many occurrences
# of the old value should be replaced.
# Store it in another variable.
rslt = gvn_str.replace(old_valu, new_valu, gvn_cnt)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

Enter some Random String = welcome to to python to to programs
Enter some Random String(oldvalue) = to
Enter some Random String(newvalue) = all
Enter some random number = 3
The given string after replacing oldvalue{ to } with new value{ all } is:
welcome all all python all to programs

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String replace() Method with Examples Read More »