Remove an element from a set

Python Practice › Set Programs › Program 3

Remove an element from a set

EasySet Programs · 3 of 48

Write a Python program to remove an element from a set.

Input
{10, 20, 30}
Output
{10, 30}

Approach

  1. remove() raises KeyError if missing; discard() does not.

Solution

Python
s = {10, 20, 30}s.remove(20)s.discard(99)print(s)
Output
{10, 30}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Add elements to a set

Python Practice › Set Programs › Program 2

Add elements to a set

EasySet Programs · 2 of 48

Write a Python program to add elements to a set.

Input
{1, 2}
Output
{1, 2, 3}

Approach

  1. add() for one item; adding an existing item does nothing.

Solution

Python
s = {1, 2}s.add(3)s.add(2)print(s)
Output
{1, 2, 3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Create a set

Python Practice › Set Programs › Program 1

Create a set

EasySet Programs · 1 of 48

Write a Python program to create a set and show that duplicates are removed.

Output
{8, 3, 5} 3
set 0

Approach

  1. Use curly braces with values, or set() for an empty set.

Solution

Python
numbers = {5, 3, 8, 3, 5}empty = set()print(numbers, len(numbers))print(type(empty).__name__, len(empty))
Output
{8, 3, 5} 3
set 0

Line by line

Line 1

The repeated 3 and 5 are stored once.

Line 2

{} would create a dictionary, so an empty set needs set().

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Python Set Programs with Solutions

Python Practice › Set Programs

Python Set Programs with Solutions

48 programs48 easy0 medium0 hard

48 set programs: creating sets, adding and removing items, union, intersection, difference, subsets and frozensets. Sets of strings have no fixed order, so these solutions print sorted(...) whenever the order would otherwise change from run to run.

Before you start

Read the Sets lesson first if the topic is new to you.

All programs

Sort lines by length

Sort lines by length

EasyFile Handling Programs · 15 of 38

Write a Python program to sort the lines of a file by their length.

Output
x
short
medium line
a much longer line here

Approach

  1. sorted(lines, key=len).

Solution

Python
with open("story.txt", "w") as f:    f.write("medium line\nx\na much longer line here\nshort\n") with open("story.txt") as f:    lines = f.read().splitlines()for line in sorted(lines, key=len):    print(line)
Output
x
short
medium line
a much longer line here

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Replace spaces with underscores

Replace spaces with underscores

EasyFile Handling Programs · 38 of 38

Write a Python program to replace every space in a file with an underscore.

Output
learn_python_at_sqatools
file_handling_demo

Approach

  1. Read, replace, write back.

Solution

Python
with open("story.txt", "w") as f:    f.write("learn python at sqatools\nfile handling demo\n") with open("story.txt") as f:    text = f.read()with open("story.txt", "w") as f:    f.write(text.replace(" ", "_"))print(open("story.txt").read())
Output
learn_python_at_sqatools
file_handling_demo

Line by line

Lines 4-7

The file is read completely before it is reopened in “w” mode, because “w” empties it immediately.

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Words shorter than 5 characters

Words shorter than 5 characters

EasyFile Handling Programs · 37 of 38

Write a Python program to display the words of a file that have fewer than 5 characters.

Output
['is', 'a', 'very', 'good', 'tool', 'for', 'test']

Approach

  1. Filter words by len(w) < 5.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python is a very good tool for test automation\n") with open("story.txt") as f:    print([w for w in f.read().split() if len(w) < 5])
Output
['is', 'a', 'very', 'good', 'tool', 'for', 'test']

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Drop lines containing ‘t’

Drop lines containing 't'

EasyFile Handling Programs · 36 of 38

Write a Python program to copy a file to another file, skipping lines that contain the letter “t”.

Output
apple
cherry
melon

Approach

  1. Write only lines where "t" not in line.lower().

Solution

Python
with open("source.txt", "w") as f:    f.write("apple\nbutter\ncherry\nmelon\ntea\n") with open("source.txt") as src, open("filtered.txt", "w") as dst:    for line in src:        if "t" not in line.lower():            dst.write(line)print(open("filtered.txt").read())
Output
apple
cherry
melon

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Count consonants in a file

Count consonants in a file

EasyFile Handling Programs · 35 of 38

Write a Python program to count the consonants in a file.

Output
16

Approach

  1. Letters that are not vowels.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python file handling is easy\n") with open("story.txt") as f:    text = f.read().lower()print(sum(ch.isalpha() and ch not in "aeiou" for ch in text))
Output
16

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Count vowels in a file

Count vowels in a file

EasyFile Handling Programs · 34 of 38

Write a Python program to count the vowels in a file.

Output
8

Approach

  1. Count characters in “aeiou” after lower-casing.

Solution

Python
with open("story.txt", "w") as f:    f.write("Python file handling is easy\n") with open("story.txt") as f:    print(sum(ch in "aeiou" for ch in f.read().lower()))
Output
8

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs