Python

Python Program for Set len() Method

In the previous article, we have discussed Python Program for Set sorted() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'is', 'btechgeeks', 'hello', 'this'}
The given set's length =  4

Example2:

Input:

Given set = {34, 2, 56, 12, 1, 4, 2, 5}

Output:

The given set is :
{1, 34, 2, 4, 5, 12, 56}
The given set's length = 7

Explanation:

Here the total items present in the set is 8, but set doesn't allow duplicate elements. 
Here 2 is the duplicate element. so, it gives the length of set as 7.

Program for Set len() Method in Python

set len() Method:

The set len() method is one of the set methods for determining the length of a set i.e, the total number of items present in the set.

Syntax:

len(set)

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply len() method to the given set to get the total number of items present (length) in the given set.
  • Store it in another variable.
  • Print the length of the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply len() method to the given set to get the total number of items present
# (length) in the given set.
# Store it in another variable.
gvn_setlengt = len(gven_set)
# Print the length of the given set.
print("The given set's length = ", gvn_setlengt)

Output:

The given set is :
{'is', 'btechgeeks', 'hello', 'this'}
The given set's length =  4

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply len() method to the given set to get the total number of items present (length) in the given set.
  • Store it in another variable.
  • Print the length of the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply len() method to the given set to get the total number of items present
# (length) in the given set.
# Store it in another variable.
gvn_setlengt = len(gven_set)
# Print the length of the given set.
print("The given set's length = ", gvn_setlengt)

Output:

Enter some random values separated by spaces = 34 2 56 12 1 4 2 5
The given set is :
{1, 34, 2, 4, 5, 12, 56}
The given set's length = 7

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.

Python Program for Set len() Method Read More »

Python Program for Set pop() Method

In the previous article, we have discussed Python Program for Set len() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given Set = {80, 40, 20, 20, 10, 35, 65}

Output:

The given set is :
{65, 35, 40, 10, 80, 20}
The above given set after poping an item =  {35, 40, 10, 80, 20}

Example2:

Input:

 Given Set = {9, 8, 7, 1, 2, 3, 4, 12, 1}

Output:

The given set is :
{1, 2, 3, 4, 7, 8, 9, 12}
The above given set after poping an item =  {2, 3, 4, 7, 8, 9, 12}

Program for Set pop() Method in Python

set pop() Method:

In python, The pop() method removes an item at random from the set. This is due to the Python set’s inability to store items using indexes. The Python set stores and returns items randomly.

This method returns the item that was removed.

Syntax:

set.pop()

Parameters: This method has no parameter values.

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply pop() method to the given set which removes the random element in the given set.
  • Print the above given set after popping an item from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {80, 40, 20, 20, 10, 35, 65}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply pop() method to the given set which removes the random element in the given set.
gven_set.pop()
# Print the above given set after popping an item
print("The above given set after poping an item = ", gven_set)

Output:

The given set is :
{65, 35, 40, 10, 80, 20}
The above given set after poping an item =  {35, 40, 10, 80, 20}

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply pop() method to the given set which removes the random element in the given set.
  • Print the above given set after popping an item from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply pop() method to the given set which removes the random element in the given set.
gven_set.pop()
# Print the above given set after poping an item
print("The above given set after poping an item = ", gven_set)

Output:

Enter some random values separated by spaces = 2 3 4 1 4 2 6 6 3
The given set is :
{1, 2, 3, 4, 6}
The above given set after poping an item = {2, 3, 4, 6}

Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.

Python Program for Set pop() Method Read More »

Python Program for Set min() Method

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

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given Set = {1, 0, 5, 8, 2, 8, 6, 2}

Output:

The given set is :
{0, 1, 2, 5, 6, 8}
The above Given set's minimum value =  0

Example2:

Input:

Given Set = {-1, 5, 3, 10, 16, 0, 3, 2, 5}

Output:

