240 lines
8.5 KiB
HTML
240 lines
8.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
|
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
|
<script>
|
|
// Apply theme immediately to prevent flash
|
|
(function() {
|
|
const theme = localStorage.getItem('theme') ||
|
|
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
})();
|
|
</script>
|
|
<title>Python Quickstart Reference - Quick Reference - Launch Pad</title>
|
|
</head>
|
|
<body>
|
|
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
|
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
|
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
|
</button>
|
|
<br/><br/>
|
|
<div class="name">
|
|
__ _______________________ _________._________________________
|
|
/ \ / \_ _____/\______ \/ _____/| \__ ___/\_ _____/
|
|
\ \/\/ /| __)_ | | _/\_____ \ | | | | | __)_
|
|
\ / | \ | | \/ \| | | | | \
|
|
\__/\ / /_______ / |______ /_______ /|___| |____| /_______ /
|
|
\/ \/ \/ \/ \/
|
|
|
|
</div>
|
|
<div class="blog-page-header">
|
|
<div class="blog-header-content">
|
|
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
|
</a>
|
|
<h1 class="blog-page-title">Python Quickstart Reference</h1>
|
|
</div>
|
|
</div>
|
|
<div class="blog-post-container">
|
|
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
|
<div class="blog-post">
|
|
<div class="blog-post-content">
|
|
<p><a href="index.html">← Back to quick reference</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
<hr>
|
|
<p>Quick reference guide for Python. Essential syntax, control flow, data structures, and common patterns.</p>
|
|
<hr>
|
|
<h2>Hello World</h2>
|
|
<pre><code>print("Hello, World!") # The universal programmer's rite of passage.</code></pre>
|
|
<hr>
|
|
<h2>Variables</h2>
|
|
<pre><code>x = 42 # integer
|
|
name = "Alice" # string
|
|
flag = True # boolean
|
|
pi = 3.14 # float
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Control Flow</h2>
|
|
<h3>If/Else</h3>
|
|
<pre><code>if x > 10: # Colon is mandatory; indentation rules supreme
|
|
print("x is big")
|
|
else:
|
|
print("x is small")
|
|
|
|
if x > 10:
|
|
print("big")
|
|
elif x > 5:
|
|
print("medium")
|
|
else:
|
|
print("small")
|
|
</code></pre>
|
|
<h3>For Loops</h3>
|
|
<pre><code>for i in range(5): # i goes from 0 to 4
|
|
print(i)
|
|
|
|
for item in [1, 2, 3]: # Iterate over list
|
|
print(item)
|
|
|
|
for i in range(0, 10, 2): # Start, stop, step
|
|
print(i) # Prints 0, 2, 4, 6, 8
|
|
</code></pre>
|
|
<h3>While Loops</h3>
|
|
<pre><code>while x > 0: # Beware infinite loops
|
|
x -= 1
|
|
print(x)
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Functions</h2>
|
|
<pre><code>def add(a, b): # 'def' starts the function
|
|
return a + b
|
|
|
|
print(add(2, 3)) # Call a function with parentheses
|
|
|
|
def greet(name="World"): # Default parameter
|
|
return f"Hello, {name}!"
|
|
|
|
greet() # Uses default
|
|
greet("Alice") # Overrides default
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Lists / Dictionaries</h2>
|
|
<h3>Lists</h3>
|
|
<pre><code>nums = [1, 2, 3] # Lists are ordered, mutable
|
|
for n in nums:
|
|
print(n)
|
|
|
|
nums.append(4) # Add to end
|
|
nums.insert(0, 0) # Insert at index
|
|
nums.pop() # Remove and return last item
|
|
nums[0] # Access by index
|
|
nums[-1] # Last item (negative indexing)
|
|
nums[1:3] # Slice: items 1 to 2 (not 3)
|
|
</code></pre>
|
|
<h3>Dictionaries</h3>
|
|
<pre><code>person = {"name": "Alice", "age": 30} # Key-value mapping
|
|
print(person["name"]) # Access via keys
|
|
person["city"] = "NYC" # Add new key-value
|
|
person.get("name") # Safe access (returns None if missing)
|
|
person.get("phone", "N/A") # With default value
|
|
|
|
for key, value in person.items(): # Iterate over key-value pairs
|
|
print(f"{key}: {value}")
|
|
</code></pre>
|
|
<h3>Tuples</h3>
|
|
<pre><code>point = (3, 4) # Immutable ordered collection
|
|
x, y = point # Unpacking
|
|
</code></pre>
|
|
<h3>Sets</h3>
|
|
<pre><code>unique = {1, 2, 3} # Unordered collection of unique items
|
|
unique.add(4) # Add item
|
|
unique.remove(2) # Remove item
|
|
</code></pre>
|
|
<hr>
|
|
<h2>String Operations</h2>
|
|
<pre><code>text = "Hello, World!"
|
|
text.upper() # "HELLO, WORLD!"
|
|
text.lower() # "hello, world!"
|
|
text.split(",") # ["Hello", " World!"]
|
|
text.replace("World", "Python") # "Hello, Python!"
|
|
f"Value: {x}" # f-string formatting
|
|
"Value: {}".format(x) # .format() method
|
|
</code></pre>
|
|
<hr>
|
|
<h2>List Comprehensions</h2>
|
|
<pre><code>squares = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, ...]
|
|
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Error Handling</h2>
|
|
<pre><code>try:
|
|
result = 10 / 0
|
|
except ZeroDivisionError:
|
|
print("Can't divide by zero!")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
finally:
|
|
print("This always runs")
|
|
</code></pre>
|
|
<hr>
|
|
<h2>File Operations</h2>
|
|
<pre><code># Reading
|
|
with open("file.txt", "r") as f:
|
|
content = f.read()
|
|
lines = f.readlines()
|
|
|
|
# Writing
|
|
with open("file.txt", "w") as f:
|
|
f.write("Hello, World!")
|
|
|
|
# Appending
|
|
with open("file.txt", "a") as f:
|
|
f.write("New line\n")
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Classes</h2>
|
|
<pre><code>class Person:
|
|
def __init__(self, name, age):
|
|
self.name = name
|
|
self.age = age
|
|
|
|
def greet(self):
|
|
return f"Hello, I'm {self.name}"
|
|
|
|
person = Person("Alice", 30)
|
|
print(person.greet())
|
|
</code></pre>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Indentation *is* syntax — Python hates lazy spacing.</li>
|
|
</ul>
|
|
<ul>
|
|
<li><code>input()</code> always returns a string; convert to int/float if needed:
|
|
<pre><code>age = int(input("How old are you? "))</code></pre>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Python is dynamically typed: flexible, but watch out for sneaky type errors.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Everything is an object, even functions and classes.</li>
|
|
</ul>
|
|
<ul>
|
|
<li><code>range(5)</code> goes 0..4, not 5 — the Pythonic gotcha.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>with</code> statements for file operations — automatic cleanup.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>List comprehensions are faster and more Pythonic than loops for simple transformations.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>enumerate()</code> when you need both index and value:
|
|
<pre><code>for i, item in enumerate(items):
|
|
print(f"{i}: {item}")</code></pre>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>zip()</code> to iterate over multiple lists simultaneously:
|
|
<pre><code>for name, age in zip(names, ages):
|
|
print(f"{name} is {age}")</code></pre>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Import modules with <code>import module</code> or specific functions with <code>from module import function</code>.</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html>
|
|
|