Utility Programs and Miscellaneous Examples

A grab bag of practical snippets—calendars, shuffling cards, mail merge, timers, and more. Great for honing problem-solving skills and building a personal toolbox of Python tricks.

Python Program to Split a List Into Evenly Sized Chunks

Chunking utility.

def chunks(lst, n):
    return [lst[i:i+n] for i in range(0, len(lst), n)]
print(chunks([1,2,3,4,5], 2))

Output

[[1, 2], [3, 4], [5]]

Python Program to Shuffle Deck of Cards

Shuffles deck deterministically.

import random
random.seed(0)
suits = ['H','D','C','S']
ranks = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
deck = [r+s for s in suits for r in ranks]
random.shuffle(deck)
print(deck[:5])

Output

['7C', '5H', 'QD', '2S', '3D']

Python Program to Display Calendar

Shows month calendar.

import calendar
print(calendar.month(2025, 1))

Output

    January 2025
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Python Program to Merge Mails

Simple mail merge.

names = ['Ann','Bob']
template = 'Hello {name}, welcome.'
msgs = [template.format(name=n) for n in names]
print('\n'.join(msgs))

Output

Hello Ann, welcome.
Hello Bob, welcome.

Python Program to Find the Size (Resolution) of an Image

Uses Pillow for image size.

from PIL import Image
img = Image.new('RGB', (640, 480))
print(img.size)

Output

(640, 480)

Python Program to Merge Two Dictionaries

Merges using unpack.

a = {'x':1}
b = {'y':2}
c = {**a, **b}
print(c)

Output

{'x': 1, 'y': 2}

Python Program to Access Index of a List Using for Loop

Uses enumerate.

lst = ['a','b','c']
for i, v in enumerate(lst):
    print(i, v)

Output

0 a
1 b
2 c

Python Program to Parse a String to a Float or Int

Converts string.

s = '3.14'
print(float(s))
print(int(float(s)))

Output

3.14
3

Python Program to Get the Last Element of the List

Index -1.

print([1,2,3][-1])

Output

3

Python Program to Randomly Select an Element From the List

Uses random.choice.

import random
random.seed(0)
print(random.choice([10,20,30]))

Output

20

Python Program to Get the Class Name of an Instance

Uses __class__.__name__.

class A: pass
print(A().__class__.__name__)

Output

A

Python Program to Reverse a Number

Reverses digits.

n = 1234
print(int(str(n)[::-1]))

Output

4321

Python Program to Check If Two Strings are Anagram

Compares sorted letters.

def anagram(a,b):
    return sorted(a.replace(' ','').lower()) == sorted(b.replace(' ','').lower())
print(anagram('listen','silent'))

Output

True

Python Program to Compute all the Permutation of the String

Uses itertools.permutations.

import itertools
s = 'abc'
perms = [''.join(p) for p in itertools.permutations(s)]
print(perms)

Output

['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

Python Program to Create a Countdown Timer

Simple countdown.

for i in range(3,0,-1):
    print(i)

Output

3
2
1

Python Program to Convert Bytes to a String

Decodes bytes.

print(b'hello'.decode('utf-8'))

Output

hello

Python Program to Find the Square Root

Computes square root using math module.

import math
print(math.sqrt(16))

Output

4.0

Keep Practicing

Use the online Python compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

You can use the online Python compiler linked above to run examples instantly. For larger projects, install Python locally.

Yes. Examples cover variables, control flow, data structures, functions, classes, and file I/O—ideal for beginners.

Yes. The programs are tested and produce consistent output across major platforms.

Learn Python by Practicing Examples

Hands-on practice is the fastest way to understand Python. Each example focuses on a single concept—from variables, strings, lists, and dictionaries to loops, functions, classes, exceptions, and file I/O. Modify examples, add new functions, and see how clean design leads to readable, maintainable code.

Use our tools to deepen learning: the Python Compiler to run snippets, and the Python Tutorial for structured lessons.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with small, readable programs.

Practical Patterns

Practice lists, dicts, comprehensions, functions, classes, exceptions, and file handling.

Grow Skills

Explore generators and decorators to write expressive, reusable code.