The given set is :
{0, 2, 3, 5, 10, 16, -1}
The above Given set's minimum value =  -1

Program for Set min() Method in Python

Set min() Function:

The set min function in Python is used to find the smallest value in a given set.

Syntax:

min(set_name)

The set min function finds the smallest number of items from a given set’s total number of items.

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply the min() method to the given set to get the minimum value in the given set.
  • Store it in another variable.
  • Print the minimum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {1, 0, 5, 8, 2, 8, 6, 2}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply min() method to the given set to get the minimum value in the given set.
# Store it in another variable.
minim_val = min(gven_set)
# Print the minimum value in the above-given set.
print("The above Given set's minimum value = ", minim_val)

Output:

The given set is :
{0, 1, 2, 5, 6, 8}
The above Given set's minimum value =  0

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply the min() method to the given set to get the minimum value in the given set.
  • Store it in another variable.
  • Print the minimum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply min() method to the given set to get the minimum value in the given set.
# Store it in another variable.
minim_val = min(gven_set)
# Print the minimum value in the above-given set.
print("The above Given set's minimum value = ", minim_val)

Output:

Enter some random values separated by spaces = -10 3 4 -25 60 50 3 -10
The given set is :
{3, 4, -25, 50, -10, 60}
The above Given set's minimum value = -25

Python Program for Set min() Method Read More »

Python Program for Set remove() Method

In the previous article, we have discussed Python Program for Set min() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}
item to be removed = 'hello'

Output:

The given set is :
{'hello', 'btechgeeks', 'is', 'this'}
The given set after removing the given specific item :
{'btechgeeks', 'is', 'this'}

Example2:

Input:

Given set = {10, 30, 50, 20, 30, 40, 60}
item to be removed = 10

Output:

The given set is :
{40, 10, 50, 20, 60, 30}
The given set after removing the given specific item :
{40, 50, 20, 60, 30}

Program for Set remove() Method in Python

set remove() Method:

In Python, the built-in method remove() removes the element from the set only if the element is present in the set, similarly to the discard() method, but If the element is not found in the set, an error or exception is thrown.

Syntax:

set.remove(item)

Parameter Values : 

item: This is required compulsorily.The item to look for and remove

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be removed as static input and store it in another variable.
  • Apply remove() method to the given set which removes the above given specific item from the given set.
  • Print the above-given set after removing the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be removed as static input.
# Store it in another variable.
remve_item = 'hello'
# Apply remove() method to the given set which removes the above given specific item
# in the given set.
gven_set.remove(remve_item)
# Print the above-given set after removing the given specific item.
print("The given set after removing the given specific item :")
print(gven_set)

Output:

The given set is :
{'hello', 'btechgeeks', 'is', 'this'}
The given set after removing the given specific item :
{'btechgeeks', 'is', 'this'}

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be removed as user input using the int(input()) function.
  • Store it in another variable.
  • Apply remove() method to the given set which removes the above given specific item from the given set.
  • Print the above-given set after removing the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be removed as user input using the int(input()) function.
# Store it in another variable.
remve_item= int(input("Enter some random number = "))
# Apply remove() method to the given set which removes the above given specific item in the given set.
gven_set.remove(remve_item)
# Print the above-given set after removing the given specific item.
print("The given set after removing the given specific item :")
print(gven_set)

Output:

Enter some random values separated by spaces = 10 30 50 20 30 40 60
The given set is :
{40, 10, 50, 20, 60, 30}
Enter some random number = 10
The given set after removing the given specific item :
{40, 50, 20, 60, 30}

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.

 

Python Program for Set remove() Method Read More »

Python Program for Set union() Method

In the previous article, we have discussed Python Program for Dictionary popitem() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given First set = {'hello', 'this', 'is', 'btechgeeks'}
Given second set = {'good', 'morning', 'btechgeeks'}

Output:

