Python Program for Set max() Method

Program for Set max() 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 = {20, 40, 50, 10, 20, 60, 50}

Output:

The given set is :
{40, 10, 50, 20, 60}
The above Given set's maximum value =  60

Explanation:

Here 60 is the maximum value in the given set . so, the maximum value 60 is printed by using the max() method.

Example2:

Input:

Given Set = {1, 3, 5, 20, 60, 90}

Output:

The given set is :
{1, 3, 5, 20, 90, 60}
The above Given set's maximum value =  90

Explanation:

Here 90 is the maximum value in the given set . so, the maximum value 90 is printed by using the max() method.

Program for Set max() Method in Python

set max() Method:

One of the set methods in Python is the set max function, which is used to find the maximum value within a given set.

Syntax:

max(set_name)

The set max function returns the maximum 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 max() method to the given set to get the maximum value in the given set.
  • Store it in another variable.
  • Print the maximum 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 = {20, 40, 50, 10, 20, 60, 50}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply max() method to the given set to get the maximum value in the given set.
# Store it in another variable.
maxim_val = max(gven_set)
# Print the maximum value in the above-given set.
print("The above Given set's maximum value = ", maxim_val)

Output:

The given set is :
{40, 10, 50, 20, 60}
The above Given set's maximum value =  60

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 max() method to the given set to get the maximum value in the given set.
  • Store it in another variable.
  • Print the maximum 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 max() method to the given set to get the maximum value in the given set.
# Store it in another variable.
maxim_val = max(gven_set)
# Print the maximum value in the above-given set.
print("The above Given set's maximum value = ", maxim_val)

Output:

Enter some random values separated by spaces = 10 25 68 90 120 5 30 80
The given set is :
{68, 5, 10, 80, 120, 25, 90, 30}
The above Given set's maximum value = 120

Python Program for Set copy() Method

Program for Set copy() 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 :
{'this', 'hello', 'btechgeeks', 'is'}
The New set after copying all the values from the given set is:
{'this', 'hello', 'btechgeeks', 'is'}

Explanation:

Here all the values of the given set are copied into the new set using the copy() function.

Example2:

Input:

Given set = {'good', 'morning', 'btechgeeks'}

Output:

The given set is :
{'btechgeeks', 'morning', 'good'}
The New set after copying all the values from the given set is:
{'btechgeeks', 'morning', 'good'}

Explanation:

Here all the values of the given set are copied into the new set using the copy() function.

Program for Set copy() Method in Python

set copy() Method:

The Python set copy method is used to copy the entire set into a new set.

Syntax:

set.copy()

Parameters: The copy() method for sets does not accept any parameters.

Return value: The function returns a shallow copy of the original set as its return value.

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 copy() function to the given set to copy all its elements into the new set.
  • Store it in another variable.
  • Print the above new set after copying all the values 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 = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply copy() function to the given set to copy all its elements into the new set.
# Store it in another variable.
gvnset_copy = gven_set.copy()
# Print the above new set after copying all the values from the given set
print("The New set after copying all the values from the given set is:")
print(gvnset_copy)

Output:

The given set is :
{'this', 'hello', 'btechgeeks', 'is'}
The New set after copying all the values from the given set is:
{'this', 'hello', 'btechgeeks', 'is'}

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

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply copy() function to the given set to copy all its elements into the new set.
  • Store it in another variable.
  • Print the above new set after copying all the values from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply copy() function to the given set to copy all its elements into the new set.
# Store it in another variable.
gvnset_copy = gven_set.copy()
# Print the above new set after copying all the values from the given set
print("The New set after copying all the values from the given set is:")
print(gvnset_copy)

Output:

Enter some random values separated by spaces = good morning btechgeeks
The given set is :
{'morning', 'btechgeeks', 'good'}
The New set after copying all the values from the given set is:
{'morning', 'btechgeeks', 'good'}

Python Program for set add() Method

Program for set add() 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'}
Value to be added = 'btechgeeks'

Output:

The given set is :
{'this', 'is', 'hello'}
The set is after a given element to the above given set :
{'this', 'is', 'hello', 'btechgeeks'}

Example2:

Input:

Given set = {'good', 'morning', 'this', 'is', 'btechgeeks'}
Value to be added = 'hello'

Output:

The given set is :
{'this', 'btechgeeks', 'good', 'is', 'morning'}
The set is after a given element to the above given set :
{'this', 'hello', 'btechgeeks', 'good', 'is', 'morning'}

