Here’s a step-by-step tutorial to get started with Python:
Step 1: Install Python
- Download Python: Visit the official Python website (https://www.python.org/downloads/) and download the latest version of Python for your operating system (Windows, macOS, Linux).
- Install Python: Run the installer and follow the prompts. Make sure to check the box that says “Add Python to PATH” during installation to make it easier to run Python from the command line.
- Verify Installation: Open a command prompt (Windows) or terminal (macOS/Linux) and type
python --version
orpython3 --version
to verify that Python has been installed correctly. You should see the version number printed.
Step 2: Set Up a Development Environment
- Text Editor or IDE: Choose a text editor or an Integrated Development Environment (IDE) to write your Python code. Popular choices include:
- Text Editors: Visual Studio Code, Sublime Text, Atom
- IDEs: PyCharm, Spyder, IDLE (comes bundled with Python)
- Create a Python File: Open your text editor or IDE and create a new file with a
.py
extension (e.g.,hello.py
).
Step 3: Your First Python Program
Let’s start with a simple “Hello, World!” program to ensure everything is set up correctly.
# hello.py
print("Hello, World!")
Save the file and run it from the command line:
- Navigate to the directory where
hello.py
is saved using thecd
command (e.g.,cd path/to/your/file
). - Type
python hello.py
(orpython3 hello.py
depending on your installation) and press Enter.
You should see Hello, World!
printed to the console.
Step 4: Basic Python Concepts
Variables and Data Types
Python supports various data types:
- Integers:
int
- Floats:
float
- Strings:
str
- Lists:
list
- Tuples:
tuple
- Dictionaries:
dict
- Boolean:
bool
- NoneType:
None
Example:
# Variables and Data Types
name = "Alice"
age = 30
height = 1.75
is_student = False
Control Flow Statements
Python uses if
, elif
, else
for conditional statements and for
and while
loops for iteration.
# Control Flow Statements
if age >= 18:
print("Adult")
else:
print("Minor")
for i in range(5):
print(i)
while age < 40:
age += 1
Functions
Functions in Python are defined using the def
keyword.
# Functions
def greet(name):
print(f"Hello, {name}!")
greet("Bob")
Step 5: Additional Resources
- Official Python Documentation: https://docs.python.org/3/
- Python Tutorial: https://docs.python.org/3/tutorial/index.html
- Online Python Interpreter: https://www.python.org/shell/
This tutorial provides a basic foundation to start writing Python code. As you progress, explore Python’s vast ecosystem including libraries for data analysis, web development, machine learning, and more. Happy coding!