Python

Python statistics.mean() Method with Examples

statistics.mean() Method in Python:

The statistics. mean() method computes the mean (average) of a data set given.

mean: To calculate the mean, add all of the given values and divide by the number of values.

for example

let list = [5, 10, 15, 20]

mean= 5+10+15+20/4 = 12.5

Syntax:

statistics.mean(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value that represents the mean of the given data.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The mean of the given list items [10, 20, 40, 15, 30]  =  23

Example2:

Input:

Given list = [-1, -2, 6, 7, 4, 9]

Output:

The mean of the given list items [-1, -2, 6, 7, 4, 9] = 3.8333333333333335

statistics.mean() Method with Examples in Python

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

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.mean() method that computes the mean (average) of the given list items.
  • Store it in another variable.
  • Print the mean (average) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.mean() method that
# computes the mean (average) of the given list items.
# Store it in another variable.
rslt = statistics.mean(gvn_lst)
# Print the mean (average) of the given list items.
print("The mean of the given list items", gvn_lst, " = ", rslt)

Output:

The mean of the given list items [10, 20, 40, 15, 30]  =  23

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

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.mean() method that computes the mean (average) of the given list items.
  • Store it in another variable.
  • Print the mean (average) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# 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 statistics.mean() method that
# computes the mean (average) of the given list items.
# Store it in another variable.
rslt = statistics.mean(gvn_lst)
# Print the mean (average) of the given list items.
print("The mean of the given list items", gvn_lst, " = ", rslt)

Output:

Enter some random List Elements separated by spaces = -1 -2 6 7 4 9
The mean of the given list items [-1, -2, 6, 7, 4, 9] = 3.8333333333333335

Python statistics.mean() Method with Examples Read More »

Python statistics.harmonic_mean() Method with Examples

statistics.harmonic_mean() Method:

The statistics.harmonic_mean() method determines the harmonic mean (central location) of a given data set.

Harmonic mean = The reciprocal of the arithmetic mean() of the data’s reciprocals.

The harmonic mean is computed in the following way:

If you have four values (a, b, c, and d), the result is 4 / (1/a + 1/b + 1/c + 1/d).

Syntax:

statistics.harmonic_mean(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note:

  • Negative values are not permitted.
  • If the data is null or empty, it throws a StatisticsError.

Return Value:

Returns a float value corresponding to the harmonic mean of the given data.

Examples:

Example1:

Input:

Given list = [30, 50, 100]

Output:

The harmonic mean of the given list items [30, 50, 100]  =  47.36842105263158

Example2:

Input:

Given list = [20, 40, 60, 80]

Output:

The harmonic mean of the given list items [20, 40, 60, 80] = 38.4

statistics.harmonic_mean() Method with Examples in Python

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

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.harmonic_mean() method that determines the harmonic mean (central location) of a given data set (given list items).
  • Store it in another variable.
  • Print the harmonic mean (central location) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [30, 50, 100]
# Pass the given list as an argument to the statistics.harmonic_mean() method
# that determines the harmonic mean (central location) of a given
# data set (given list items).
# Store it in another variable.
rslt = statistics.harmonic_mean(gvn_lst)
# Print the harmonic mean (central location) of the given list items.
print("The harmonic mean of the given list items", gvn_lst, " = ", rslt)

Output:

The harmonic mean of the given list items [30, 50, 100]  =  47.36842105263158

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

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.harmonic_mean() method that determines the harmonic mean (central location) of a given data set (given list items).
  • Store it in another variable.
  • Print the harmonic mean (central location) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# 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 statistics.harmonic_mean() method
# that determines the harmonic mean (central location) of a given
# data set (given list items).
# Store it in another variable.
rslt = statistics.harmonic_mean(gvn_lst)
# Print the harmonic mean (central location) of the given list items.
print("The harmonic mean of the given list items", gvn_lst, " = ", rslt)

Output:

Enter some random List Elements separated by spaces = 20 40 60 80 
The harmonic mean of the given list items [20, 40, 60, 80] = 38.4

Python statistics.harmonic_mean() Method with Examples Read More »

Python type() Function with Examples

type() Function in Python:

The type() function returns the type of the object that was passed to it.

Based on the arguments passed, the type() function either returns the type of the object or returns a new type object.

Syntax:

type(object, base, dict)

Parameters

object: This is required. The type() function returns the type of this object if only one parameter is specified.

base: This is optional. It Describes or specifies the base classes.

dict: This is optional. Specifies the namespace containing the class definition.

Return Value:

This function returns

  • If only one object parameter is passed, the type of the object is returned.
  • Returns a new type, if three parameters are passed.

Examples:

Example1:

Input:

Given number = 30
Given string = "hello this is btechgeeks"
Given list = [1, 4, 5, 6]

Output:

The type of given number =  <class 'int'>
The type of given string =  <class 'str'>
The type of given list =  <class 'list'>

type() 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.
  • Give the string as static input and store it in another variable.
  • Give the list as static input and store it in another variable.
  • Pass the given number as an argument to the type() function that returns the type of the object that was passed to it.
  • Store it in another variable.
  • Print the result after applying the type() function on the given number.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 30
# Give the string as static input and store it in another variable.
gvn_str = "hello this is btechgeeks"
# Give the list as static input and store it in another variable.
gvn_lst = [1, 4, 5, 6]
# Pass the given number as an argument to the type() function that returns the 
# type of the object that was passed to it.
# Store it in another variable.
rslt1 = type(gvn_numb)
# Print the result after applying the type() function on the given number.
print("The type of given number = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = type(gvn_str)
print("The type of given string = ", rslt2)
rslt3 = type(gvn_lst)
print("The type of given list = ", rslt3)

Output:

The type of given number =  <class 'int'>
The type of given string =  <class 'str'>
The type of given list =  <class 'list'>

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 string as user input using the input() function and store it in another variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given number as an argument to the type() function that returns the type of the object that was passed to it.
  • Store it in another variable.
  • Print the result after applying the type() function on the given number.
  • Similarly, do the same for the given string and list and print it.
  • 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_numb = float(input("Enter some random number = "))
# Give the string as user input using the input() function and 
# store it in another variable.
gvn_str = input("Enter some random string = ")
# 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 number as an argument to the type() function that returns the 
# type of the object that was passed to it.
# Store it in another variable.
rslt1 = type(gvn_numb)
# Print the result after applying the type() function on the given number.
print("The type of given number = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = type(gvn_str)
print("The type of given string = ", rslt2)
rslt3 = type(gvn_lst)
print("The type of given list = ", rslt3)

Output:

Enter some random number = 50.5
Enter some random string = good morning btechgeeks
Enter some random List Elements separated by spaces = 3 2 4 1
The type of given number = <class 'float'>
The type of given string = <class 'str'>
The type of given list = <class 'list'>

 

Python type() Function with Examples Read More »

Python set() Function with Examples

set() Function in Python:

In Python, the set() function creates a set.

Note: set doesn’t allow duplicate elements and arranges in random order.

Syntax:

set(iterable)

Parameters

set() accepts only one optional parameter:

iterable: a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set.

Return Value:

The set() function returns:

  • If no parameters are passed, an empty set is returned.
  • Otherwise, a set constructed from the given iterable parameter is returned.

Examples:

Example1:

Input:

Given tuple = (3, 4, 3, 3, 2, 1, 1)
Given string = "btechgeeks"
Given list = [10, 20, 30, 10, 20, 40]

Output:

The result after applying set() function on the given tuple =  {1, 2, 3, 4}
The result after applying set() function on the given string =  {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}
The result after applying set() function on the given list =  {40, 10, 20, 30}

Example2:

Input:

Given tuple = (25, 35, 45)
Given string = "good"
Given list = [1, 3, 5, 6, 1]

Output:

The result after applying set() function on the given tuple =  {25, 35, 45}
The result after applying set() function on the given string =  {'o', 'g', 'd'}
The result after applying set() function on the given list =  {1, 3, 5, 6}

set() Function with Examples in Python

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

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the string as static input and store it in another variable.
  • Give the list as static input and store it in another variable.
  • Pass the given tuple as an argument to the set() function that converts a given tuple to a set and returns a set.
  • Store it in another variable.
  • Print the result list after applying the set() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvn_tupl = (3, 4, 3, 3, 2, 1, 1)
# Give the string as static input and store it in another variable.
gvn_str = "btechgeeks"
# Give the list as static input and store it in another variable.
gvn_list = [10, 20, 30, 10, 20, 40]
# Pass the given tuple as an argument to the set() function that converts a given
# tuple to a set and returns a set.
# Store it in another variable.
rslt1 = set(gvn_tupl)
# Print the result list after applying set() function on the given tuple.
print("The result after applying set() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = set(gvn_str)
print("The result after applying set() function on the given string = ", rslt2)
rslt3 = set(gvn_list)
print("The result after applying set() function on the given list = ", rslt3)

Output:

The result after applying set() function on the given tuple =  {1, 2, 3, 4}
The result after applying set() function on the given string =  {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}
The result after applying set() function on the given list =  {40, 10, 20, 30}

Tip: Similarly, you can also try for the set, dictionaries.

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

Approach:

  • Give the tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given tuple as an argument to the set() function that converts a given tuple to a set and returns a set.
  • Store it in another variable.
  • Print the result list after applying the set() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random Tuple 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 = ")
# 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 tuple as an argument to the set() function that converts a given
# tuple to a set and returns a set.
# Store it in another variable.
rslt1 = set(gvn_tupl)
# Print the result list after applying set() function on the given tuple.
print("The result after applying set() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = set(gvn_str)
print("The result after applying set() function on the given string = ", rslt2)
rslt3 = set(gvn_lst)
print("The result after applying set() function on the given list = ", rslt3)

Output:

Enter some random Tuple Elements separated by spaces = 3 4 2
Enter some random string = good
Enter some random List Elements separated by spaces = 6 6 7 8 8
The result after applying set() function on the given tuple = {2, 3, 4}
The result after applying set() function on the given string = {'d', 'g', 'o'}
The result after applying set() function on the given list = {8, 6, 7}

Python set() Function with Examples Read More »

Python tuple() Function with Examples

tuple() Function in Python:

To create tuples in Python, use the tuple() built-in function.

A tuple is an immutable sequence type in Python. The tuple() construct is one method for generating tuples.

Syntax:

tuple(iterable)

Parameters

iterable: This is optional.An iterable (list, range, etc.) or an iterator object

  • If tuple() is not passed an iterable, the function returns an empty tuple.

Return Value: This function returns a tuple.

Examples:

Example1:

Input:

Given list = [-1, 3, 5, 6]
Given string = "good"
Given tuple = (10, 35, 45)

Output:

The result after applying tuple() function on the given list =  (-1, 3, 5, 6)
The result after applying tuple() function on the given string =  ('g', 'o', 'o', 'd')
The result after applying tuple() function on the given tuple =  (10, 35, 45)

Example2:

Input:

Given list = [10, 20, 30, 40]
Given string = "hello"
Given tuple = (1, 2, 3, 4, 5)

Output:

The result after applying tuple() function on the given list =  (10, 20, 30, 40)
The result after applying tuple() function on the given string =  ('h', 'e', 'l', 'l', 'o')
The result after applying tuple() function on the given tuple =  (1, 2, 3, 4, 5)

tuple() 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.
  • Give the string as static input and store it in another variable.
  • Give the tuple as static input and store it in another variable.
  • Pass the given list as an argument to the tuple () function that converts a given list to a tuple and returns a tuple.
  • Store it in another variable.
  • Print the result tuple after applying the tuple() function on the given list.
  • Similarly, do the same for the given string and tuple and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [-1, 3, 5, 6]
# Give the string as static input and store it in another variable.
gvn_str = "good"
# Give the tuple as static input and store it in another variable.
gvn_tupl = (10, 35, 45)
# Pass the given list as an argument to the tuple() function that converts a given
# list to a tuple and returns a tuple.
# Store it in another variable.
rslt1 = tuple(gvn_lst)
# Print the result tuple after applying tuple() function on the given list.
print("The result after applying tuple() function on the given list = ", rslt1)
# similarly do the same for the given string and tuple and print it.
rslt2 = tuple(gvn_str)
print("The result after applying tuple() function on the given string = ", rslt2)
rslt3 = tuple(gvn_tupl)
print("The result after applying tuple() function on the given tuple = ", rslt3)

Output:

The result after applying tuple() function on the given list =  (-1, 3, 5, 6)
The result after applying tuple() function on the given string =  ('g', 'o', 'o', 'd')
The result after applying tuple() function on the given tuple =  (10, 35, 45)

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 string as user input using the input() function and store it in another variable.
  • Give the tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the tuple () function that converts a given list to a tuple and returns a tuple.
  • Store it in another variable.
  • Print the result tuple after applying the tuple() function on the given list.
  • Similarly, do the same for the given string and tuple and print it.
  • 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()))
# Give the string as user input using the input() function and 
# store it in another variable.
gvn_str = input("Enter some random string = ")
# Give the tuple as user input using tuple(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random Tuple Elements separated by spaces = ').split()))
# Pass the given list as an argument to the tuple() function that converts a given
# list to a tuple and returns a tuple.
# Store it in another variable.
rslt1 = tuple(gvn_lst)
# Print the result tuple after applying tuple() function on the given list.
print("The result after applying tuple() function on the given list = ", rslt1)
# similarly do the same for the given string and tuple and print it.
rslt2 = tuple(gvn_str)
print("The result after applying tuple() function on the given string = ", rslt2)
rslt3 = tuple(gvn_tupl)
print("The result after applying tuple() function on the given tuple = ", rslt3)

Output:

Enter some random List Elements separated by spaces = 23 45 6 1
Enter some random string = hello
Enter some random Tuple Elements separated by spaces = 3 6 7 9
The result after applying tuple() function on the given list = (23, 45, 6, 1)
The result after applying tuple() function on the given string = ('h', 'e', 'l', 'l', 'o')
The result after applying tuple() function on the given tuple = (3, 6, 7, 9)

Python tuple() Function with Examples Read More »

Python list() Function with Examples

list() Function in Python:

The list() function is used to create a list object.

A list object is an ordered and changeable collection.

Syntax:

list(iterable)

Parameters

iterable: This is required. It could be a sequence, collection, or iterator object.

Return Value:

It returns a list.

  • It returns an empty list if no parameters are passed.
  • When iterable is passed as a parameter, it generates a list of iterable’s items.

Examples:

Example1:

Input:

Given tuple = (1, 2, 3, 4, 5)
Given string = "hello"
Given list = [10, 20, 30, 40]

Output:

The result after applying list() function on the given tuple =  [1, 2, 3, 4, 5]
The result after applying list() function on the given string =  ['h', 'e', 'l', 'l', 'o']
The result after applying list() function on the given list =  [10, 20, 30, 40]

Example2:

Input:

Given tuple = (25, 35, 45)
Given string = "good"
Given list = [1, 3, 5, 6]

Output:

The result after applying list() function on the given tuple =  [25, 35, 45]
The result after applying list() function on the given string =  ['g', 'o', 'o', 'd']
The result after applying list() function on the given list =  [1, 3, 5, 6]

list() Function with Examples in Python

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

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the string as static input and store it in another variable.
  • Give the list as static input and store it in another variable.
  • Pass the given tuple as an argument to the list() function that converts a given tuple to a list and returns a list.
  • Store it in another variable.
  • Print the result list after applying the list() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvn_tupl = (1, 2, 3, 4, 5)
# Give the string as static input and store it in another variable.
gvn_str = "hello"
# Give the list as static input and store it in another variable.
gvn_list = [10, 20, 30, 40]
# Pass the given tuple as an argument to the list() function that converts a given
# tuple to a list and returns a list.
# Store it in another variable.
rslt1 = list(gvn_tupl)
# Print the result list after applying list() function on the given tuple.
print("The result after applying list() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = list(gvn_str)
print("The result after applying list() function on the given string = ", rslt2)
rslt3 = list(gvn_list)
print("The result after applying list() function on the given list = ", rslt3)

Output:

The result after applying list() function on the given tuple =  [1, 2, 3, 4, 5]
The result after applying list() function on the given string =  ['h', 'e', 'l', 'l', 'o']
The result after applying list() function on the given list =  [10, 20, 30, 40]

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

Approach:

  • Give the tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given tuple as an argument to the list() function that converts a given tuple to a list and returns a list.
  • Store it in another variable.
  • Print the result list after applying the list() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random Tuple 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 = ")
# 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 tuple as an argument to the list() function that converts a given
# tuple to list and returns a list.
# Store it in another variable.
rslt1 = list(gvn_tupl)
# Print the result list after applying list() function on the given tuple.
print("The result after applying list() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = list(gvn_str)
print("The result after applying list() function on the given string = ", rslt2)
rslt3 = list(gvn_lst)
print("The result after applying list() function on the given list = ", rslt3)

Output:

Enter some random Tuple Elements separated by spaces = 3 4 5 1
Enter some random string = good
Enter some random List Elements separated by spaces = 0 1 2 3
The result after applying list() function on the given tuple = [3, 4, 5, 1]
The result after applying list() function on the given string = ['g', 'o', 'o', 'd']
The result after applying list() function on the given list = [0, 1, 2, 3]

Python list() Function with Examples Read More »

Python sum() Function with Examples

sum() Function in Python:

The sum() function returns a number that represents the sum of all items in an iterable.

Syntax:

sum(iterable, start)

Parameters

iterable: This is required. It is the sequence  to be sumed.

start: This is optional. It is a value added to the return value. The value of start is 0 by default (if omitted)

Return Value:

sum() returns the sum of the iterable’s start and items.

Examples:

Example1:

Input:

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

Output:

The sum of all the items in a given list = 29

Example2:

Input:

Given List = [1, 2, 3, 5, 10]
start value = 4

Output:

The sum of all the items in a given list with given start value =  25

sum() Function with Examples in Python

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

1)Without start value

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the sum() function to get the sum of all the items in a given list.
  • Store it in another variable.
  • Print the sum of all the items in a 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, 6, 15, 5]
# Pass the given list as an argument to the sum() function to get the sum of all
# the items in a given list
# Store it in another variable.
summ_lst = sum(gvn_lst)
# Print the sum of all the items in a given list.
print("The sum of all the items in a given list =", summ_lst)

Output:

The sum of all the items in a given list = 29
2)With start value

Approach:

  • Give the list as static input and store it in a variable.
  • Give the start value as static input and store it in a variable.
  • Pass the given list, start value as the arguments to the sum() function to get the sum of all the items in a given list. Begin with the given start value and add all of the items in a list to it.
  • Store it in another variable.
  • Print the sum of all the items in a given list with the given start value.
  • 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, 3, 5, 10]
# Give the start value as static input and store it in a variable.
gvn_strt = 4
# Pass the given list, start value as the arguments to the sum() function to
# get the sum of all the items in a given list.
# Begin with the given start value and add all of the items in a list to it.
# Store it in another variable.
summ_lst = sum(gvn_lst, gvn_strt)
# Print the sum of all the items in a given list with the given start value.
print("The sum of all the items in a given list with given start value = ", summ_lst)

Output:

The sum of all the items in a given list with given start value =  25

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

1)Without start value

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 sum() function to get the sum of all the items in a given list.
  • Store it in another variable.
  • Print the sum of all the items in a 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 sum() function to get the sum of all
# the items in a given list
# Store it in another variable.
summ_lst = sum(gvn_lst)
# Print the sum of all the items in a given list.
print("The sum of all the items in a given list =", summ_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 10
The sum of all the items in a given list = 110
2)With start value

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the start value as user input using the int(input()) function and store it in a variable.
  • Pass the given list, start value as the arguments to the sum() function to get the sum of all the items in a given list. Begin with the given start value and add all of the items in a list to it.
  • Store it in another variable.
  • Print the sum of all the items in a given list with the given start 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.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the start value as user input using the int(input()) function
# and store it in a variable.
gvn_strt = int(input("Enter some random number = "))
# Pass the given list, start value as the arguments to the sum() function to
# get the sum of all the items in a given list.
# Begin with the given start value and add all of the items in a list to it.
# Store it in another variable.
summ_lst = sum(gvn_lst, gvn_strt)
# Print the sum of all the items in a given list with the given start value.
print("The sum of all the items in a given list with given start value = ", summ_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 10
Enter some random number = 20
The sum of all the items in a given list with given start value = 130

Python sum() Function with Examples Read More »

Python min() Function with Examples

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

The min() function returns the item with the lowest value or the item in an iterable with the lowest value.

If the values are strings, they are compared alphabetically.

Syntax:

min(iterable)

or

min(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:

min() returns the iterable’s smallest element.

Examples:

Example1:

Input:

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

Output:

The smallest number in the given list = 1

Example2:

Input:

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

Output:

The smallest string in the given list (alphabetically ordered) is :
btechgeeks

Note:

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

min() 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 min() function to get the smallest element in the given list.
  • Store it in another variable.
  • Print the smallest 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 min() function to get the smallest
# element in the given list.
# Store it in another variable.
smallst_numb = min(gvn_lst)
# Print the smallest element(number) in the given list.
print("The smallest number in the given list =", smallst_numb)

Output:

The smallest number in the given list = 1
2)For String List

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

Note: If the items in an iterable are strings, the smallest (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 min() function to get the smallest string in the given list. If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the smallest 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 min() function to get the smallest
# string in the given list.If the items in an iterable are strings,
# the smallest (alphabetically ordered) item is returned.
# Store it in another variable.
smallst_str = min(gvn_strlst)
# Print the smallest string in the given list (alphabetically ordered).
print("The smallest string in the given list (alphabetically ordered) is :")
print(smallst_str)

Output:

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

To get the smallest number from the given numbers.

# Give some random numbers as arguments to the min() function.
smallst_numb = min(5, 10, -1, 9, 6)
# Print the smallest number from the given numbers
print("The smallest number from the given numbers = ", smallst_numb)

Output:

The smallest number from the given numbers =  -1

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 min() function to get the smallest element in the given list.
  • Store it in another variable.
  • Print the smallest 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 min() function to get the smallest
# element in the given list.
# Store it in another variable.
smallst_numb = min(gvn_lst)
# Print the smallest element(number) in the given list.
print("The smallest number in the given list =", smallst_numb)

Output:

Enter some random List Elements separated by spaces = 6 2 1 -2 0
The smallest number in the given list = -2
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 min() function to get the smallest string in the given list. If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the smallest 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 min() function to get the smallest
# string in the given list.If the items in an iterable are strings,
# the smallest (alphabetically ordered) item is returned.
# Store it in another variable.
smallst_str = min(gvn_strlst)
# Print the smallest string in the given list (alphabetically ordered).
print("The smallest string in the given list (alphabetically ordered) is :")
print(smallst_str)

Output:

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

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 min() Function with Examples Read More »

Python next() Function with Examples

next() Function in Python:

The next() function returns the iterator’s next item.

You can specify a default return value to be returned if the iterable has reached its limit or end.

Syntax:

next(iterable, default)

Parameters

iterable: This is required. It is an iterable object.

default: This is Optional. If the iterable has reached its limit or end, this is the default value to return.

Return Value: 

  • The next() function returns the iterator’s next item.
  • If the iterator runs out of values, it returns the value passed as an argument.
  • If the default parameter is omitted and the iterator becomes exhausted, the StopIteration exception is thrown.

Examples:

Example1:

Input:

Given List =  ['good', 'morning', 'btechgeeks']

Output:

good
morning
btechgeeks
Traceback (most recent call last):
File "jdoodle.py", line 13, in <module>
print(next(iteratr))
StopIteration

Explanation

Here it prints the iterator value of the list element and increase the 
iterator to next element. 
It raises an errors when it exceeds the length of the list.

Example2:

Input:

Given List = ['good', 'morning', 'btechgeeks']
default value = hello

Output:

good
morning
btechgeeks
hello

Explanation

Here it prints the iterator value of the list element and increase the 
iterator to next element. 
When it exceeds the length of the list it prints the default value.

next() Function with Examples in Python

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

1)Without default value

Approach:

  • Give the list as static input and store it in a variable.
  • Convert the given list to an iterator using the iter() function and store it in another variable.
  • Apply the next() function to the above iterator to get the next item in the given list.
  • Store it in another variable.
  • Similarly, do the same till the end of the list.
  • 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']
# Convert the given list to an iterator using the iter() function and
# store it in another variable.
iteratr = iter(gvn_lst)
# Apply the next() function to the above iterator to get the next item in the
# given list.
# Store it in another variable.
print(next(iteratr))
# Similarly, do the same till the end of the list.
print(next(iteratr))
print(next(iteratr))
print(next(iteratr))

Output:

good
morning
btechgeeks
Traceback (most recent call last):
File "jdoodle.py", line 13, in <module>
print(next(iteratr))
StopIteration

Explanation

Here it prints the iterator value of the list element and increase iterator to
next element. 
It raises an errors when it exceeds the length of the list.
2)With the default value

Approach:

  • Give the list as static input and store it in a variable.
  • Give the default value as static input and store it in another variable.
  • Convert the given list to an iterator using the iter() function and store it in another variable.
  • Pass the above iterator and default value as the arguments to the given list to get the next item in the given list.
  • Store it in another variable.
  • Similarly, do the same till the end of the list.
  • 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 default value as static input and store it in another variable.
gvn_defalt_valu = 'hello'
# Convert the given list to an iterator using the iter() function and
# store it in another variable.
iteratr = iter(gvn_lst)
# Pass the above iterator and default value as the arguments to the given list to
# get the next item in the given list.
# Store it in another variable.
print(next(iteratr, gvn_defalt_valu))
# Similarly, do the same till the end of the list.
print(next(iteratr, gvn_defalt_valu))
print(next(iteratr, gvn_defalt_valu))
print(next(iteratr, gvn_defalt_valu))

Output:

good
morning
btechgeeks
hello

Explanation

Here it prints the iterator value of the list element and increase iterator to
next element. 
When it exceeds the length of the list it prints the default value.

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

1)Without default value

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Convert the given list to an iterator using the iter() function and store it in another variable.
  • Apply the next() function to the above iterator to get the next item in the given list.
  • Store it in another variable.
  • Similarly, do the same till the end of the 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()))