The given first set is :
{'this', 'hello', 'btechgeeks', 'is'}
The given second set is :
{'btechgeeks', 'morning', 'good'}
The union set for the given first and second sets is :
{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}

Explanation:

Here 'hello' is repeated in the both the sets. so, the union() method combines both the sets but prints 
'hello' only once(i.e, removes duplicates and writes only once )

Example2:

Input:

Given First set = {2, 4, 6, 8, 0, 2, 1}
Given second set = {3, 1, 4, 4, 0, 2, 5, 1}

Output:

The given first set is :
{0, 1, 2, 4, 6, 8}
The given second set is :
{0, 1, 2, 3, 4, 5}
The union set for the given first and second sets is :
{0, 1, 2, 3, 4, 5, 6, 8}

Program for Set union() Method in Python

set union() Method:

The union() method returns a set containing all of the items from the original set as well as all of the items from the specified set (s).

You may enter as many sets as you like, separated by commas.

It does not have to be a set; any iterable object can be used.

If an item appears in more than one set, the result will only contain one instance of that item. (i.e, appears only once)

Syntax:

set.union(set1, set2........)

Parameters:

set1: This is required. The iterable to combine with. (union)

set2: This is Optional.The other iterable with which to combine. You are free to compare as many iterables as you want. Use a comma to separate each iterable.

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

Approach:

  • Give the first set as static input and initialize it with some random values.
  • Store it in a variable.
  • Give the second set as static input and initialize it with some random values.
  • Store it in another variable.
  • Print the above-given first set.
  • Print the above-given second set.
  • Apply union() method to the given first and second set which combines both the sets and removes the duplicate items.
  • Store it in another variable.
  • Print the union set for the given first and second sets.
  • The Exit of Program.

Below is the implementation:

# Give the first set as static input and initialize it with some random values.
# Store it in a variable.
fst_sett = {'hello', 'this', 'is', 'btechgeeks'}
# Give the second set as static input and initialize it with some random values.
# Store it in another variable.
scnd_sett = {'good', 'morning', 'btechgeeks'}
# Print the above-given first set.
print("The given first set is :")
print(fst_sett)
# Print the above-given second set.
print("The given second set is :")
print(scnd_sett)
# Apply union() method to the given first and second set which combines both the
# sets and removes the duplicate items.
# Store it in another variable.
unionn_sett = fst_sett.union(scnd_sett)
# Print the union set for the given first and second sets.
print("The union set for the given first and second sets is :")
print(unionn_sett)

Output:

The given first set is :
{'this', 'hello', 'btechgeeks', 'is'}
The given second set is :
{'btechgeeks', 'morning', 'good'}
The union set for the given first and second sets is :
{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}

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

Approach:

  • Give the first set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Give the second set as user input using the set(), map(), input(), and split() functions.
  • Store it in another variable.
  • Print the above-given first set.
  • Print the above-given second set.
  • Apply union() method to the given first and second set which combines both the sets and removes the duplicate items.
  • Store it in another variable.
  • Print the union set for the given first and second sets.
  • The Exit of Program.

Below is the implementation:

