DEV Community

Chandru
Chandru

Posted on

Python Beginner Projects

After learning Python variables and data types, the next step is to understand how we can use them in small projects.

1. Random Credit Card Test Number Generator

This program generates a random 16-digit number that resembles a credit card number.

import random

card_number = ""

for i in range(16):
    card_number += str(random.randint(0, 9))

print("Generated Test Number:", card_number)
Enter fullscreen mode Exit fullscreen mode

How it works

  • random.randint(0, 9) generates a random digit.
  • str() converts the digit into a string.
  • card_number stores the digits as text.
  • The for loop repeats 16 times to create a 16-digit number.

output:

Generated Test Number: 4839201746502831
Enter fullscreen mode Exit fullscreen mode

This is only a random number for learning or testing. It is not necessarily a valid payment card number and cannot be used to make purchases.

2. Random Color Generator

Ever wondered how websites generate random colors?

Python can do that too!

import random

color = "#"

for i in range(6):
    color += random.choice("0123456789ABCDEF")

print("Random Color:", color)
Enter fullscreen mode Exit fullscreen mode

How it works

A HEX color code contains six hexadecimal characters, usually written after a # symbol.

For example, #A3F5B2 represents a color.

The random.choice() function selects one random character from the given string. The loop repeats six times to generate the complete color code.

output:

Random Color: #A3F5B2
Enter fullscreen mode Exit fullscreen mode

You can copy the generated HEX code and use it in your CSS.

3. Simple URL Shortener

A URL shortener converts a long web address into a shorter link.

In this example, we'll generate a random short code for the entered URL.

import random
import string

url = input("Enter a URL: ")

short_code = ""

characters = string.ascii_letters + string.digits

for i in range(6):
    short_code += random.choice(characters)

short_url = "short.ly/" + short_code

print("Original URL:", url)
print("Short URL:", short_url)
Enter fullscreen mode Exit fullscreen mode

How it works

  • input() gets a URL from the user.
  • string.ascii_letters contains uppercase and lowercase letters.
  • string.digits contains numbers from 0 to 9.
  • random.choice() selects random characters.
  • The generated code is added to the short URL.

output:

Enter a URL: https://www.google.com

Original URL: https://www.google.com
Short URL: short.ly/aB12xY
Enter fullscreen mode Exit fullscreen mode

This is a local demonstration only. A real URL shortener needs a server and database to store the original URL and redirect visitors when they open the shortened link.

4. MD5 Hash Generator

Hashing is a process that converts input data into a fixed-length string.

Python provides the hashlib module, which supports several hashing algorithms.

Let's generate an MD5 hash:

import hashlib

text = input("Enter text: ")

md5_hash = hashlib.md5(text.encode())

print("MD5 Hash:", md5_hash.hexdigest())
Enter fullscreen mode Exit fullscreen mode

How it works

  • input() gets text from the user.
  • encode() converts the text into bytes.
  • hashlib.md5() generates the MD5 hash object.
  • hexdigest() returns the hash as a hexadecimal string.

output:

Enter text: hello

MD5 Hash: 5d41402abc4b2a76b9719d911017c592
Enter fullscreen mode Exit fullscreen mode

MD5 produces a 128-bit hash, commonly displayed as 32 hexadecimal characters.

Top comments (0)