Python Get Started
To get started with Python, follow these steps:
1. Install Python
Most systems (Windows, macOS, Linux) come with Python pre-installed, but you may need to install the latest version.
- Download Python: Go to the official Python website and download the latest stable release for your operating system.
- Installation on Windows:
- During installation, check the box that says “Add Python to PATH.”
- Follow the installer instructions and choose default settings.
- Installation on macOS/Linux:
- macOS often has Python pre-installed. Use Homebrew to install the latest version if needed:
brew install python - On Linux, use a package manager like
apt(for Ubuntu/Debian):sudo apt update sudo apt install python3
- macOS often has Python pre-installed. Use Homebrew to install the latest version if needed:
2. Verify Python Installation
Once installed, open a terminal (or command prompt on Windows) and type:
python --version
or
python3 --version
This should show you the installed version of Python, e.g., Python 3.11.2.
3. Running Python Code
You can run Python code in several ways:
- Interactive Mode: Open the terminal and type
pythonorpython3to enter Python’s interactive shell. You can type and execute commands interactively here.Example:>>> print("Hello, World!") Hello, World! - Script Mode: Write your Python code in a file with a
.pyextension and run it from the terminal.Example:- Create a file called
hello.pywith the following content:print("Hello, World!") - Run the script:
python hello.py
- Create a file called
4. Python IDEs and Text Editors
You can write Python code in any text editor, but using an Integrated Development Environment (IDE) or a code editor will make development easier. Here are some popular choices:
- IDLE: Comes bundled with Python, a simple IDE for beginners.
- VS Code: A free, lightweight code editor with Python extensions.
- PyCharm: A professional Python IDE, great for large projects.
- Sublime Text: A lightweight editor with Python support.
5. First Python Program
Now, let’s create your first Python program:
- Open a text editor and create a file named
first_program.py. - Add the following code:
print("Welcome to Python programming!") - Save the file and run it in the terminal:
python first_program.py
You should see:
Welcome to Python programming!
6. Installing Packages with pip
Python has a large number of libraries and modules you can install using pip, Python’s package manager. For example, to install the requests library (used for making HTTP requests), type the following in your terminal:
pip install requests
Then you can use it in your code:
import requests
response = requests.get('https://api.github.com')
print(response.status_code)
7. Learning Resources
Here are some resources to help you learn Python:
- Official Python Documentation: docs.python.org
- Online Courses: iLovePython
- Books:
- Python Crash Course by Eric Matthes
- Automate the Boring Stuff with Python by Al Sweigart
8. Next Steps
- Start small: Begin by writing simple scripts.
- Explore Python’s built-in functions and libraries.
- Work on projects like a simple calculator, text-based games, or web scraping.
With these steps, you are now set up to start programming in Python!