Program for Set add() Method in Python

add() Function in set:

The set add() function in Python is used to add a new item to an existing set.

The syntax is as follows:

set_Name.add(element)

The python set add() function only accepts one parameter. You can, however, use a tuple as an argument. Remember that you cannot use this add() method to add an existing (duplicate value) to a 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.
  • Add a new value to the given set by using the add() function.
  • Print the above-given set after adding a given new value.
  • 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'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Add a new value to the given set by using the add() function.
gven_set.add('btechgeeks')
# Print the above-given set after adding a given new value.
print("The set is after a given element to the above given set :")
print(gven_set)

Output:

The given set is :
{'this', 'is', 'hello'}
The set is after a given element to the above given set :
{'this', 'is', 'hello', 'btechgeeks'}

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

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Give the value to be added as user input using the input() function and store it in another variable.
  • Add the above value to the given set by using the add() function.
  • Print the above-given set after adding a given new value.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the value to be added as user input using the input() function and 
# store it in another variable.
k=input("Enter some random value = ")
# Add the above value to the given set by using the add() function.
gven_set.add(k)
# Print the above-given set after adding a given new value.
print("The set is after a given element to the above given set :")
print(gven_set)

Output:

Enter some random values separated by spaces = good morning this is btechgeeks
The given set is :
{'this', 'btechgeeks', 'good', 'is', 'morning'}
Enter some random value = hello
The set is after a given element to the above given set :
{'this', 'hello', 'btechgeeks', 'good', 'is', 'morning'}

II) add Tuple to set :

Method #3: 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 tuple as static input and initialize it with some random values.
  • Add the above-given tuple to the given set by using the add() function.
  • Print the above-given set after adding a given tuple.
  • 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 = {11, 12, 13, 14, 15}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the tuple as static input and initialize it with some random values.
gvn_tupl = (16, 17, 18, 19)
# Add the above given tuple to the given set by using the add() function.
gven_set.add(gvn_tupl)
print("The given set after after adding a given tuple:")
# Print the above-given set after adding a given tuple.
print(gven_set)

Output:

The given set is :
{11, 12, 13, 14, 15}
The given set after after adding a given tuple:
{11, 12, 13, 14, 15, (16, 17, 18, 19)}

Python Program for Set clear() Method

Program for Set clear() 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 :
{'this', 'btechgeeks', 'is', 'hello'}
The given set after clearing all the values :
set()

Explanation:

Here all the values of the given set got cleared using the clear() function.

Example2:

Input:

Given set = {'good', 'morning', 'btechgeeks'}

Output:

The given set is :
{'morning', 'btechgeeks', 'good'}
The given set after clearing all the values :
set()

Explanation:

Here all the values of the given set got cleared using the clear() function.

Program for Set clear() Method in Python

Set clear() Method:

One of the set methods used to clear or remove total set items in Python is the set clear() function.

Syntax:

set_name.clear()

The set clear function allows you to clear or remove all of the items in a given 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.
  • Clear the values of the given set by applying the clear() function to the given set.
  • Print the above-given set after clearing all the values.
  • 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)
# Clear the values of the given set by applying clear() function to the given set.
gven_set.clear()
# Print the above-given set after clearing all the values.
print("The given set after clearing all the values :")
print(gven_set)

Output:

The given set is :
{'this', 'btechgeeks', 'is', 'hello'}
The given set after clearing all the values :
set()

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

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Clear the values of the given set by applying the clear() function to the given set.
  • Print the above-given set after clearing all the values.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Clear the values of the given set by applying clear() function to the given set.
gven_set.clear()
# Print the above-given set after clearing all the values.
print("The given set after clearing all the values :")
print(gven_set)

Output:

Enter some random values separated by spaces = good morning btechgeeks
The given set is :
{'morning', 'btechgeeks', 'good'}
The given set after clearing all the values :
set()

 

 

Python Program for Dictionary items() Function

Program for Dictionary items() 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': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}

Output:

The above given dictionary is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The above given dictionary items are :
dict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

Example2:

Input:

