Learn the Fundamentals of Python Programming
This guide will walk you through the essential steps to begin your Python journey. You’ll learn how to install Python on your system, set up a powerful code editor, write and execute your first Python program, and understand the basics of variables and data types. By the end of this tutorial, you’ll have a solid foundation to build upon for more advanced Python concepts.
Prerequisites
- A computer (Windows, macOS, or Linux)
- An internet connection
- A web browser
Step 1: Install Python
The first step is to download and install the latest version of Python. Python is a versatile and widely-used programming language, known for its readability and extensive libraries.
- Open your web browser and navigate to the official Python website: python.org.
- Hover over the “Downloads” menu.
- Click on the download button for the latest stable version of Python (e.g., Python 3.13 or newer). The website usually detects your operating system and offers the appropriate installer.
- For Windows users: Before clicking “Install Now,” make sure to check the box that says “Add Python to PATH.” This is a crucial step that simplifies running Python from the command line later. Then, click “Install Now.”
- For macOS users: Run the downloaded installer package and follow the on-screen instructions. Python 3 on macOS is often installed as the command
python3. - For Linux users: Python is often pre-installed. You can check by opening a terminal and typing
python3 --version. If it’s not installed or you need a newer version, you can usually install it via your distribution’s package manager (e.g.,sudo apt install python3on Debian/Ubuntu).
Verify Installation
After the installation is complete, it’s essential to verify that Python has been installed correctly.
- Open your system’s terminal or command prompt:
- Windows: Press the Windows key, type “cmd” or “terminal,” and press Enter.
- macOS: Press
Command + Spaceto open Spotlight, type “Terminal,” and press Enter. - Linux: Open your preferred terminal application.
- In the terminal window, type the following command and press Enter:
- Windows:
python --version - macOS/Linux:
python3 --version
- Windows:
You should see the version number of Python that you just installed (e.g., Python 3.13.0). If you see an error, revisit the installation steps, especially the “Add Python to PATH” option on Windows.
Step 2: Set Up a Code Editor (Visual Studio Code)
While you can write Python code in a simple text editor, using a code editor or an Integrated Development Environment (IDE) significantly enhances your productivity. We’ll use Visual Studio Code (VS Code), a popular, free, and powerful code editor.
- Go to the VS Code website: code.visualstudio.com.
- Download and install VS Code for your operating system.
- Once installed, open VS Code.
- Create a new project folder:
- Go to File > Open Folder… (or File > Add Folder to Workspace… on macOS).
- Navigate to a location where you want to store your projects (e.g., your Documents folder).
- Click “New Folder” and name it something like
python_projects. - Select this new folder and click “Open” (or “Select Folder”).
- Create your first Python file:
- In the Explorer sidebar on the left, click the “New File” icon (it looks like a document with a plus sign).
- Name the file
app.py. The.pyextension tells the system that this is a Python file.
Install the Python Extension for VS Code
To get the best experience for Python development in VS Code, you need to install the official Python extension.
- In VS Code, click on the Extensions icon in the Activity Bar on the left side (it looks like four squares, one detached).
- In the search bar at the top, type
Python. - Look for the official Python extension published by Microsoft and click the “Install” button.
- If prompted, reload VS Code to activate the extension.
Step 3: Write and Run Your First Python Program
Now that your environment is set up, let’s write a simple “Hello, World!” program.
- Open the
app.pyfile you created earlier. - Type the following code into the editor:
print("Hello, World!") - Save the file (
Ctrl + Son Windows/Linux,Command + Son macOS).
Running the Code
You have a few ways to run your Python script:
- Using the Play Button: With the Python extension installed, you’ll see a “Run Python File” button (a play icon) in the top-right corner of the editor. Click it.
- Using the Integrated Terminal:
- Open the integrated terminal in VS Code by pressing
Ctrl + `(backtick key, usually below Esc) on Windows/Linux, orOption + `on macOS. - Type the following command and press Enter:
- Windows:
python app.py - macOS/Linux:
python3 app.py
- Windows:
- Open the integrated terminal in VS Code by pressing
In the terminal output, you should see:
Hello, World!Tip: The print() function is a built-in Python function used to display output to the console. Text, also known as strings, must be enclosed in quotes (either single or double).
Step 4: Understanding Variables and Data Types
Variables are fundamental to programming. They act as containers for storing data values in your computer’s memory.
Declaring Variables
To create a variable, you simply choose a name and assign a value to it using the equals sign (=).
Example:
student_count = 1000
rating = 4.99
is_published = True
course_name = "Python Programming"Basic Data Types
Python supports various data types. Here are the most common primitive types:
- Integers (int): Whole numbers (e.g.,
10,-5,0). - Floats (float): Numbers with a decimal point (e.g.,
3.14,-0.5,2.718). - Booleans (bool): Represent truth values, either
TrueorFalse. Note that Python is case-sensitive, so these must start with a capital letter. - Strings (str): Sequences of characters, used for text. They must be enclosed in single (
') or double (") quotes (e.g.,"Hello",'Python').
Using Variables
You can use variables in your code to refer to the data they hold. For example, you can print the value of a variable:
student_count = 1000
print(student_count)This will output: 1000.
Expert Note: Python is dynamically typed, meaning you don’t need to declare the type of a variable explicitly. The interpreter determines the type based on the value assigned.
Step 5: Code Formatting and Linting
Writing clean, readable code is crucial for collaboration and maintainability. Python has style guides, like PEP 8, which recommend formatting conventions.
Linting
Linting is the process of using a tool (a linter) to analyze your code for potential errors, stylistic issues, and suspicious constructs. The Python extension for VS Code includes a linter (like Pylint) that helps you identify problems as you type.
- If you write code like
print "Hello"(without parentheses), the linter will show a red underline, indicating an error. Hovering over it will provide a message suggesting you add parentheses. - Similarly, incomplete expressions like
2 +will be flagged.
Tip: Use the “Problems” panel in VS Code (Shift + Ctrl + M on Windows/Linux, Shift + Command + M on macOS) to see a list of all issues in your project.
Formatting
Code formatting tools automatically adjust your code’s layout to comply with style guides like PEP 8. VS Code can integrate with formatters like autopep8.
- Enable Format on Save: Go to File > Preferences > Settings (or
Ctrl + ,/Command + ,). Search forFormat on Saveand enable theEditor: Format On Saveoption. - Automatic Formatting: Now, whenever you save your
app.pyfile, VS Code will automatically reformat the code according to PEP 8 standards (e.g., adding spaces around operators like=). - Manual Formatting: You can also manually format the document by opening the Command Palette (
Shift + Ctrl + PorShift + Command + P) and searching forFormat Document.
Expert Note: While tools automate formatting, understanding PEP 8 principles helps you write cleaner code from the start and interpret the formatter’s actions.
Conclusion
You have successfully installed Python, set up a professional development environment with VS Code, written and executed your first Python program, and learned about the basics of variables and code quality. This is a significant achievement! Continue practicing these steps and explore the vast possibilities that Python offers for AI, web development, automation, and more.
Source: Python Full Course for Beginners (YouTube)