Python Basics
Getting started with Python - installation, running code, and fundamental concepts.
What is Python?
- Python is a high-level, interpreted programming language.
- Created by Guido van Rossum in 1991.
- Known for its simple, readable syntax that resembles English.
- Used for web development, data science, automation, AI, and more.
Installing Python
- Download from python.org (Python 3.x recommended).
- Check installation with 'python --version' or 'python3 --version'.
- Use pip to install packages: 'pip install package_name'.
# Check Python version
python --version
# Check pip version
pip --version
# Install a package
pip install requests
Running Python Code
- Interactive mode: Type 'python' in terminal for immediate execution.
- Script mode: Save code in .py file and run with 'python filename.py'.
- IDEs: Use VS Code, PyCharm, or Jupyter Notebook for development.
# Interactive mode
>>> print("Hello, World!")
Hello, World!
# Script mode (save as hello.py)
print("Hello, World!")
# Run: python hello.py
Comments
- Single-line comments start with #.
- Multi-line comments use triple quotes (''' or """).
- Comments explain code and are ignored by Python.
# This is a single-line comment
'''
This is a
multi-line comment
'''
x = 5 # Inline comment
Indentation
- Python uses indentation (spaces/tabs) to define code blocks.
- Standard is 4 spaces per indentation level.
- Consistent indentation is required - mixing causes errors.
# Correct indentation
if True:
print("This is indented")
print("Same level")
# Wrong - will cause IndentationError
if True:
print("Not indented")