15 Minimal Python Tasks for Practice and Skill Building
This article offers fifteen concise Python exercises—ranging from duplicate detection and anagram testing to memory inspection, byte‑size calculation, string repetition, title‑casing, list chunking, filtering falsy values, unpacking, chained comparisons, comma joining, vowel counting, decapitalizing, list flattening, and list difference—to help beginners and developers sharpen their coding skills through hands‑on practice.
Learning Python effectively requires hands‑on mini‑projects; this article presents fifteen concise tasks that illustrate common operations such as duplicate detection, anagram checking, memory inspection, byte size calculation, string repetition, title‑casing, list chunking, filtering falsy values, unpacking, chained comparisons, comma joining, vowel counting, decapitalizing, list flattening, and list difference.
1. Duplicate Element Check
The following function returns True if all elements in a list are unique, otherwise False .
<code>def all_unique(lst):
return len(lst) == len(set(lst))
x = [1,1,2,2,3,2,3,4,5,6]
y = [1,2,3,4,5]
print(all_unique(x)) # False
print(all_unique(y)) # True</code>2. Anagram (Character Composition) Check
Uses collections.Counter to compare the character frequencies of two strings.
<code>from collections import Counter
def anagram(first, second):
return Counter(first) == Counter(second)
print(anagram("abcd3", "3acdb")) # True</code>3. Memory Usage
Shows how to obtain the memory size of a variable using sys.getsizeof .
<code>import sys
variable = 30
print(sys.getsizeof(variable)) # 24</code>4. Byte Size of a String
Calculates the number of bytes a string occupies when encoded in UTF‑8.
<code>def byte_size(string):
return len(string.encode('utf-8'))
print(byte_size('😀')) # 4
print(byte_size('Hello World')) # 11</code>5. Print a String N Times Without a Loop
<code>n = 2
s = "Programming"
print(s * n) # ProgrammingProgramming</code>6. Capitalize First Letter of Each Word
<code>s = "programming is awesome"
print(s.title()) # Programming Is Awesome</code>7. List Chunking
Splits a list into sub‑lists of a given size.
<code>from math import ceil
def chunk(lst, size):
return list(
map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(lst) / size)))))
print(chunk([1,2,3,4,5], 2)) # [[1, 2], [3, 4], [5]]</code>8. Compact (Remove Falsy Values)
Filters out falsy values like False , None , 0 , and empty strings.
<code>def compact(lst):
return list(filter(bool, lst))
print(compact([0, 1, False, 2, '', 3, 'a', 's', 34])) # [1, 2, 3, 'a', 's', 34]</code>9. Unpacking Paired Lists
<code>array = [['a', 'b'], ['c', 'd'], ['e', 'f']]
transposed = zip(*array)
print(list(transposed)) # [('a', 'c', 'e'), ('b', 'd', 'f')]
</code>10. Chained Comparisons
<code>a = 3
print(2 < a < 8) # True
print(1 == a < 2) # False
</code>11. Join List Elements with Commas
<code>hobbies = ["basketball", "football", "swimming"]
print("My hobbies are: " + ", ".join(hobbies))
# My hobbies are: basketball, football, swimming
</code>12. Vowel Count Using Regular Expressions
<code>import re
def count_vowels(s):
return len(re.findall(r'[aeiou]', s, re.IGNORECASE))
print(count_vowels('foobar')) # 3
print(count_vowels('gym')) # 0
</code>13. Decapitalize First Character
<code>def decapitalize(string):
return string[:1].lower() + string[1:]
print(decapitalize('FooBar')) # fooBar
</code>14. Flatten a Nested List
<code>def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def deep_flatten(lst):
result = []
result.extend(
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
return result
print(deep_flatten([1, [2], [[3], 4], 5])) # [1, 2, 3, 4, 5]
</code>15. List Difference
Returns elements present in the first list but not in the second.
<code>def difference(a, b):
set_a = set(a)
set_b = set(b)
return list(set_a.difference(set_b))
print(difference([1,2,3], [1,2,4])) # [3]
</code>These snippets provide quick, practical examples for Python learners to experiment with and adapt to their own projects.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.