Given dictionary = {'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}

Output:

The above given dictionary is :
{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}
The above given dictionary items are :
dict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

Program for Dictionary items() Function in Python

Dictionary items() Function:

Python’s items Dictionary function returns a list of dictionary items that are currently available (total keys and values).

The dictionary items function returns the total number of key-value pairs in a given dictionary.

Syntax:

dictionary_name.items()

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

Approach:

  • Give the dictionary by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply dictionary. items() method to the above-given dictionary and print it.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. items() 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': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply dictionary.items() method to the above-given dictionary and print it.
print("The above given dictionary items are :")
print(gvn_dictinry.items())
# 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.items() method to the above new empty dictionary and print it.
print("The above given New empty dictionary items are :")
print(new_dict.items())

Output:

The above given dictionary is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The above given dictionary items are :
dict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

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. items() method to the above-given dictionary and print it.
  • Take a new empty dictionary and store it in another variable.
  • Print the above-initialized new empty dictionary.
  • Apply dictionary. items() 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.items() method to the above-given dictionary and print it.
print("The above given dictionary items are :")
print(gvn_dictinry.items())
# 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.items() method to the above new empty dictionary and print it.
print("The above given New empty dictionary items are :")
print(new_dict.items())

Output:

Enter some random number of keys of the dictionary = 5
Enter key and value separated by spaces = good 1
Enter key and value separated by spaces = morning 3
Enter key and value separated by spaces = this 5
Enter key and value separated by spaces = is 7
Enter key and value separated by spaces = btechgeeks 9
The above given dictionary is :
{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}
The above given dictionary items are :
dict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])
The above given new dictionary is :
{}
The above given New empty dictionary items are :
dict_items([])

Python Program for Dictionary get() Function

Program for Dictionary get() 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 = {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}

Output:

The above given dictionary is :
{100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}
The respective value at the given key 200 :   this
The respective value at the given key 400 :   btechgeeks
The respective value at the given key 500:   None

Example2:

Input:

Given dictionary = {'good': '5', 'morning': '10', 'btechgeeks': '15'}

Output:

The above given dictionary is :
{'good': '5', 'morning': '10', 'btechgeeks': '15'}
The respective value at the given key btechgeeks = 15
The respective value at the given key hello = None

Program for Dictionary get() Function in Python

 get() method in Dictionary:

The get() method of the Python dictionary returns a value for the given key. If the key is not available, the default value None is returned.

Syntax:

dict.get(key, default = None)

Parameters:

Key: This is the Key that will be looked up in the dictionary.

default: If the key does not exist, this is the value that will be returned.

Return Value:

This method returns the value for the specified key. If the key is not available, the default value None is returned.

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

Approach:

  • Give the dictionary by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary.
  • Apply the get() method to the given dictionary to get the respective value for the given key.
  • Store it in another variable.
  • Similarly, do the same for the other keys to get the respective value for the given key.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary by initializing it with some random values and store it
# in a variable.
gvn_dictinry = {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}
# Print the above-given dictionary.
print("The above given dictionary is :")
print(gvn_dictinry)
# Apply get() method to the given dictionary to get the respective value
# for the given key.
# Store it in another variable.
rslt = gvn_dictinry.get(200)
# Similarly, do the same for the other keys to get the respective value for the given key.
print("The respective value at the given key 200 :  ", rslt)
rslt_1 = gvn_dictinry.get(400)
print("The respective value at the given key 400 :  ", rslt_1)
rslt_2 = gvn_dictinry.get(500)
print("The respective value at the given key 500:  ", rslt_2)

Output:

The above given dictionary is :
{100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}
The respective value at the given key 200 :   this
The respective value at the given key 400 :   btechgeeks
The respective value at the given key 500:   None

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.
  • Give the key as user input using the input() function to get the respective value for the given key.
  • Store it in another variable.
  • Apply the get() method to the given dictionary to get the respective value for the above-given key.
  • Store it in another variable.
  • Similarly, do the same for the other keys to get the respective value for the given key.
  • The Exit of the 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)
# Give the key as user input using the input() function to get the respective value
# for the given key.
# Store it in another variable.
gvn_key= input("Enter some random key = ")
# Apply the get() method to the given dictionary to get the respective value for the above-given key.
p = gvn_dictinry.get(gvn_key)
# Store it in another variable.
print("The respective value at the given key ",gvn_key," = ", p )
# Similarly, do the same for the other keys to get the respective value for the given key.
gvn_key2= input("Enter some random key = ")
q = gvn_dictinry.get(gvn_key2)
print("The respective value at the given key ",gvn_key2," = ", q)

Output:

Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = good 5
Enter key and value separated by spaces = morning 10
Enter key and value separated by spaces = btechgeeks 15
The above given dictionary is :
{'good': '5', 'morning': '10', 'btechgeeks': '15'}
Enter some random key = btechgeeks
The respective value at the given key btechgeeks = 15
Enter some random key = hello
The respective value at the given key hello = None

