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)
How it works
-
random.randint(0, 9)generates a random digit. -
str()converts the digit into a string. -
card_numberstores the digits as text. - The
forloop repeats 16 times to create a 16-digit number.
output:
Generated Test Number: 4839201746502831
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)
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
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)
How it works
-
input()gets a URL from the user. -
string.ascii_letterscontains uppercase and lowercase letters. -
string.digitscontains 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
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())
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
MD5 produces a 128-bit hash, commonly displayed as 32 hexadecimal characters.
Top comments (0)