Python Cheat Sheet
Python programming basics reference guide
Python Cheat Sheet
Quick reference guide for Python programming basics. Click the copy button to copy any command.
94
Total Commands
94
Filtered Results
Variables and Data Types
Command | Copy | Description |
---|---|---|
x = 5 | Assign integer to variable | |
name = "John" | Assign string to variable | |
is_valid = True | Assign boolean to variable | |
price = 19.99 | Assign float to variable | |
type(x) | Get type of variable | |
int(x) | Convert to integer | |
str(x) | Convert to string | |
float(x) | Convert to float |
String Methods
Command | Copy | Description |
---|---|---|
s.upper() | Convert string to uppercase | |
s.lower() | Convert string to lowercase | |
s.strip() | Remove leading/trailing whitespace | |
s.split(",") | Split string by delimiter | |
s.replace("a", "b") | Replace substring | |
s.startswith("pre") | Check if string starts with prefix | |
s.endswith("suf") | Check if string ends with suffix | |
s.find("text") | Find substring position | |
s.count("a") | Count occurrences of substring | |
f"Hello {name}" | Format string (f-string) | |
"{}".format(x) | Format string (format method) | |
"-".join(list) | Join list elements into string |
Lists
Command | Copy | Description |
---|---|---|
list = [1, 2, 3] | Create a list | |
list.append(4) | Add item to end of list | |
list.insert(0, 0) | Insert item at position | |
list.remove(2) | Remove first occurrence of value | |
list.pop() | Remove and return last item | |
list.pop(0) | Remove and return item at index | |
list.sort() | Sort list in place | |
list.reverse() | Reverse list in place | |
len(list) | Get length of list | |
list[0] | Access first element | |
list[-1] | Access last element | |
list[1:3] | Slice list (elements 1-2) | |
[x*2 for x in list] | List comprehension |
Dictionaries
Command | Copy | Description |
---|---|---|
dict = {"key": "value"} | Create a dictionary | |
dict["key"] | Access value by key | |
dict.get("key", default) | Get value with default | |
dict["new"] = "value" | Add or update key-value pair | |
del dict["key"] | Delete key-value pair | |
dict.keys() | Get all keys | |
dict.values() | Get all values | |
dict.items() | Get all key-value pairs | |
"key" in dict | Check if key exists |
Control Flow
Command | Copy | Description |
---|---|---|
if x > 0: | If statement | |
elif x == 0: | Else if statement | |
else: | Else statement | |
x if condition else y | Ternary operator |
Loops
Command | Copy | Description |
---|---|---|
for i in range(5): | For loop with range | |
for item in list: | For loop over list | |
for i, item in enumerate(list): | For loop with index | |
for key, value in dict.items(): | For loop over dictionary | |
while condition: | While loop | |
break | Exit loop | |
continue | Skip to next iteration |
Functions
Command | Copy | Description |
---|---|---|
def func(): | Define function | |
def func(arg1, arg2): | Function with arguments | |
def func(arg="default"): | Function with default argument | |
def func(*args): | Function with variable arguments | |
def func(**kwargs): | Function with keyword arguments | |
return value | Return value from function | |
lambda x: x*2 | Lambda (anonymous) function |
File I/O
Command | Copy | Description |
---|---|---|
open("file.txt", "r") | Open file for reading | |
open("file.txt", "w") | Open file for writing | |
open("file.txt", "a") | Open file for appending | |
f.read() | Read entire file | |
f.readline() | Read one line | |
f.readlines() | Read all lines into list | |
f.write("text") | Write to file | |
f.close() | Close file | |
with open("file.txt") as f: | Open file with context manager |
Exception Handling
Command | Copy | Description |
---|---|---|
try: | Try block | |
except Exception: | Catch exception | |
except ValueError as e: | Catch specific exception | |
finally: | Always execute block | |
raise Exception("error") | Raise exception |
Classes
Command | Copy | Description |
---|---|---|
class MyClass: | Define class | |
def __init__(self): | Constructor method | |
self.attribute = value | Instance attribute | |
obj = MyClass() | Create class instance |
Imports
Command | Copy | Description |
---|---|---|
import module | Import module | |
from module import func | Import specific function | |
import module as m | Import with alias | |
from module import * | Import all from module |
Common Functions
Command | Copy | Description |
---|---|---|
print(x) | Print to console | |
input("prompt") | Get user input | |
range(start, stop, step) | Create range object | |
len(obj) | Get length of object | |
sum(list) | Sum all items in list | |
max(list) | Get maximum value | |
min(list) | Get minimum value | |
sorted(list) | Return sorted list | |
enumerate(list) | Add counter to iterable | |
zip(list1, list2) | Combine iterables | |
map(func, list) | Apply function to each item | |
filter(func, list) | Filter items by function |
All operations are performed locally in your browser. No data is sent to any server.
About Python Cheat Sheet
This section will contain detailed, SEO-friendly content about the Python Cheat Sheet.
In the future, this content will be managed through a headless CMS, allowing you to:
- Add detailed explanations about how to use this tool
- Include examples and use cases
- Provide tips and best practices
- Add FAQs and troubleshooting guides
- Update content without touching the code
How to Use
Step-by-step instructions for using the Python Cheat Sheet will appear here. This content will be fully customizable through the admin panel.
Features
Key features and benefits of this tool will be listed here. All content is editable via the CMS.