Python Program for Dictionary fromkeys() Method

Program for Dictionary fromkeys() 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 keys = {'hello', 'this', 'is', 'btechgeeks'}
Given value = 'python'

Output:

The dictonary obtained for the given keys and values is :
{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}

Explanation:

Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.

Example2:

Input:

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

Output:

The dictonary obtained for the given keys without value given is :
{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}

Explanation:

Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.

Program for Dictionary fromkeys() Method in Python

fromkeys() Method:

The dict.fromkeys() method returns a new dictionary with the specified value and the given iterable (string, list, set, tuple) as keys.

Syntax:

dictionary.fromkeys(sequence, value)

Parameters:

sequence:

This is required. A sequence/iterable, the elements of which will be used as keys in the new dictionary.
value:

This is required. Each key’s value. None is the default value.

Return Value:

This function returns a new dictionary.

Using the dict.fromkeys() method, the following creates a new dict object.

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

Approach:

  • Give the keys as a set(static input) and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply the fromkeys() method for the given keys and values which return the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys and values.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}
# Give the value as static input and store it in another variable.
gvn_valu = 'python'
# Apply the fromkeys method for the given keys and values which
# returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss, gvn_valu)
print("The dictonary obtained for the given keys and values is :")
# Print the above dictionary for the given keys and values.
print(dictionary)

Output:

The dictonary obtained for the given keys and values is :
{'is': 'python', 'this': 'python', 'hello': 'python', 'btechgeeks': 'python'}

Explanation:

Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.

II) without Value:

Approach:

  • Give the keys as a set(static input) and store it in a variable.
  • Apply the fromkeys() method for the given keys which returns the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}
# Apply the fromkeys() method for the given keys  which returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss)
print("The dictonary obtained for the given keys without value given is :")
# Print the above dictionary for the given keys.
print(dictionary)

Output:

The dictonary obtained for the given keys without value given is :
{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}

Explanation:

Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.

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

Approach:

  • Give the keys as a set(user input) using set(), input(), and split() functions and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply the fromkeys() method for the given keys and values which return the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys and values.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = set(input("Enter some random keys separated by spaces = ").split())
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some random String = ")
# Apply the fromkeys method for the given keys and values which
# returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss, gvn_valu)
print("The dictonary obtained for the given keys and values is :")
# Print the above dictionary for the given keys and values.
print(dictionary)

Output:

Enter some random keys separated by spaces = hello this is btechgeeks
Enter some random String = python
The dictonary obtained for the given keys and values is :
{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}

II) without Value:

Approach:

  • Give the keys as a set(user input) using set(), input(), and split() functions and store it in a variable.
  • Apply the fromkeys() method for the given keys which returns the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = set(input("Enter some random keys separated by spaces = ").split())
# Apply the fromkeys() method for the given keys which returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss)
print("The dictonary obtained for the given keys without value given is :")
# Print the above dictionary for the given keys.
print(dictionary)

Output:

Enter some random keys separated by spaces = hello this is btechgeeks
The dictonary obtained for the given keys without value given is :
{'hello': None, 'btechgeeks': None, 'this': None, 'is': None}

Python Program for Dictionary clear() Function

Program for Set clear() 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': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}

Output:

The given dictionary before clearing is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The length of the given dictionary before clearing =  4
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing =  0

Example2:

Input:

