Python cơ bản: Giới thiệu và Setup

Bắt đầu học Python từ zero - cài đặt môi trường, viết chương trình đầu tiên và hiểu các khái niệm cơ bản.

Python là gì?

Python là ngôn ngữ lập trình bậc cao, dễ học và cực kỳ linh hoạt. Python được sử dụng rộng rãi trong nhiều lĩnh vực:

  • Web Development: Django, Flask, FastAPI
  • Data Science: Pandas, NumPy, Matplotlib
  • Machine Learning: TensorFlow, PyTorch, scikit-learn
  • Automation: Scripts, DevOps, CI/CD
  • Cybersecurity: Penetration testing, network analysis, forensics

Tại sao Python cho Cybersecurity?

Python là công cụ hàng đầu của security professionals vì:

  1. Dễ học: Syntax đơn giản, gần với ngôn ngữ tự nhiên
  2. Thư viện phong phú: Scapy, requests, cryptography, paramiko
  3. Cross-platform: Chạy trên mọi hệ điều hành
  4. Rapid prototyping: Viết tools nhanh chóng
  5. Community: Cộng đồng security rất lớn

Cài đặt Python

Windows

  1. Tải Python từ python.org
  2. Chọn Add Python to PATH khi cài đặt
  3. Verify cài đặt:
python --version
# Python 3.12.x

macOS

# Sử dụng Homebrew
brew install python3

# Verify
python3 --version

Linux

# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-pip

# Verify
python3 --version

Chương trình đầu tiên

Tạo file hello.py:

# hello.py - Chương trình Python đầu tiên

print("Hello, PION!")
print("Chào mừng bạn đến với Python for Cybersecurity")

# Variables
name = "Hacker"
level = 1

print(f"Xin chào {name}! Level: {level}")

Chạy chương trình:

python hello.py
# Output:
# Hello, PION!
# Chào mừng bạn đến với Python for Cybersecurity
# Xin chào Hacker! Level: 1

Python Interactive Shell

Python có interactive shell để test code nhanh:

>>> 2 + 2
4
>>> "hello" + " world"
'hello world'
>>> len("cybersecurity")
13
>>> print("Python is awesome!")
Python is awesome!

Cấu trúc cơ bản

Comments

# Single line comment

"""
Multi-line comment
hoặc docstring
"""

'''
Cũng có thể dùng
single quotes
'''

Indentation

Python sử dụng indentation (thụt lề) thay vì brackets:

# Đúng - sử dụng 4 spaces
if True:
    print("Indented correctly")
    if True:
        print("Nested indentation")

# Sai - sẽ báo lỗi IndentationError
# if True:
# print("No indent")  # Error!

IDE và Công cụ

VS Code (Khuyến nghị)

  1. Cài đặt VS Code
  2. Cài extension Python của Microsoft
  3. Cài extension Pylance cho type checking

PyCharm

IDE chuyên dụng cho Python với nhiều tính năng:

  • Debugger mạnh mẽ
  • Refactoring tools
  • Git integration

Jupyter Notebook

Tốt cho data science và learning:

pip install jupyter
jupyter notebook

Virtual Environments

Luôn sử dụng virtual environment cho projects:

# Tạo virtual environment
python -m venv myenv

# Activate (Windows)
myenv\Scripts\activate

# Activate (macOS/Linux)
source myenv/bin/activate

# Cài đặt packages
pip install requests scapy

# Deactivate
deactivate

Package Management với pip

# Cài đặt package
pip install requests

# Cài nhiều packages
pip install requests scapy cryptography

# Xem packages đã cài
pip list

# Export requirements
pip freeze > requirements.txt

# Cài từ requirements
pip install -r requirements.txt

Bước tiếp theo

Trong bài tiếp theo, chúng ta sẽ học về:

  • Variables và Data Types: String, int, float, bool
  • Operators: Arithmetic, comparison, logical
  • Input/Output: Nhận input từ user

💡 Pro tip: Luyện tập hàng ngày! Viết ít nhất một script Python mỗi ngày để cải thiện kỹ năng.