APPRENDRE PYTHON DE A à Z

4 min read 5 hours ago
Published on Sep 21, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the basics of learning Python, starting from installation to creating your first scripts. It is designed for complete beginners and covers all operating systems: Windows, Mac OS, and Linux. By the end of this guide, you will be equipped to write simple Python programs and understand fundamental programming concepts.

Step 1: Install Python

Choose an IDE

  • Select an Integrated Development Environment (IDE) that suits your needs. Popular choices include:
    • Visual Studio Code (VS Code)
    • PyCharm
    • Jupyter Notebook

Installation on Windows

  1. Download Python from the official website.
  2. Run the installer and follow these steps:
    • Ensure to check "Add Python to PATH".
    • Select "Install Now".
  3. Verify the installation:
    • Open Command Prompt and type python --version.
    • Ensure the correct version appears.

Common Errors on Windows

  • If Python is not recognized:
    • Check if it is added to your system PATH.
  • If installation fails:
    • Ensure you have administrative rights.

Installation on Mac OS

  1. Use Homebrew (if installed) by running:
    brew install python
    
  2. Alternatively, download the installer from the official Python website and follow similar steps as Windows.

Installation on Linux

  1. Open the terminal and run:
    sudo apt update
    sudo apt install python3
    
  2. Verify installation with:
    python3 --version
    

Step 2: Your First Python Script

  • Open your chosen IDE or a simple text editor.
  • Write a simple script to print "Hello, World!".
print("Hello, World!")
  • Save the file with a .py extension (e.g., hello.py).

Step 3: Using the Terminal

Basic Terminal Commands

  • ls: List files in the current directory.
  • pwd: Show the current directory path.
  • cd: Change directory.
  • clear: Clear the terminal screen.
  • mkdir: Create a new directory.
  • rm: Remove a file or directory.
  • touch: Create a new empty file.

Step 4: Configure Visual Studio Code

Installation and Setup

  1. Download and install Visual Studio Code from the official website.
  2. Install the Python extension for VS Code from the marketplace.
  3. Configure the Python interpreter:
    • Open Command Palette (Ctrl + Shift + P).
    • Search for "Python: Select Interpreter" and choose your installed Python version.

Running Python Scripts

  • To run a script within VS Code:
    • Open the script file.
    • Click on the "Run" button or use the terminal command:
    python hello.py
    

Step 5: Understanding Python Data Types

Basic Data Types

  • Strings: Text enclosed in quotes.
  • Integers: Whole numbers.
  • Booleans: True or False values.

Variables

  • Variables store data:
name = "Alice"
age = 30
is_student = True
  • Naming conventions:
    • Use descriptive names.
    • Avoid special characters.

Step 6: Using Basic Operators

Arithmetic Operators

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /

Comparison Operators

  • Equal: ==
  • Not equal: !=
  • Greater than: >

Step 7: Conditional Statements

  • Use if, elif, and else to control the flow based on conditions.
if age >= 18:
    print("Adult")
else:
    print("Minor")

Step 8: Loops

For Loop

  • Iterate over a sequence:
for i in range(5):
    print(i)

While Loop

  • Continue as long as a condition is true:
count = 0
while count < 5:
    print(count)
    count += 1

Conclusion

You've now covered the essential steps to get started with Python programming. You should be able to install Python, create and run scripts, understand data types and variables, and use basic control flow with conditionals and loops. As you progress, consider exploring additional resources and projects to enhance your skills further. Happy coding!