Given dictionary = {'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}

Output:

The given dictionary before clearing is :
{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}
The length of the given dictionary before clearing = 5
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing = 0

Program for Dictionary clear() Function in Python

Dictionary clear() Function:

clear() is a Python dictionary method that removes all items from the dictionary.

Syntax:

dict.clear()

Parameters: NA

Return Value: This method has no return value.

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

Approach:

  • Give the dictionary by initializing it with some random values and store it in a variable.
  • Print the above-given dictionary before clearing.
  • Calculate the length of the given dictionary using the len() function and print it.
  • Apply clear() function to the given dictionary.
  • Print the above-given dictionary after clearing it.
  • Calculate the length of the above dictionary after clearing using the len() function and print it.
  • The Exit of the program.

Below is the implementation:

# Give the dictionary by initializing it with some random values and store it
# in a variable.
gvn_dictinry = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
# Print the above-given dictionary before clearing.
print("The given dictionary before clearing is :")
# Calculate the length of the given dictionary using the len() function and print it.
print(gvn_dictinry)
print("The length of the given dictionary before clearing = ", len(gvn_dictinry))
# Apply clear() function to the given dictionary.
gvn_dictinry.clear()
# Print the above-given dictionary after clearing it.
print("The given dictionary After clearing is :")
print(gvn_dictinry)
# Calculate the length of the above dictionary after clearing using the len() function
# and print it.
print("The length of the given dictionary After clearing = ", len(gvn_dictinry))

Output:

The given dictionary before clearing is :
{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
The length of the given dictionary before clearing =  4
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing =  0

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 before clearing.
  • Calculate the length of the given dictionary using the len() function and print it.
  • Apply clear() function to the given dictionary.
  • Print the above-given dictionary after clearing it.
  • Calculate the length of the above dictionary after clearing using the len() function and print it.
  • The Exit of the program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# 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_dict[keyy] = valuee

# Print the above-given dictionary before clearing.
print("The given dictionary before clearing is :")
# Calculate the length of the given dictionary using the len() function and print it.
print(gvn_dict)
print("The length of the given dictionary before clearing = ", len(gvn_dict))
# Apply clear() function to the given dictionary.
gvn_dict.clear()
# Print the above-given dictionary after clearing it.
print("The given dictionary After clearing is :")
print(gvn_dict)
# Calculate the length of the above dictionary after clearing using the len() function
# and print it.
print("The length of the given dictionary After clearing = ", len(gvn_dict))

Output:

Enter some random number of keys of the dictionary = 5
Enter key and value separated by spaces = good 1
Enter key and value separated by spaces = morning 2
Enter key and value separated by spaces = this 3
Enter key and value separated by spaces = is 4
Enter key and value separated by spaces = btechgeeks 5
The given dictionary before clearing is :
{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}
The length of the given dictionary before clearing = 5
The given dictionary After clearing is :
{}
The length of the given dictionary After clearing = 0

 

Python Program for Area of a Polygon with Given n Ordered Vertices

Program for Area of a Polygon with Given n Ordered Vertices

Given two lists which are the ordered coordinates of a polygon with n vertices, the task is to find the area of a given polygon.

Ordered means that the coordinates are given either clockwise or anticlockwise from the first vertex to the last.

Examples:

Example1:

Input:

Given x-coordinates list = [1, 5, 1]
Given y-coordinates list = [6, 4, 3]

Output:

The Area of the given polygon for the given n ordered vertices :
6

Example2:

Input:

Given x-coordinates list = [2, 3, 3, 1]
Given y-coordinates list = [9, 2, 1, 2]

Output:

The Area of the given polygon for the given n ordered vertices :
8

Program for Area of a Polygon with Given n Ordered Vertices in Python

Below are the ways to find the area of a given polygon for the given n ordered vertices in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the x-coordinates list as static input and store it in a variable.
  • Give the y-coordinates list as static input and store it in another variable.
  • Calculate the length of the given x-coordinates list and store it in another variable.
  • Take a variable say polygn_area and initialize its value to 0.
  • Subtract 1 from the above x-coordinates list length and store it in another variable say k .
  • Loop till the length of the given x-coordinates list using the for loop.
  • Calculate the area of the polygon using the above given mathematical formula and add it to the above-initialized variable polygn_area 
  • Assign the value of the iterator to the k which is the previous vertex to the iterator.
  • Divide the above polygn_area by 2, apply the abs() function to it which gives the absolute value, and convert it to an integer using the int() function.
  • Store it in another variable.
  • Print the above result which is the area of the given polygon.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinates list as static input and store it in a variable.
a = [1, 5, 1]
# Give the y-coordinates list as static input and store it in another variable.
b = [6, 4, 3]
# Calculate the length of the given x-coordinates list and store it in another variable.
lst_lengt = len(a)
# Take a variable say polygn_area and initialize its value to 0.
polygn_area = 0
# Subtract 1 from the above x-coordinates list length and store it in another variable
# say k .
k = lst_lengt - 1
print("The Area of the given polygon for the given n ordered vertices :")
# Loop till the length of the given x-coordinates list using the for loop.
for itr in range(0, lst_lengt):
    # Calculate the area of the polygon using the above given mathematical formula and
    # add it to the above-initialized variable polygn_area.
    polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])
    # Assign the value of the iterator to the k which is the previous vertex
    # to the iterator.
    k = itr
