> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/language/python/basic/overview.md).

# Overview

Python is a **high-level, interpreted, general-purpose programming language** known for its **simplicity**, **readability**, and **versatility**. It is widely used in **web development, data analysis, cybersecurity, automation, AI, and scripting**.

***

### **1. Key Features of Python**

| Feature                 | Description                                                   |
| ----------------------- | ------------------------------------------------------------- |
| **Easy to Learn**       | Simple syntax similar to English.                             |
| **Interpreted**         | No need for compilation — runs line by line.                  |
| **Dynamic Typing**      | No need to declare variable types explicitly.                 |
| **Object-Oriented**     | Supports classes and objects.                                 |
| **Extensive Libraries** | Comes with a rich standard library and many external modules. |
| **Portable**            | Runs on multiple platforms (Windows, Linux, macOS).           |
| **Open Source**         | Free to use and modify.                                       |

***

### **2. Python Syntax Basics**

#### **a. Print Statement**

Used to display output.

```python
print("Hello, World!")
```

**Output:**

```
Hello, World!
```

#### **b. Comments**

* **Single-line comment:** begins with `#`
* **Multi-line comment:** enclosed in triple quotes (`'''` or `"""`)

```python
# This is a single-line comment

"""
This is
a multi-line
comment
"""
```

***

### **3. Variables and Data Types**

Variables are used to **store data values**. You don’t need to declare their type — Python detects it automatically.

```python
x = 10            # Integer
name = "Alice"    # String
pi = 3.14         # Float
is_active = True  # Boolean
```

#### **Common Data Types**

| Type    | Example            | Description                   |
| ------- | ------------------ | ----------------------------- |
| `int`   | `5`                | Integer (whole numbers)       |
| `float` | `3.14`             | Decimal numbers               |
| `str`   | `"Hello"`          | Text data                     |
| `bool`  | `True / False`     | Boolean values                |
| `list`  | `[1, 2, 3]`        | Ordered, mutable collection   |
| `tuple` | `(1, 2, 3)`        | Ordered, immutable collection |
| `set`   | `{1, 2, 3}`        | Unordered, unique elements    |
| `dict`  | `{"key": "value"}` | Key-value pairs               |

***

### **4. Input from User**

Use the `input()` function to take user input.

```python
name = input("Enter your name: ")
print("Hello,", name)
```

**Output:**

```
Enter your name: Alice
Hello, Alice
```

> 🔹 Note: `input()` always returns data as a **string**. Convert it to other types if needed using `int()`, `float()`, etc.

***

### **5. Type Conversion (Casting)**

```python
x = "10"
y = int(x)       # Convert string to integer
z = float(y)     # Convert integer to float

print(type(z))   # <class 'float'>
```

***

### **6. Basic Operators**

| Type           | Operator          | Example             | Result                   |
| -------------- | ----------------- | ------------------- | ------------------------ |
| **Arithmetic** | `+ - * / % // **` | `5 + 2`             | `7`                      |
| **Comparison** | `== != > < >= <=` | `5 > 2`             | `True`                   |
| **Logical**    | `and or not`      | `(x > 0 and y > 0)` | True if both True        |
| **Assignment** | `= += -= *= /=`   | `x += 1`            | Add and assign           |
| **Membership** | `in, not in`      | `'a' in 'cat'`      | True                     |
| **Identity**   | `is, is not`      | `x is y`            | Compares memory location |

***

### **7. Strings in Python**

Strings are sequences of characters enclosed in quotes.

```python
name = "Python"
print(name[0])       # Access characters
print(name.lower())  # Convert to lowercase
print(name.upper())  # Convert to uppercase
print(name[:3])      # Slicing
```

**Output:**

```
P
python
PYTHON
Pyt
```

#### **String Concatenation and Formatting**

```python
first = "Hello"
second = "World"
print(first + " " + second)             # Concatenation
print(f"{first}, {second}!")            # f-string formatting
```

***

### **8. Data Structures Overview**

#### **List**

```python
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
```

#### **Tuple**

```python
numbers = (1, 2, 3)
print(numbers[0])
```

#### **Set**

```python
colors = {"red", "green", "blue"}
colors.add("yellow")
print(colors)
```

#### **Dictionary**

```python
person = {"name": "Alice", "age": 25}
print(person["name"])
```

***

### **9. Importing Modules**

Modules let you use external code.

```python
import math

print(math.sqrt(16))
print(math.pi)
```

**Output:**

```
4.0
3.141592653589793
```

***

### **10. Common Built-in Functions**

| Function          | Description            | Example           | Output            |
| ----------------- | ---------------------- | ----------------- | ----------------- |
| `len()`           | Returns length         | `len("Hello")`    | `5`               |
| `type()`          | Returns data type      | `type(3.14)`      | `<class 'float'>` |
| `range()`         | Generates sequence     | `range(5)`        | `[0,1,2,3,4]`     |
| `max()` / `min()` | Largest/smallest value | `max([1,2,3])`    | `3`               |
| `sum()`           | Sum of elements        | `sum([1,2,3])`    | `6`               |
| `sorted()`        | Returns sorted list    | `sorted([3,1,2])` | `[1,2,3]`         |

***

### **11. Basic Input/Output Example**

```python
name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(f"Hello {name}, you are {age} years old.")
```

***

### **12. Comments and Docstrings**

* **Comment:** Used for notes or explanation (`#`)
* **Docstring:** Describes what a function/class does

```python
def add(a, b):
    """Returns the sum of two numbers."""
    return a + b
```

***

### **13. Summary**

| Concept          | Example              | Description                |
| ---------------- | -------------------- | -------------------------- |
| **Variables**    | `x = 10`             | Store values               |
| **Input/Output** | `input()`, `print()` | User interaction           |
| **Data Types**   | `int`, `str`, `list` | Various data forms         |
| **Operators**    | `+`, `and`, `==`     | Used in expressions        |
| **Conditionals** | `if`, `elif`, `else` | Decision making            |
| **Loops**        | `for`, `while`       | Repetition                 |
| **Functions**    | `def greet():`       | Reusable logic             |
| **Modules**      | `import math`        | Add external functionality |
