0% found this document useful (0 votes)
10 views

Python Uses-Part 2

Uploaded by

pecem29366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Uses-Part 2

Uploaded by

pecem29366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Automation and Scripting: Python is often used for writing scripts to automate tasks.

python

 import os
import shutil

# Define the source and destination directories


source_dir = "/path/to/source"
dest_dir = "/path/to/destination"

# Create the destination directory if it doesn't exist


os.makedirs(dest_dir, exist_ok=True)

# Copy files from source to destination


for filename in os.listdir(source_dir):
full_file_name = os.path.join(source_dir, filename)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, dest_dir)

print(f"Files copied from {source_dir} to {dest_dir}")

 Game Development: Python, with libraries like Pygame, can be used for game development.
python

 import pygame
import sys

pygame.init()

# Set up the game window


screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Simple Game")

# Main game loop


while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill((0, 0, 0)) # Fill the screen with black


pygame.display.flip() # Update the display

 Web Scraping: Python is also popular for web scraping with libraries like BeautifulSoup and
Scrapy.
python

6. import requests
from bs4 import BeautifulSoup

# Send a GET request to the website


url = "https://example.com"
response = requests.get(url)
# Parse the HTML content
soup = BeautifulSoup(response.content, "html.parser")

# Extract and print the title


title = soup.title.string
print(f"Title: {title}")

These are just a few examples of how Python can be used. Let me know if you need more detailed
examples or have specific applications in mind!

You might also like