Comprehensive Collection of Python Mini-Program Code Examples

Python, a popular high-level programming language, is widely used for various applications due to its simplicity, readability, and vast library support. In this blog post, we’ll explore a comprehensive collection of Python mini-program code examples that demonstrate the versatility of the language.

1. Simple “Hello, World!” Program

The most basic program in any programming language is the “Hello, World!” program. Here’s the Python version:

pythonprint("Hello, World!")

2. FizzBuzz Challenge

The FizzBuzz program is a popular programming challenge. It iterates over a range of numbers and prints specific messages based on divisibility rules:

pythonfor i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

3. Guessing Game

A simple guessing game where the user has to guess a randomly generated number:

pythonimport random

number = random.randint(1, 100)
guess = None
attempts = 0

while guess != number:
guess = int(input("Guess a number between 1 and 100: "))
attempts += 1
if guess < number:
print("Too low. Try again.")
elif guess > number:
print("Too high. Try again.")

print(f"Congratulations! You guessed the number correctly in {attempts} attempts.")

4. List Manipulation

Demonstrating basic list manipulation operations in Python:

python# Create a list
my_list = [1, 2, 3, 4, 5]

# Append an element
my_list.append(6)

# Reverse the list
my_list.reverse()

# Sort the list
my_list.sort()

# Print the list
print(my_list)

5. Web Scraping with BeautifulSoup

Scraping data from a web page using the BeautifulSoup library:

pythonfrom bs4 import BeautifulSoup
import requests

url = "https://example.com" # Replace with the desired URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Example: Scraping all the titles from a web page
titles = soup.find_all('title')
for title in titles:
print(title.get_text())

6. Simple Calculator

A basic command-line calculator that performs addition, subtraction, multiplication, and division:

pythondef calculator():
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
choice = input("Enter choice(1/2/3/4): ")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
print(num1, "+", num2, "=", num1 + num2)

elif choice == '2':
print(num1, "-", num2, "=", num1 - num2)

elif choice == '3':
print(num1, "*", num2, "=", num1 * num2)

elif choice == '4':
if num2 != 0:
print(num1, "/", num2, "=", num1 / num2)
else:
print("Error! Division by zero is not allowed.")

else:
print("Invalid choice")
break

calculator()

These are just a few examples of the vast array of mini-programs that can be created using Python. From simple command-line applications to web scraping and data analysis, Python’s intuitive syntax and robust libraries make it a powerful tool for developers of all levels.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *