Practical Python Scripts for Beginners: From Hello World to Mini Games
This article introduces a series of beginner-friendly Python scripts, ranging from the classic 'Hello, World!' program to interactive games, calculators, web scrapers, and utility tools, each explained with clear examples and step-by-step code to help newcomers grasp fundamental programming concepts.
This tutorial presents a collection of practical Python scripts designed for absolute beginners. It starts with the iconic "Hello, World!" program, then moves on to a simple calculator, a number‑guessing game, rock‑paper‑scissors, a to‑do list manager, a basic web scraper, a weather app, and many other useful utilities.
Each script is accompanied by a concise explanation that relates programming concepts to everyday analogies, making the material easy to understand.
# Hello, World! program
print("Hello, World!") # Simple calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 == 0:
print("Cannot divide by zero")
else:
result = num1 / num2
else:
print("Invalid operator")
if "result" in locals():
print("Result:", result) # Guess the number game
import random
number = random.randint(1, 100)
guess = 0
while guess != number:
guess = int(input("Guess a number between 1 and 100: "))
if guess < number:
print("Too low, try again!")
elif guess > number:
print("Too high, try again!")
else:
print("Congratulations, you guessed it!") # Rock‑paper‑scissors game
import random
choices = ["石头", "剪刀", "布"]
computer_choice = random.choice(choices)
user_choice = input("Enter your choice (石头, 剪刀, 布): ")
print("Your choice:", user_choice)
print("Computer's choice:", computer_choice)
# ... determine winner ... # To‑do list manager
todo_list = []
while True:
action = input("Enter action (add, view, exit): ")
if action == "add":
item = input("Enter task: ")
todo_list.append(item)
elif action == "view":
for item in todo_list:
print(item)
elif action == "exit":
break
else:
print("Invalid action") # Basic web scraper
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
title = soup.title.string
print("Page title:", title)
links = soup.find_all("a")
for link in links:
print(link.get("href"))
# pip install requests beautifulsoup4 # Simple weather app (requires API key)
import requests
city = input("Enter city: ")
api_key = "YOUR_API_KEY"
url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}"
response = requests.get(url)
weather_data = response.json()
if "current" in weather_data:
temperature = weather_data["current"]["temp_c"]
condition = weather_data["current"]["condition"]["text"]
print(f"{city} temperature: {temperature}°C, condition: {condition}")
else:
print("Failed to retrieve weather information")
# pip install requestsAdditional scripts cover topics such as prime number checking, Fibonacci sequence generation, password generation, email scheduling, file organization, QR‑code creation, batch renaming, a simple HTTP server, and YouTube video downloading, providing a well‑rounded introduction to Python's capabilities.
By following these examples, readers can quickly build confidence in writing Python code and gain a solid foundation for further learning.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