# Give the first set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
fst_sett = set(map(int,input("Enter some random values separated by spaces = ").split()))
# Give the second set as user input using the set(), map(), input(), and split() functions.
# Store it in another variable.
scnd_sett = set(map(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given first set.
print("The given first set is :")
print(fst_sett)
# Print the above-given second set.
print("The given second set is :")
print(scnd_sett)
# Apply union() method to the given first and second set which combines both the
# sets and removes the duplicate items.
# Store it in another variable.
unionn_sett = fst_sett.union(scnd_sett)
# Print the union set for the given first and second sets.
print("The union set for the given first and second sets is :")
print(unionn_sett)

Output:

Enter some random values separated by spaces = 45 55 65 75 85
Enter some random values separated by spaces = 28 45 65 20 10 30
The given first set is :
{65, 75, 45, 85, 55}
The given second set is :
{65, 10, 45, 20, 28, 30}
The union set for the given first and second sets is :
{65, 10, 75, 45, 20, 85, 55, 28, 30}

Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.

Python Program for Set union() Method Read More »

Python Program for Set discard() Method

Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'} 
item to be discarded = 'hello'

Output:

The given set is :
{'is', 'this', 'hello', 'btechgeeks'}
The given set after discarding the given specific item :
{'is', 'this', 'btechgeeks'}

Example2:

Input:

Given set = {10, 20, 30, 40, 50, 60, 70}
item to be discarded = 90

Output:

The given set is :
{70, 40, 10, 50, 20, 60, 30}
The given set after discarding the given specific item :
{70, 40, 10, 50, 20, 60, 30}

Explanation:

Here there is no 90 in the given set. so, discard() method prints the given set itself without raising an error.
whereas the remove() method raises an error if item is not present.

Program for Set discard() Method in Python

Set discard() Method:

The discard() method deletes the item specified from the set.

This method differs from remove() in that the remove() method will throw an error if the specified item does not exist, whereas the discard() method will not.

Syntax:

set.discard(value)

Parameters:

value: This is required.The item to look for and remove

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

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be discarded as static input and store it in another variable.
  • Apply discard() method to the given set which discards the above given specific item from the given set.
  • Print the above-given set after discarding the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be discarded as static input.
# Store it in another variable.
discrd_item = 'hello'
# Apply discard() method to the given set which discards the above given specific item
# in the given set.
gven_set.discard(discrd_item)
# Print the above-given set after discarding the given specific item.
print("The given set after discarding the given specific item :")
print(gven_set)

Output:

The given set is :
{'is', 'this', 'hello', 'btechgeeks'}
The given set after discarding the given specific item :
{'is', 'this', 'btechgeeks'}

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

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be discarded as user input using the int(input()) function.
  • Store it in another variable.
  • Apply discard() method to the given set which discards the above given specific item from the given set.
  • Print the above-given set after discarding the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be discarded as user input using the int(input()) function.
# Store it in another variable.
discrd_item= int(input("Enter some random number = "))
# Apply discard() method to the given set which discards the above given specific item in the given set.
gven_set.discard(discrd_item)
# Print the above-given set after discarding the given specific item.
print("The given set after discarding the given specific item :")
print(gven_set)

Output:

Enter some random values separated by spaces = 10 1 4 5 2 1 3 2 4
The given set is :
{1, 2, 3, 4, 5, 10}
Enter some random number = 5
The given set after discarding the given specific item :
{1, 2, 3, 4, 10}

Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.

Python Program for Set discard() Method Read More »

Python Program for Dictionary keys() Function

In the previous article, we have discussed Python Program for Set pop() Method
Dictionary in python :

A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

Examples:

Example1:

Input:

Given dictionary = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The above given dictionary keys are :
dict_keys(['hello', 'this', 'is', 'btechgeeks'])
The above given new dictionary is :
{}
The above given New empty dictionary keys are :
dict_keys([])

Example2:

Input:

Given dictionary = {10: 'good', 20: 'morning', 30: 'btechgeeks'}

Output:

The above given dictionary is :
{10: 'good', 20: 'morning', 30: 'btechgeeks'}
The above given dictionary keys are :
dict_keys([10, 20, 30])
The above given new dictionary is :
{}
The above given New empty dictionary keys are :
dict_keys([])

Program for Dictionary keys() Function in Python

Dictionary keys() Function:

In python, the dictionary keys() function returns a list of available keys in a dictionary (total keys present).

Syntax:

dictionary.keys()

Parameter Values: The dictionary keys() function doesn’t have any parameters.

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

Approach:

  • Give the dictionary as static input by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply dictionary. keys() method to the above-given dictionary to get all the keys.
  • Store it in another variable.
  • Print the above-given dictionary keys.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. keys() method to the above new empty dictionary and print it.
  • The Exit of Program.

Below is the implementation:

# Give the dictionary by initializing it with some random values and store it
# in a variable.
gvn_dictinry = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.keys() method to the above-given dictionary to get all the keys.
# Store it in another variable
dict_keyss = gvn_dictinry.keys()
# Print the above given dictionary keys.
print("The above given dictionary keys are :")
print(dict_keyss)
# Take a new empty dictionary and store it in another variable.
new_dict = {}
# Print the above-initialized new empty dictionary.
print("The above given new dictionary is :")
print(new_dict)
# Apply dictionary.keys() method to the above new empty dictionary and print it.
print("The above given New empty dictionary keys are :")
print(new_dict.keys())

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The above given dictionary keys are :
dict_keys(['hello', 'this', 'is', 'btechgeeks'])
The above given new dictionary is :
{}
The above given New empty dictionary keys are :
dict_keys([])

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

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Print the above-given dictionary.
  • Apply dictionary. keys() method to the above-given dictionary to get all the keys.
  • Store it in another variable.
  • Print the above-given dictionary keys.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. keys() method to the above new empty dictionary and print it.
  • The Exit of Program.

Below is the implementation:

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

# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.keys() method to the above-given dictionary to get all the keys.
# Store it in another variable
dict_keyss = gvn_dictinry.keys()
# Print the above given dictionary keys.
print("The above given dictionary keys are :")
print(dict_keyss)
# Take a new empty dictionary and store it in another variable.
new_dict = {}
# Print the above-initialized new empty dictionary.
print("The above given new dictionary is :")
print(new_dict)
# Apply dictionary.keys() method to the above new empty dictionary and print it.
print("The above given New empty dictionary keys are :")
print(new_dict.keys())

Output:

Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 10 good
Enter key and value separated by spaces = 20 morning
Enter key and value separated by spaces = 30 btechgeeks
The above given dictionary is :
{'10': 'good', '20': 'morning', '30': 'btechgeeks'}
The above given dictionary keys are :
dict_keys(['10', '20', '30'])
The above given new dictionary is :
{}
The above given New empty dictionary keys are :
dict_keys([])

Want to excel in creating, adding, removing elements from python dictionaries? Look at this tutorial ie., python dictionary method examples, and explore more.

Python Program for Dictionary keys() Function Read More »

Python Program for Dictionary popitem() Method

In the previous article, we have discussed Python Program for Dictionary keys() Function
Dictionary in python :

A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

Examples:

Example1:

Input:

Given dictionary = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The key-value pair removed from the given dictionary= ('btechgeeks', 150)
The above given dictionary after the removal of the last item(key-value):
{'hello': 120, 'this': 130, 'is': 140}

Example2:

Input:

Given dictionary = {10: 'good', 60: 'morning', 140: 'btechgeeks'}

Output:

The above given dictionary is :
{10: 'good', 60: 'morning', 140: 'btechgeeks'}
The key-value pair removed from the given dictionary= (140, 'btechgeeks')
The above given dictionary after the removal of the last item(key-value):
{10: 'good', 60: 'morning'}

Program for Dictionary popitem() Method in python

Dictionary popitem() Function:

Python popitem() is a Dictionary function that removes the most recently inserted key-value pair and prints the removed pair.

Syntax:

dictionary.popitem()

Parameter Values:

The dictionary popitem() function doesn’t have any parameters.

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

Approach:

  • Give the dictionary as static input by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply dictionary.popitem() method to the above-given dictionary to remove the last inserted key-value pair.
  • Store it in another variable.
  • Print the above removed key-value pair.
  • Print the above-given dictionary after removal of the last item (key-value pair).
  • The Exit of Program.

Below is the implementation:

# Give the dictionary as static input by initializing it with some random values
# and store it in a variable.
gvn_dictinry = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.popitem() method to the above-given dictionary to remove the last
# inserted key value pair
# Store it in another variable.
remvd_lstitem = gvn_dictinry.popitem()
# Print the above removed key-value pair.
print("The key-value pair removed from the given dictionary=", remvd_lstitem)
# Print the above-given dictionary after removal of last item.
print("The above given dictionary after the removal of the last item(key-value):")
print(gvn_dictinry)

Output:

The above given dictionary is :
{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
The key-value pair removed from the given dictionary= ('btechgeeks', 150)
The above given dictionary after the removal of the last item(key-value):
{'hello': 120, 'this': 130, 'is': 140}

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

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Print the above-given dictionary.
  • Apply dictionary.popitem() method to the above-given dictionary to remove the last inserted key-value pair.
  • Store it in another variable.
  • Print the above removed key-value pair.
  • Print the above-given dictionary after removal of the last item (key-value pair).
  • The Exit of Program.

Below is the implementation:

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

# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.popitem() method to the above-given dictionary to remove the last
# inserted key value pair
# Store it in another variable.
remvd_lstitem = gvn_dictinry.popitem()
# Print the above removed key-value pair.
print("The key-value pair removed from the given dictionary=", remvd_lstitem)
# Print the above-given dictionary after removal of last item.
print("The above given dictionary after the removal of the last item(key-value):")
print(gvn_dictinry)

Output:

Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 10 good
Enter key and value separated by spaces = 60 morning
Enter key and value separated by spaces = 140 btechgeeks
The above given dictionary is :
{'10': 'good', '60': 'morning', '140': 'btechgeeks'}
The key-value pair removed from the given dictionary= ('140', 'btechgeeks')
The above given dictionary after the removal of the last item(key-value):
{'10': 'good', '60': 'morning'}

Learn several Python Dictionary Method Examples on our page and access the dictionary items easily.

 

Python Program for Dictionary popitem() Method Read More »

Python Program for floor() Function

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

If necessary, the math.floor() method rounds a number DOWN to the nearest integer and returns the result.

The python math. floor() function returns the nearest integer value that is less than or equal to the specified expression or Value.

Syntax:

math.floor(number)

Parameter Values:

number: This is required. It Sets the number to round down.

Return Value: The return value is an int value that represents the rounded number.

Examples:

Example1:

Input:

Given tuple = (1.3, 10.04, -4.4, 7.7, 9.45)
Given List = [2.8, 3.4, -1.7, -6.82, 12.2]

Output:

Given positive number 9.2 floor value = 9.00
Given negative number -8.6 floor value = -9.00
Given Tuple element(gvn_tupl[3]) floor value = 7.00
Given Tuple element(gvn_lst[2]) floor value = -2.00
The floor of pi is:  3
Given multiple number(35+40-10.3) floor value  = 64.00

Example2:

Input:

Given tuple = (2.4, 85.67, 37.8, -25.5)
Given List =  [45.9, 11.32, -25, 6, 12.8, -10]

Output:

Given positive number 30.1 floor value = 30.00
Given negative number -20.4 floor value = -21.00
Given Tuple element(gvn_tupl[0]) floor value = 2.00
Given Tuple element(gvn_lst[3]) floor value = 6.00
The floor of pi is:  3
Given multiple number(23-10+5) floor value  = 18.00

Program for floor() Function in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Give the list as static input and store it in another variable.
  • Apply math. floor() function to any positive number and print it.
  • Apply math. floor() function to any negative number and print it.
  • Apply math. floor() function to the given tuple element and print it.
  • Apply math. floor() function to the pi value and print it.
  • Apply math. floor() function to any multiple numbers and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as static input and store it in a variable.
gvn_tupl = (1.3, 10.04, -4.4, 7.7, 9.45)
# Give the list as static input and store it in another variable.
gvn_lst = [2.8, 3.4, -1.7, -6.82, 12.2]
# Apply math. floor() function to any positive number and print it.
print('Given positive number 9.2 floor value = %.2f' % math.floor(9.2))
# Apply math. floor() function to any negative number and print it.
print('Given negative number -8.6 floor value = %.2f' % math.floor(-8.6))
# Apply math. floor() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[3]) floor value = %.2f' % math.floor(
    gvn_tupl[3]))
# Apply math. floor() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[2]) floor value = %.2f' % math.floor(gvn_lst[2]))
# Apply math. floor() function to the pi value and print it.
print("The floor of pi is: ", math.floor(math.pi))
# Apply math. floor() function to any multiple numbers and print it.
print('Given multiple number(35+40-10.3) floor value  = %.2f' %
      math.floor(35+40-10.3))

Output:

Given positive number 9.2 floor value = 9.00
Given negative number -8.6 floor value = -9.00
Given Tuple element(gvn_tupl[3]) floor value = 7.00
Given Tuple element(gvn_lst[2]) floor value = -2.00
The floor of pi is:  3
Given multiple number(35+40-10.3) floor value  = 64.00

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the positive number as user input using the float(input()) function
  • Store it in another variable.
  • Apply math. floor() function to the given positive number and print it.
  • Give the negative number as user input using the float(input()) function and store it in another variable.
  • Apply math. floor() function to the given negative number and print it.
  • Apply math. floor() function to the given tuple element and print it.
  • Apply math. floor() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(float, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the positive number as user input using the float(input()) function and
# Store it in another variable.
gvn_positvenum=float(input("Enter some random number = "))
# Apply math. floor() function to the given positive number and print it.
print('Given positive number',gvn_positvenum,'floor value = %.2f' % math.floor(gvn_positvenum))
# # Give the negative number as user input using the float(input()) function and
# Store it in another variable.
gvn_negatvenum=float(input("Enter some random number = "))
# Apply math. floor() function to the given negative number and print it.
print('Given negative number',gvn_negatvenum,' floor value = %.2f' % math.floor(gvn_negatvenum))
# Apply math. floor() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[0]) floor value = %.2f' % math.floor(
    gvn_tupl[0]))
