# Introduction

Python is a high-level programming language that is widely used for various applications, including web development, data science, automation, artificial intelligence, and cybersecurity. It is known for its simplicity and readability, making it easy for beginners to learn while also being powerful enough for advanced users. Python supports multiple programming styles, including object-oriented, procedural, and functional programming, which makes it versatile and flexible for different types of projects.

Python was developed by Guido van Rossum in the late 1980s and officially released in 1991. Guido wanted to create a language that was easy to understand and fun to use, so he designed Python with a clean and straightforward syntax. Over the years, Python has gone through many updates and improvements, becoming one of the most widely used programming languages in the world. Its large community of developers continuously contributes to its development, making it even better over time.

## Why is it so popular?

Python is popular because of its ease of use, extensive libraries, and strong community support. Unlike some other programming languages that require writing long and complex code, Python allows programmers to achieve the same results with fewer lines of code. It has libraries and frameworks for almost everything, from data analysis to web development and machine learning. Its open-source nature means anyone can use it for free and contribute to its improvement. These factors make Python a top choice for beginners and professionals alike.

## Why do we need python?

Python is also widely used in penetration testing (pentesting) because of its powerful scripting capabilities and large collection of cybersecurity tools. Pentesters use Python to create custom scripts for scanning networks, exploiting vulnerabilities, and automating security tasks. Many cybersecurity tools, such as Scapy, Nmap, and Metasploit, have Python-based modules that make pentesting more efficient. Python’s ability to interact with operating systems, networks, and web applications makes it a valuable tool for security professionals who need to test and secure systems against cyber threats.

In this sub-module, we will cover some of the most useful Python concepts you need to know. You can then apply these concepts by exploring additional resources and building projects as you progress.

## How is Python Code Executed

Python code is executed through an **interpreter**, which is a program that reads and executes the code line by line. When you write Python code and run it, the interpreter translates it into machine code, which the computer can understand and execute. The process begins when you invoke the Python interpreter, either by running a script through a command line or by executing code inside an interactive shell. The interpreter reads the code, processes it, and outputs the result to your screen or performs the defined operations.

First, the Python code is compiled into an intermediate form called **bytecode**. This bytecode is not machine-specific, meaning it can be run on any machine that has a compatible Python interpreter. The compilation process doesn’t happen explicitly when you run a script; instead, Python compiles the script into bytecode behind the scenes. This bytecode is stored in a `.pyc` file, which the Python virtual machine (PVM) uses to execute the code. The bytecode step helps Python achieve portability across different platforms without needing to recompile the code each time.

Once the bytecode is ready, the Python **virtual machine (PVM)** takes over. The PVM is responsible for reading the bytecode and executing it on the computer’s hardware. This is where Python's dynamic nature shines: it interprets the code at runtime, meaning you can interact with the program while it's running, debugging and modifying it as you go. This process allows Python to be highly flexible and easy to use, although it can be slower compared to languages like C, which are directly compiled to machine code.

## Code Editor

Since we've already covered Software Management, you can now try installing VSCode on your Kali Linux. We will write and run code there.&#x20;

Below are the steps to open a directory in VSCode and run Python code:

```bash
┌──(kali㉿kali)-[~]
└─$ mkdir learn-python && cd learn-python

┌──(kali㉿kali)-[~/learn-python]
└─$ touch script.py ## Create a .py script

┌──(kali㉿kali)-[~/learn-python]
└─$ code . # open current directory in VSCode

┌──(kali㉿kali)-[~/learn-python]
└─$ python3 script.py ## Execute the script using python version 3 i.e python3
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.ncateam.xyz/fundamentals/python/introduction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
