Author name: Vikram Chiluka

Python object() Function with Examples

object() Function in Python:

The function object() returns an empty object.

This object cannot be extended with new properties or methods.

This object serves as the foundation for all classes; it contains the built-in properties and methods that are the default for all classes.

Syntax:

object()

Parameters: This function has no Parameters.

Return Value:

The object() function returns an object with no features.

object() Function with Examples in Python

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

Approach:

  • Take a variable and initialize it with the object() function.
  • Pass the above result as an argument to the type() function to get the type of the above object.
  • Store it in a variable.
  • Pass the above result as an argument to the dir() function to get all the attributes.
  • Store it in another variable.
  • Print the type of the result object.
  • Print all the attributes (dir) of the result object.
  • The Exit of the Program.

Below is the implementation:

# Take a variable and initialize it with the object() function.
m = object()
# Pass the above result as an argument to the type() function to get the type
# of the above object.
# Store it in a variable.
obj_type = type(m)
# Pass the above result as an argument to the dir() function to get all the
# attributes.
# Store it in another variable.
obj_dir = dir(m)
# Print the type of the result object.
print(obj_type)
# Print all the attributes (dir) of the result object.
print(obj_dir)

Output:

<class 'object'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

An object m is created here.

We used type() in the program to determine the type of the object.

Similarly, we used dir() to retrieve all of the attributes. These attributes (properties and methods) are shared by all Python class instances.

 

Python object() Function with Examples Read More »

Python frozenset() Function with Examples

frozenset() Function in Python:

The frozenset() function returns an immutable frozenset object that has been initialized with elements from the iterable passed in.

A frozen set is nothing more than an immutable version of a Python set object. While elements of a set can be changed at any time, elements of a frozen set do not change after they are created.

As a result, frozen sets can be used as Dictionary keys or as elements of another set. However, it is not ordered in the same way that sets are (the elements can be set at any index).

Syntax:

frozenset(iterable)

Parameters

The frozenset() function only accepts one parameter:

iterable: This is Optional. The iterable contains the elements to be used to initialize the frozenset.

Iterable types include set, dictionary, tuple, and so on.

Return Value:

The frozenset() function returns an immutable frozenset that has been initialized with elements from the specified iterable.

It returns an empty frozenset if no parameters are passed.

Examples:

Example1:

Input:

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

Output:

The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'})
An empty frozenset =  frozenset()

Example2:

Input:

Given dictionary = {"hello": 12, "this": 14, "is": 10, "btechgeeks": 20}

Output:

The Given dictionary's frozenset :
frozenset({'btechgeeks', 'is', 'hello', 'this'})

frozenset() 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.
  • Pass the given list as an argument to the frozenset () function that returns an immutable frozenset object that has been initialized with elements from the iterable passed in.
  • Store it in another variable.
  • Print the frozenset of the given list.
  • Print an empty frozenset without passing any parameters to the frozenset () function.
  • 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']
# Pass the given list as an argument to the frozenset () function that returns
# an immutable frozenset object that has been initialized with elements from
# the iterable passed in.
# Store it in another variable.
rslt = frozenset(gvn_lst)
# Print the frozenset of the given list.
print("The Given list's frozenset =", rslt)
# Print an empty frozenset without passing any parameters to the frozenset ()
# function.
print("An empty frozenset = ", frozenset())

Output:

The Given list's frozenset = frozenset({'morning', 'good', 'btechgeeks'})
An empty frozenset =  frozenset()
Similarly, do the same for the dictionaries

When you use a dictionary as an iterable for a frozen set, the set is created using only dictionary keys.

# Give the dictionary as static input and store it in a variable.
gvn_dict = {"hello": 12, "this": 14, "is": 10, "btechgeeks": 20}
# Pass the given dictionary as an argument to the frozenset () function that returns
# an immutable frozenset object that has been initialized with elements from
# the iterable passed in.
# Store it in another variable.
rslt = frozenset(gvn_dict)
# Print the frozenset of the given dictionary.
print("The Given dictionary's frozenset :")
print(rslt)

