Python Cheat Sheet Part 05 - Advanced Python & Best Practices
Welcome to Cyber Supto! I'm Supto. In this Python Cheat Sheet series, we covered Python from fundamentals to practical coding concepts that every developer should know.
In this final part, we will explore advanced Python features and best practices that help developers write cleaner, more efficient, and more professional code.
List Comprehension
List comprehension provides a concise way to create lists.
numbers = [1,2,3,4,5]
squares = [x*x for x in numbers]
print(squares)
This is shorter and more readable than traditional loops.
squares = []
for x in numbers:
squares.append(x*x)
Dictionary Comprehension
Python also supports dictionary comprehension.
numbers = [1,2,3,4]
square_dict = {x: x*x for x in numbers}
print(square_dict)
Exception Handling
Errors can crash programs. Python provides exception handling to manage them safely.
try:
num = int(input("Enter a number: "))
print(num)
except ValueError:
print("Invalid number")
Using Finally
try:
x = 10 / 2
except:
print("Error occurred")
finally:
print("Execution finished")
The finally block always runs.
Working with Files
Python can read and write files easily.
Reading a File
file = open("data.txt", "r")
content = file.read()
print(content)
file.close()
Writing to a File
file = open("data.txt", "w")
file.write("Hello Cyber Supto")
file.close()
Using With Statement
The recommended method:
with open("data.txt", "r") as file:
print(file.read())
This automatically closes the file.
Virtual Environments
Virtual environments isolate project dependencies.
python -m venv env
Activate environment:
source env/bin/activate
Install packages:
pip install requests
Pip Package Manager
pip is used to install Python libraries.
pip install flask
pip install numpy
pip install pandas
Check installed packages:
pip list
Popular Python Libraries
- NumPy – numerical computing
- Pandas – data analysis
- Matplotlib – data visualization
- Flask – web development
- Requests – HTTP requests
Python Best Practices
- Follow the PEP8 coding style
- Use meaningful variable and function names
- Keep functions short and focused
- Write comments for complex logic
- Organize code using modules and packages
- Handle errors using exception handling
Performance Tips
- Use list comprehension when possible
- Avoid unnecessary loops
- Use built-in Python functions
- Choose the correct data structure
- Profile slow code before optimizing
Quick Python Developer Tips
- Practice coding daily
- Read other developers' code
- Build small automation scripts
- Contribute to open source projects
- Keep learning Python libraries
All Parts of This Python Cheat Sheet Series
- Python Cheat Sheet Part 01 - Python Basics & Syntax
- Python Cheat Sheet Part 02 - Control Flow (If, Else, Loops)
- Python Cheat Sheet Part 03 - Functions & Modules
- Python Cheat Sheet Part 04 - Data Structures (Lists, Tuples, Sets, Dictionaries)
- Python Cheat Sheet Part 05 - Advanced Python & Best Practices
Final Thoughts
Python is one of the most powerful and beginner-friendly programming languages. Whether you want to build websites, automate tasks, analyze data, or develop AI systems, Python provides the tools to make it possible.
This Python Cheat Sheet series on Cyber Supto was created to give developers and beginners a quick reference guide for learning Python efficiently.
Thanks for reading,
Cyber Supto.
Post a Comment