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

python_80_20_cheatsheet

This document is a Python 80/20 Cheatsheet covering essential programming concepts including variables, conditionals, loops, functions, data structures, list comprehensions, file handling, useful modules, quick scripts, templates, and a mini web scraper. It provides code snippets for each topic to illustrate usage. The cheatsheet serves as a quick reference for Python programming basics and common tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python_80_20_cheatsheet

This document is a Python 80/20 Cheatsheet covering essential programming concepts including variables, conditionals, loops, functions, data structures, list comprehensions, file handling, useful modules, quick scripts, templates, and a mini web scraper. It provides code snippets for each topic to illustrate usage. The cheatsheet serves as a quick reference for Python programming basics and common tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python 80/20 Cheatsheet

BASICS
# Variables
x = 10
name = "Alice"

# Conditionals
if x > 5:
print("Big")
elif x == 5:
print("Equal")
else:
print("Small")

# Loops
for i in range(3): # 0,1,2
print(i)

while True:
break

FUNCTIONS
def greet(name="world"):
return f"Hello, {name}!"

print(greet("Bob"))

DATA STRUCTURES
# Lists
nums = [1, 2, 3]
nums.append(4)

# Dictionary
person = {"name": "Ann", "age": 25}
person["city"] = "Tokyo"

# Set
uniq = set([1, 2, 2, 3]) # {1, 2, 3}

LIST COMPREHENSIONS
squares = [x**2 for x in range(5)]
evens = [x for x in range(10) if x % 2 == 0]

FILE HANDLING
with open("file.txt", "r") as f:
content = f.read()

with open("out.txt", "w") as f:


f.write("Hello!")

USEFUL MODULES
import os # File ops
import sys # Args
import math # sqrt, pi
import random # randint()
import datetime # now()

QUICK SCRIPT
import sys
name = sys.argv[1] if len(sys.argv) > 1 else "World"
print(f"Hi {name}")

TEMPLATES
# Timer
import time
start = time.time()
# ...code...
print("Done in", time.time() - start)

# CLI Tool
def main():
name = input("Name? ")
print(f"Hello, {name}")

if __name__ == "__main__":
main()

MINI WEB SCRAPER


import requests
from bs4 import BeautifulSoup

r = requests.get("https://example.com")
soup = BeautifulSoup(r.text, "html.parser")
print(soup.title.text)

You might also like