# Convert the given list to an iterator using the iter() function and
# store it in another variable.
iteratr = iter(gvn_lst)
# Apply the next() function to the above iterator to get the next item in the
# given list.
# Store it in another variable.
print(next(iteratr))
# Similarly, do the same till the end of the list.
print(next(iteratr))
print(next(iteratr))
print(next(iteratr))

Output:

Enter some random List Elements separated by spaces = 10 20 30
10
20
30
Traceback (most recent call last):
File "jdoodle.py", line 15, in <module>
print(next(iteratr))
StopIteration
2)With the default value

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the default value as a user input using the int(input()) function and store it in another variable.
  • Convert the given list to an iterator using the iter() function and store it in another variable.
  • Pass the above iterator and default value as the arguments to the given list to get the next item in the given list.
  • Store it in another variable.
  • Similarly, do the same till the end of the 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()))
# Give the default value as a user input using the int(input()) function and 
# store it in another variable.
gvn_defalt_valu = int(input("Enter some random number = "))
# Convert the given list to an iterator using the iter() function and
# store it in another variable.
iteratr = iter(gvn_lst)
# Pass the above iterator and default value as the arguments to the given list to
# get the next item in the given list.
# Store it in another variable.
print(next(iteratr, gvn_defalt_valu))
# Similarly, do the same till the end of the list.
print(next(iteratr, gvn_defalt_valu))
print(next(iteratr, gvn_defalt_valu))
print(next(iteratr, gvn_defalt_valu))

