Author name: Vikram Chiluka

Python Program for trunc() Function

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

The math.trunc() method returns the integer part of a number that has been truncated.

Note: Please keep in mind that this method will NOT round the number up or down to the nearest integer, but will simply remove the decimals.

Syntax:

math.trunc(x)

Parameters:

x: This is required. It is a number. The number from which you want to remove the decimal point(truncate). If the value is not a number, a TypeError is returned.

Return Value: Returns an int value that represents a number’s truncated integer parts.

Examples:

Example1:

Input:

Given first number = 870.26
Given second number = -11.56

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11

Example2:

Input:

Given first number = 80.432
Given second number = 22.4

Output:

The truncated value of the given first number  80.432  =  80
The truncated value of the given second number  22.4  =  22

Program for trunc() Function in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Apply math.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() 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 list as static input and store it in a variable.
gvn_lst = [12.005, 11.89, 30.224]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 870.26
# Apply math.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = -11.56
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[2], " = ", math.trunc(gvn_lst[2]))

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11
The truncated value of the given list element  30.224  =  30

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

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Apply math.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() 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 list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[1], " = ", math.trunc(gvn_lst[1]))

Output:

Enter some random List Elements separated by spaces = 3 45.66 20.01 54
Enter some random number = 80.432
The truncated value of the given first number 80.432 = 80
Enter some random number = 22.4
The truncated value of the given second number 22.4 = 22
The truncated value of the given list element 45.66 = 45

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 trunc() Function Read More »

Python Program for How To Use While Not

In the previous article, we have discussed Python Program for trunc() Function
Iteration is the process of repeatedly running the same block of code, potentially many times. Loops are built using Python’s while statement.

In Python, a while loop is used to iterate over a block of code as long as the test condition) is True. The While Loop executes the set of statements as long as the condition is True. The while loop instructs a computer to perform an action as long as the condition is met or holds True.

Python’s While Not loop:

In Python, a while not loop executes the loop’s body until the condition for loop termination is met. If the condition evaluates to False, use the syntax while not condition with the condition as a boolean expression to execute the loop’s body.

Program for How To Use While Not in Python

Method #1: Using While Not Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 8
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

8
7
6
5
4
3
2
1

Method #2: Using While Not Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input('Enter some random number = '))
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

Enter some random number = 4
4
3
2
1

If the variable is not iterable, use the syntax “while variable not in” iterable to execute the loop’s body.

gvn_lst = [2, 6, 7, 3, 4]
while 8 not in gvn_lst:
    gvn_lst.append(len(gvn_lst) + 1)
print(gvn_lst)

output:

[2, 6, 7, 3, 4, 6, 7, 8]

break and continue statements in Python:

The Python break statement terminates a loop completely.

The Python continue statement terminates the current loop iteration immediately.

While else clause in Python:

We can use the else statement to run a block of code only once when the condition is no longer true.

Syntax:

while <expr>: 
    <statement(s)> 
else: 
    <additional_statement(s)>

When the while loop ends, the additional statement(s)> specified in the else clause will be executed.

Method #3: Using While else Clause (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is less than 8 using the while loop.
  • Inside the loop print the given number.
  • Increment the given number by 1 and store it in the same variable.
  • Else, print “The given number is greater than 8”.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 3
# Iterate till the given number is less than 8 using the while loop.
while gvn_numb < 8:
    # Inside the loop print the given number.
    print(gvn_numb)
    # Increment the given number by 1 and store it in the same variable.
    gvn_numb += 1
else:
    # Else, print "The given number is greater than 8".
    print("The given number is greater than 8")

Output:

3
4
5
6
7
The given number is greater than 8

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python Program for How To Use While Not Read More »

Python Program for Set sorted() Method

In the previous article, we have discussed Python Program for Set remove() 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', 'hello', 'btechgeeks', 'this'}
The given set after sorting the items in ascending order :
['btechgeeks', 'hello', 'is', 'this']

Explanation:

Here the items of the set are sorted in ascending order based upon the ASCII values.

Example2:

Input:

 Given set = {2, 3, 5, 10, 6, 12, 50, 1}

Output:

The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

Explanation:

Here the items of the set are sorted in descending order using the reverse= True condition
(based upon the ASCII values)

Program for Set sorted() Method in Python

set sorted() Method:

One of the set methods in Python is the set sorted() function, which is used to sort the given set items in ascending or descending order.

Syntax:

sorted(iterable,key,reverse)  

ex: sorted(setname) - for ascending order
     sorted(setname, reverse=True)- for descending order.

Parameter Values:

sorted() accepts three parameters, two of which are optional.

Iterable: a sequence (list, tuple, or string) or a collection (dictionary, set, or frozen set), or any other iterator that must be sorted.
Key(optional): A function that can be used as a key or as the basis for sort comparison.
Reverse(optional): If set to true, the iterable is sorted in reverse (descending) order; otherwise, it is set to false.

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 sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in ascending order.
  • 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 sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set)
# Print the given set after sorting all the items in ascending order.
print("The given set after sorting the items in ascending order :")
print(sortd_set)

Output:

The given set is :
{'this', 'hello', 'is', 'btechgeeks'}
The given set after sorting the items:
['btechgeeks', 'hello', '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.
  • Apply sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in descending order.
  • 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 sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set, reverse=True)
# Print the given set after sorting all the items in descending order.
print("The given set after sorting the items in descending order :")
print(sortd_set)

Output:

Enter some random values separated by spaces = 2 3 5 10 6 12 50 1
The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

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 sorted() Method Read More »

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 »