Output:

The Given dictionary's frozenset :
frozenset({'btechgeeks', 'is', 'hello', 'this'})
Operations of frozenset

frozenset, like normal sets, can perform operations such as copy, difference, intersection, symmetric difference, and union.

# Give two frozenset's and store them in two separate variables
gvn_froznset1 = frozenset([10, 20, 30, 40])
gvn_froznset2 = frozenset([30, 40, 50, 60])

# copy the given frozenset1 to new variable.
neww = gvn_froznset1.copy()
print(neww)

# Union of frozenset1 and frozenset2
print(gvn_froznset1.union(gvn_froznset2))

# intersection of frozenset1 and frozenset2
print(gvn_froznset1.intersection(gvn_froznset2))

# difference of frozenset1 and frozenset2
print(gvn_froznset1.difference(gvn_froznset2))

# symmetric_difference of frozenset1 and frozenset2
print(gvn_froznset1.symmetric_difference(gvn_froznset2))

Output:

frozenset({40, 10, 20, 30})
frozenset({40, 10, 50, 20, 60, 30})
frozenset({40, 30})
frozenset({10, 20})
frozenset({10, 50, 20, 60})

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.
  • Pass the given list as an argument to the frozenset () function that returns an immutable frozenset object that has been initialized with elements from the iterable passed in.
  • Store it in another variable.
  • Print the frozenset of the given list.
  • Print an empty frozenset without passing any parameters to the frozenset () function.
  • 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 frozenset () function that returns
# an immutable frozenset object that has been initialized with elements from
# the iterable passed in.
# Store it in another variable.
rslt = frozenset(gvn_lst)
# Print the frozenset of the given list.
print("The Given list's frozenset =", rslt)
# Print an empty frozenset without passing any parameters to the frozenset ()
# function.
print("An empty frozenset = ", frozenset())

Output:

Enter some random List Elements separated by spaces = 10 40 70 90
The Given list's frozenset = frozenset({40, 10, 90, 70})
An empty frozenset = frozenset()

 

Python frozenset() Function with Examples Read More »

Python vars() Function with Examples

vars() Function in Python:

The vars() function returns an object’s __dict__ attribute.

The __dict__ attribute is a dictionary that contains a list of the object’s changeable attributes.

Note:

It should be noted that calling the vars() function without any parameters will result in the return of a dictionary containing the local symbol table.

Syntax:

vars(object)

Parameters

object: It may be Any object that has the __dict__ attribute.

Return Value:

vars() returns the given object’s __dict__ attribute.

  • If the object passed to vars() does not have the __dict__ attribute, a TypeError exception is thrown.
  • If no arguments are passed to vars(), it behaves similarly to the locals() function.

Note: It should be noted that __dict__ refers to a dictionary or a mapping object. It stores the (writable) attributes of an object.

vars() Function with Examples in Python

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

Approach:

  • Create a class say Employee.
  • Take a variable and initialize it with some random number(eid).
  • Take another variable and initialize it with some random name(ename).
  • Take another variable and initialize it with some random job role(jobrole).
  • Create an object for the above class and apply the vars() method to it.
  • Store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say, Employee.
class Employee:
    # Take a variable and initialize it with some random number(eid).
    eid = 5
    # Take another variable and initialize it with some random name(ename).
    ename = "kevin"
    # Take another variable and initialize it with some random job role(jobrole).
    jobrole = "software developer"


# Create an object for the above class and apply the vars() method to it
# Store it in a variable.
rslt = vars(Employee)
# Print the above result.
print(rslt)

Output:

{'__module__': '__main__', 'eid': 5, 'ename': 'kevin', 'jobrole': 'software developer', '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}

 

Python vars() Function with Examples Read More »

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 »