Python, renowned for its simplicity and versatility, offers a vast array of programs that cater to various needs. Below is a curated list of Python programs along with illustrative examples to help you grasp their functionalities effortlessly.
1. Hello World Program
print("Hello, World!")
The quintessential beginner's program in Python, it simply prints "Hello, World!" to the console.
2. Calculator Program
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y
This program defines basic arithmetic operations such as addition, subtraction, multiplication, and division.
3. Fibonacci Series Program
def fibonacci(n): fib_series = [0, 1] for i in range(2, n): fib_series.append(fib_series[i-1] + fib_series[i-2]) return fib_series
Generates the Fibonacci series up to the specified number of terms 'n'.
4. Guessing Game Program
import random def guessing_game(): number = random.randint(1, 100) guess = int(input("Guess the number (between 1 and 100): ")) while guess != number: if guess number: print("Too low. Try again.") else: print("Too high. Try again.") guess = int(input("Guess again: ")) print("Congratulations! You guessed the correct number.") guessing_game()
Interactive game where the user guesses a randomly generated number.
5. File Handling Program
# Writing to a file with open('example.txt', 'w') as file: file.write("This is an example text.") # Reading from a file with open('example.txt', 'r') as file: content = file.read() print(content)
Demonstrates writing and reading text to and from a file.
6. Web Scraping Program
import requests from bs4 import BeautifulSoup url = 'https://www.example.com' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') print(soup.title)
Scrapes the title of a webpage using BeautifulSoup.
7. Basic GUI Program
import tkinter as tk root = tk.Tk() root.title("Simple GUI") label = tk.Label(root, text="Hello, GUI!") label.pack() root.mainloop()
Creates a simple graphical user interface with a greeting message.
8. Data Visualization Program
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Line Plot') plt.show()
Plots a simple line graph using Matplotlib.
9. Sending Email Program
import smtplib from email.mime.text import MIMEText def send_email(): sender_email = 'your_email@gmail.com' receiver_email = 'recipient_email@gmail.com' message = MIMEText('This is a test email.') message['Subject'] = 'Test Email' message['From'] = sender_email message['To'] = receiver_email with smtplib.SMTP('smtp.gmail.com', 587) as server: server.starttls() server.login(sender_email, 'your_password') server.sendmail(sender_email, receiver_email, message.as_string()) send_email()
Sends a test email using the SMTP protocol.
10. Database Connectivity Program
import sqlite3 # Connect to SQLite database conn = sqlite3.connect('example.db') # Create a cursor object cursor = conn.cursor() # Execute SQL query cursor.execute('''CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''') # Insert data into the table cursor.execute("INSERT INTO students (name, age) VALUES ('John', 25)") # Commit changes and close connection conn.commit() conn.close()
Demonstrates basic database connectivity and CRUD operations with SQLite.
These examples serve as a springboard for your Python journey. Experiment, explore, and unlock the vast potential of Python programming!