# Apply math. floor() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[3]) floor value = %.2f' % math.floor(gvn_lst[3]))

Output:

Enter some random tuple Elements separated by spaces = 26.4 10.8 -11.23 -10.7
Enter some random List Elements separated by spaces = 89 45.2 65.3 96.8
Enter some random number = 54.3
Given positive number 54.3 floor value = 54.00
Enter some random number = -10.5
Given negative number -10.5 floor value = -11.00
Given Tuple element(gvn_tupl[0]) floor value = 26.00
Given Tuple element(gvn_lst[3]) floor value = 96.00

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.

Python Program for floor() Function Read More »

Python Program for ceil() Function

In the previous article, we have discussed Python Program for Set discard() Method
ceil() Function in Python:

If necessary, the math.ceil() method rounds a number UP to the nearest integer and returns the result.

The ceil function in Python is used to return the smallest integer value that is greater than or equal to the specified expression or number.

Syntax:

math.ceil(number)

Parameter Values:

number: This is required. It Sets the number to round up.

Note: The ceil function returns the ceiling value if the number argument is positive or negative.
The math ceil function returns TypeError if the number argument is not a number.

Return Value: The return value is an int value that represents the rounded number.

Examples:

Example1:

Input:

Given tuple = (1.3, 10.04, -4.4, 7.7, 9.45)
Given List = [2.8, 3.4, -1.7, -6.82, 12.2]

