From 0db07f96647eb6602ead99116043e2b887a276cf Mon Sep 17 00:00:00 2001 From: Beata Date: Thu, 9 May 2024 19:25:02 +0300 Subject: [PATCH 01/15] Add .discard() method. --- .../concepts/sets/terms/discard/discard.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 content/python/concepts/sets/terms/discard/discard.md diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md new file mode 100644 index 00000000000..f6cbcc0cd62 --- /dev/null +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -0,0 +1,65 @@ +--- +Title: '.discard()' +Description: 'Removes a passing element from the set if it's present.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Collections' + - 'Functions' + - 'Methods' + - 'Sets' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The `.discard()` method removes the specified item from the set of objects. + +## Syntax + +The syntax is the following: + +``` +set_name.discard(value) +``` +- This method takes exactly one argument; + +- It differs from the `.remove()` method because it **will not raise a KeyError** if the specified item doesn't exist in the set. + + +## Example + +The example below shows possible usage of the method: + +```py +coffee_set = {'espresso', 'flat_white', 'cappuccino', 'filter'} +print(coffee_set) + +coffee_set.discard('espresso') +print(coffee_set) + +coffee_set.discard('aeropress') +print(coffee_set) +``` + +The output would be the following: + +```py +{'espresso', 'flat_white', 'cappuccino', 'filter'} +{'flat_white', 'cappuccino', 'filter'} +{'flat_white', 'cappuccino', 'filter'} +``` + +## Codebyte Example + +In the example, we see that our code continues to execute though there are already no coffee beans from Vietnam in our set. + +```codebyte/python +coffee_beans = {'Brazil', 'Ethiopia', 'Kenya', 'Columbia'} +print(coffee_beans) + +coffee_beans.discard('Vietnam') +coffee_beans.discard('Brazil') +print(coffee_beans) +``` \ No newline at end of file From f56d94b19326d65f9bb69de48b554d1fc4881d20 Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:05:03 +0300 Subject: [PATCH 02/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index f6cbcc0cd62..b7fa7104423 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -1,14 +1,14 @@ --- Title: '.discard()' -Description: 'Removes a passing element from the set if it's present.' -Subjects: +Description: 'Removes a specified element from the set if it exists.' +Subjects: + - 'Code Foundations' - 'Computer Science' - - 'Data Science' -Tags: +Tags: - 'Collections' - - 'Functions' - 'Methods' - 'Sets' + - 'Strings' CatalogContent: - 'learn-python-3' - 'paths/computer-science' From 9017c736da1f6f09935a9f356fabf32f983c829f Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:05:43 +0300 Subject: [PATCH 03/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index b7fa7104423..83ac0ba01e2 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -The `.discard()` method removes the specified item from the set of objects. +In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/resources/docs/python/sets) removes a specified element if present; however, if the element is not found, it takes no action and does not raise an error. ## Syntax From cee90d79ced96e0fb89e4942b51f20a9bf532c5d Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:06:12 +0300 Subject: [PATCH 04/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 83ac0ba01e2..bcff5d05ac7 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -18,8 +18,6 @@ In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/reso ## Syntax -The syntax is the following: - ``` set_name.discard(value) ``` From 2b2cd4d607bae79c26fc442fcec68addce90c48d Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:06:30 +0300 Subject: [PATCH 05/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index bcff5d05ac7..5e4633766aa 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -18,7 +18,7 @@ In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/reso ## Syntax -``` +```pseudo set_name.discard(value) ``` - This method takes exactly one argument; From e467fd4887093900bd51f586a3791a9043b0131f Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:06:45 +0300 Subject: [PATCH 06/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 5e4633766aa..afbbdefedab 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -19,7 +19,7 @@ In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/reso ## Syntax ```pseudo -set_name.discard(value) +set.discard(value) ``` - This method takes exactly one argument; From 7fc532c15ba8796e5bd9554e3925521cc6a3b799 Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:07:17 +0300 Subject: [PATCH 07/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index afbbdefedab..af732ff9871 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -34,10 +34,12 @@ The example below shows possible usage of the method: coffee_set = {'espresso', 'flat_white', 'cappuccino', 'filter'} print(coffee_set) -coffee_set.discard('espresso') +# removing 'espresso' from the set +coffee_set.discard('espresso') print(coffee_set) -coffee_set.discard('aeropress') +# removing 'aeropress' from the set +coffee_set.discard('aeropress') print(coffee_set) ``` From 919733716bf070dd1753c05c25c2aebfb6e8805d Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:07:34 +0300 Subject: [PATCH 08/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index af732ff9871..4401741e64e 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -43,7 +43,7 @@ coffee_set.discard('aeropress') print(coffee_set) ``` -The output would be the following: +The code above produces the following output: ```py {'espresso', 'flat_white', 'cappuccino', 'filter'} From 4b8ed1f3fddaa52a450b5726877c18f05f6a749d Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:07:45 +0300 Subject: [PATCH 09/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 4401741e64e..2d3fdfeeee6 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -45,7 +45,7 @@ print(coffee_set) The code above produces the following output: -```py +```shell {'espresso', 'flat_white', 'cappuccino', 'filter'} {'flat_white', 'cappuccino', 'filter'} {'flat_white', 'cappuccino', 'filter'} From d5063d891fcae5740c5c3574189dd977dab97c86 Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:08:29 +0300 Subject: [PATCH 10/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 2d3fdfeeee6..84c99200952 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -53,7 +53,7 @@ The code above produces the following output: ## Codebyte Example -In the example, we see that our code continues to execute though there are already no coffee beans from Vietnam in our set. +In the example, our code continues to execute without errors even though we attempt to remove elements ('Vietnam' and 'Brazil') from the set coffee_beans that are not present in the set. ```codebyte/python coffee_beans = {'Brazil', 'Ethiopia', 'Kenya', 'Columbia'} From 49a8f12af8df81ce948eda433b2684e741d6d03d Mon Sep 17 00:00:00 2001 From: Beata <168515009+BeateBB@users.noreply.github.com> Date: Sat, 11 May 2024 15:08:53 +0300 Subject: [PATCH 11/15] Update content/python/concepts/sets/terms/discard/discard.md Co-authored-by: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> --- content/python/concepts/sets/terms/discard/discard.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 84c99200952..0aee4f7bd05 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -61,5 +61,4 @@ print(coffee_beans) coffee_beans.discard('Vietnam') coffee_beans.discard('Brazil') -print(coffee_beans) -``` \ No newline at end of file +print(coffee_beans) \ No newline at end of file From 19e976e2713b4dae3266153f5b4fe304ab7490e7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> Date: Sat, 11 May 2024 18:06:36 +0530 Subject: [PATCH 12/15] Update discard.md --- content/python/concepts/sets/terms/discard/discard.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 0aee4f7bd05..0431cd454cb 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -21,10 +21,9 @@ In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/reso ```pseudo set.discard(value) ``` -- This method takes exactly one argument; - -- It differs from the `.remove()` method because it **will not raise a KeyError** if the specified item doesn't exist in the set. +- `set`: This refers to the set from which the specified element is to be removed. +- `value`: This denotes the element that should be removed from the set. ## Example @@ -61,4 +60,5 @@ print(coffee_beans) coffee_beans.discard('Vietnam') coffee_beans.discard('Brazil') -print(coffee_beans) \ No newline at end of file +print(coffee_beans) +``` From 215a0b1043e1a80d848c6c6edfe256c7e3826018 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> Date: Sat, 11 May 2024 18:09:12 +0530 Subject: [PATCH 13/15] Update discard.md --- content/python/concepts/sets/terms/discard/discard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 0431cd454cb..b8d51dc3421 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -27,7 +27,7 @@ set.discard(value) ## Example -The example below shows possible usage of the method: +The example below shows the usage of the `.discard()` method: ```py coffee_set = {'espresso', 'flat_white', 'cappuccino', 'filter'} From 5ba5b736814054c1dc676abb3a9fb60687a83a61 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani <53176352+mamtawardhani@users.noreply.github.com> Date: Sat, 11 May 2024 18:14:07 +0530 Subject: [PATCH 14/15] Update discard.md --- content/python/concepts/sets/terms/discard/discard.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index b8d51dc3421..864035a53ac 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -22,7 +22,7 @@ In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/reso set.discard(value) ``` -- `set`: This refers to the set from which the specified element is to be removed. +- `set`: Refers to the set from which the specified element is to be removed. - `value`: This denotes the element that should be removed from the set. ## Example @@ -37,8 +37,8 @@ print(coffee_set) coffee_set.discard('espresso') print(coffee_set) -# removing 'aeropress' from the set -coffee_set.discard('aeropress') +# removing 'latte' from the set +coffee_set.discard('latte') print(coffee_set) ``` @@ -50,9 +50,9 @@ The code above produces the following output: {'flat_white', 'cappuccino', 'filter'} ``` -## Codebyte Example +In the example, our code continues to execute without errors even though we attempt to remove the element 'latte' which doesn't even exist in the set. -In the example, our code continues to execute without errors even though we attempt to remove elements ('Vietnam' and 'Brazil') from the set coffee_beans that are not present in the set. +## Codebyte Example ```codebyte/python coffee_beans = {'Brazil', 'Ethiopia', 'Kenya', 'Columbia'} From c0b48cc2fd220038ccc3dbe75daed2dad5845c19 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Wed, 15 May 2024 21:41:22 +0530 Subject: [PATCH 15/15] Minor changes --- .../concepts/sets/terms/discard/discard.md | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/content/python/concepts/sets/terms/discard/discard.md b/content/python/concepts/sets/terms/discard/discard.md index 864035a53ac..c3760fe45ef 100644 --- a/content/python/concepts/sets/terms/discard/discard.md +++ b/content/python/concepts/sets/terms/discard/discard.md @@ -1,12 +1,12 @@ --- Title: '.discard()' -Description: 'Removes a specified element from the set if it exists.' +Description: 'Removes a specified element from a set.' Subjects: - - 'Code Foundations' - 'Computer Science' + - 'Code Foundations' Tags: - - 'Collections' - 'Methods' + - 'Collections' - 'Sets' - 'Strings' CatalogContent: @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In Python, the **`.discard()`** method in [sets](https://www.codecademy.com/resources/docs/python/sets) removes a specified element if present; however, if the element is not found, it takes no action and does not raise an error. +In Python, the **`.discard()`** method removes a specified element from a [set](https://www.codecademy.com/resources/docs/python/sets). If the element is not found, it takes no action and does not raise an [error](https://www.codecademy.com/resources/docs/python/errors) either. ## Syntax @@ -23,26 +23,26 @@ set.discard(value) ``` - `set`: Refers to the set from which the specified element is to be removed. -- `value`: This denotes the element that should be removed from the set. +- `value`: Denotes the element to be removed from the set. ## Example -The example below shows the usage of the `.discard()` method: +The below example shows the usage of the `.discard()` method: ```py coffee_set = {'espresso', 'flat_white', 'cappuccino', 'filter'} print(coffee_set) -# removing 'espresso' from the set -coffee_set.discard('espresso') +# Removing 'espresso' from the set +coffee_set.discard('espresso') print(coffee_set) -# removing 'latte' from the set -coffee_set.discard('latte') +# Removing 'latte' from the set +coffee_set.discard('latte') print(coffee_set) ``` -The code above produces the following output: +The above code produces the following output: ```shell {'espresso', 'flat_white', 'cappuccino', 'filter'} @@ -50,10 +50,12 @@ The code above produces the following output: {'flat_white', 'cappuccino', 'filter'} ``` -In the example, our code continues to execute without errors even though we attempt to remove the element 'latte' which doesn't even exist in the set. +In the above example, the code continues to get executed without any errors despite the attempt to remove the element `latte`, which doesn't even exist in the set. ## Codebyte Example +Here is a codebyte example demonstrating the use of the `.discard()` method: + ```codebyte/python coffee_beans = {'Brazil', 'Ethiopia', 'Kenya', 'Columbia'} print(coffee_beans)