Output:

Enter some random List Elements separated by spaces = 10 20 30 
Enter some random number = 40
10
20
30
40

 

Python next() Function with Examples Read More »

Python locals() Function with Examples

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

The locals() method updates the current local symbol table and returns a dictionary of its contents.

A symbol table is a data structure maintained by a compiler that contains all necessary program information.

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

There are two types of symbol tables.

  1. Global symbol table
  2. Local symbol table

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.

Similarly, 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.

Syntax:

locals()

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

Return Value:

The dictionary associated with the current local symbol table is updated and returned by the locals() method.

locals() Function with Examples in Python

Using Built-in Functions (Static Input)

1)How does Python’s globals() method work?

Approach:

  • Take a variable and initialize it with the locals() function.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

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

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f65e0415cc0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/29fd5ae710f8ed30944276be6a192e84.py', '__cached__': None, 'm': {...}}
Example-2: How does locals() function within a local scope?

Approach:

  • Create a function localNotExist() which returns the value returned  by locals() function.
  • Create another function localsExist().
  • Inside the localsExist take a variable and set its value to true.
  • Return the value returned by locals() function.
  • Inside the main function call localNotExist() function and print it.
  • call localsExist() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function localNotExist() which returns the value returned  by locals() function.


def localNotExist():
    return locals()
# Create another function localsExist().


def localsExist():
  # Inside the localsExist take a variable and set its value to true.
    present = True
    # Return the value returned by locals() function.

    return locals()


# Inside the main function call localNotExist() function and print it.
# call localsExist() function and print it.
print('localNotExist :', localNotExist())
print('localsExist :', localsExist())

Output:

localNotExist : {}
localsExist : {'present': 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 locals() Function with Examples Read More »