Output:

Given positive number 9.2 ceiling value = 10.00
Given negative number -8.6 ceiling value = -8.00
Given Tuple element(gvn_tupl[3]) ceiling value = 8.00
Given Tuple element(gvn_lst[2]) ceiling value = -1.00
The Ceil of pi is:  4
Given multiple number(35+40-10.3) ceiling value  = 65.00

Example2:

Input:

Given tuple = (2.4, 85.67, 37.8, -25.5)
Given List =  [45.9, 11.32, -25, 6, 12.8, -10]

Output:

Given positive number 30.1 ceiling value = 31.00
Given negative number -20.4 ceiling value = -20.00
Given Tuple element(gvn_tupl[0]) ceiling value = 3.00
Given Tuple element(gvn_lst[3]) ceiling value = 6.00
The Ceil of pi is:  4
Given multiple number (23-10+5) ceiling value  = 18.00

Program for ceil() Function in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Give the list as static input and store it in another variable.
  • Apply math. ceil() function to any positive number and print it.
  • Apply math. ceil() function to any negative number and print it.
  • Apply math. ceil() function to the given tuple element and print it.
  • Apply math. ceil() function to the pi value and print it.
  • Apply math. ceil() function to any multiple numbers and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as static input and store it in a variable.
gvn_tupl = (1.3, 10.04, -4.4, 7.7, 9.45)
# Give the list as static input and store it in another variable.
gvn_lst = [2.8, 3.4, -1.7, -6.82, 12.2]
# Apply math. ceil() function to any positive number and print it.
print('Given positive number 9.2 ceiling value = %.2f' % math.ceil(9.2))
# Apply math. ceil() function to any negative number and print it.
print('Given negative number -8.6 ceiling value = %.2f' % math.ceil(-8.6))
# Apply math. ceil() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[3]) ceiling value = %.2f' % math.ceil(
    gvn_tupl[3]))