# Divide the above polygn_area by 2, apply the abs() function to it which gives
# the absolute value, and convert it to an integer using the int() function.
# Store it in another variable.
rslt = int(abs(polygn_area / 2.0))
# Print the above result which is the area of the given polygon.
print(rslt)

Output:

The Area of the given polygon for the given n ordered vertices :
6

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the x-coordinates list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the y-coordinates list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Calculate the length of the given x-coordinates list and store it in another variable.
  • Take a variable say polygn_area and initialize its value to 0.
  • Subtract 1 from the above x-coordinates list length and store it in another variable say k .
  • Loop till the length of the given x-coordinates list using the for loop.
  • Calculate the area of the polygon using the above given mathematical formula and add it to the above-initialized variable polygn_area 
  • Assign the value of the iterator to the k which is the previous vertex to the iterator.
  • Divide the above polygn_area by 2, apply the abs() function to it which gives the absolute value, and convert it to an integer using the int() function.
  • Store it in another variable.
  • Print the above result which is the area of the given polygon.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinates list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
a = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Give the y-coordinates list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
b = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given x-coordinates list and store it in another variable.
lst_lengt = len(a)
# Take a variable say polygn_area and initialize its value to 0.
polygn_area = 0
# Subtract 1 from the above x-coordinates list length and store it in another variable
# say k .
k = lst_lengt - 1
print("The Area of the given polygon for the given n ordered vertices :")
# Loop till the length of the given x-coordinates list using the for loop.
for itr in range(0, lst_lengt):
    # Calculate the area of the polygon using the above given mathematical formula and
    # add it to the above-initialized variable polygn_area.
    polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])
    # Assign the value of the iterator to the k which is the previous vertex
    # to the iterator.
    k = itr
# Divide the above polygn_area by 2, apply the abs() function to it which gives
# the absolute value, and convert it to an integer using the int() function.
# Store it in another variable.
rslt = int(abs(polygn_area / 2.0))
# Print the above result which is the area of the given polygon.
print(rslt)

Output:

Enter some random List Elements separated by spaces = 2 3 3 1
Enter some random List Elements separated by spaces = 9 2 1 2
The Area of the given polygon for the given n ordered vertices :
8

 

Python Program for How To End Program

Program for How To End Program

In Python, we can use one of the following methods to exit the program.

  1. sys.exit()
  2. quit()
  3. exit()

All of these functions have nearly identical functionality because they raise the SystemExit exception, which is the reason the Python interpreter exists, and no stack traceback is printed.

When we run a program in Python, we execute every line of code in the file, from top to bottom. Scripts normally terminate when the interpreter reaches the end of the file, but the execution can also be terminated programmatically.

Program for How To End Program in Python

1. sys.exit() Method:

In Python, use the sys. exit() function to exit the program. To exit the program, use the sys module’s built-in function sys. exit(). The sys. exit() function can be called at any time without fear of causing code corruption.

Syntax:

sys.exit([arg])

The sys. exit() function accepts an optional argument arg, which can be an integer representing the exit or another type of object.

Examples:

Example1:

import sys
gvn_numb = 10
if gvn_numb != 15:
    sys.exit("Number mismatch.Exiting from program")
else:
    print("The given numbers are matched")

Output:

Number mismatch.Exiting from program

Example2:

Input:

import sys
gvn_numb = 15
if gvn_numb != 15:
    sys.exit("Number mismatch.Exiting from program")
else:
    print("The given numbers are matched")

Output:

The given numbers are matched

2.quit() Method:

Use the quit() function to exit the Python program. quit() is a built-in Python function that raises the SystemExit exception and displays a message.

Syntax:

quit()

Examples:

Example1:

for itr in range(2, 6):
    print(itr*5)
    quit()

Output:

10

Example2:

Input:

for itr in range(1, 12):
    if(itr % 2 == 0):
        print(itr, end=" ")
        quit()

Output:

2

3.exit() Method:

In Python, use the exit() function to exit the program. The exit() function in Python is a built-in function that exits the program’s execution loop.

Syntax:

exit()

The exit() function is an alternative to the quit() function, which terminates the program’s execution.

That is for terminating a Python program.

Example1:

for itor in range(10):
    if itor == 5:
        exit()
    print(itor)

Output:

0
1
2
3
4

Example2:

for itor in range(20):
    if itor % 2 != 0 and itor > 8:
        exit()
    print(itor)

Output:

0
1
2
3
4
5
6
7
8