# Apply math. ceil() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[2]) ceiling value = %.2f' % math.ceil(gvn_lst[2]))
# Apply math. ceil() function to the pi value and print it.
print("The Ceil of pi is: ", math.ceil(math.pi))
# Apply math. ceil() function to any multiple numbers and print it.
print('Given multiple number(35+40-10.3) ceiling value  = %.2f' %
      math.ceil(35+40-10.3))

Output:

Given positive number 9.2 ceiling value = 10.00
Given negative number -8.6 ceiling value = -8.00
Given Tuple element(gvn_tupl[3]) ceiling value = 8.00
Given Tuple element(gvn_lst[2]) ceiling value = -1.00
The Ceil of pi is:  4
Given multiple number(35+40-10.3) ceiling value  = 65.00

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

Approach:

  • Import math module using the import keyword.
  • Give the tuple as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the positive number as user input using the float(input()) function
  • Store it in another variable.
  • Apply math. ceil() function to the given positive number and print it.
  • Give the negative number as user input using the float(input()) function and store it in another variable.
  • Apply math. ceil() function to the given negative number and print it.
  • Apply math. ceil() function to the given tuple element and print it.
  • Apply math. ceil() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(float, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the positive number as user input using the float(input()) function and
# Store it in another variable.
gvn_positvenum=float(input("Enter some random number = "))
# Apply math. ceil() function to the given positive number and print it.
print('Given positive number',gvn_positvenum,'ceiling value = %.2f' % math.ceil(gvn_positvenum))
# # Give the negative number as user input using the float(input()) function and
# Store it in another variable.
gvn_negatvenum=float(input("Enter some random number = "))
# Apply math. ceil() function to the given negative number and print it.
print('Given negative number',gvn_negatvenum,'ceiling value = %.2f' % math.ceil(gvn_negatvenum))
# Apply math. ceil() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[0]) ceiling value = %.2f' % math.ceil(
    gvn_tupl[0]))
# Apply math. ceil() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[3]) ceiling value = %.2f' % math.ceil(gvn_lst[3]))



Output:

Enter some random tuple Elements separated by spaces = 12.3 -11.2 6.7 4.6
Enter some random List Elements separated by spaces = 8.5 10.3 -6.4 -20.8
Enter some random number = 30.1
Given positive number 30.1 ceiling value = 31.00
Enter some random number = -20.4
Given negative number -20.4 ceiling value = -20.00
Given Tuple element(gvn_tupl[0]) ceiling value = 13.00
Given Tuple element(gvn_lst[3]) ceiling value = -20.00

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.

Python Program for ceil() Function Read More »