> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Basics

***

# 1. Variables

## Definition

A **variable** is used to store a value that can be used later in the program.

### Example

```python theme={null}
name = "John"
age = 20
```

### Output

```text theme={null}
(No output)
```

### Explanation

Variables hold data such as numbers, text, or Boolean values.

***

# 2. Variable Naming Rules (snake\_case)

## Rules

* Use letters, numbers, and underscores (`_`)
* Cannot start with a number
* Cannot contain spaces
* Cannot use special characters except `_`
* Case-sensitive (`age` and `Age` are different)
* Use **snake\_case** for readability

### Good Examples

```python theme={null}
student_name = "Alice"
total_marks = 95
is_logged_in = True
```

### Bad Examples

```python theme={null}
2name = "Alice"
student-name = "Alice"
student name = "Alice"
```

### Output

```text theme={null}
SyntaxError
```

### Explanation

Use meaningful lowercase variable names separated with underscores.

***

# 3. Assign Multiple Values

## Multiple Variables

```python theme={null}
x, y, z = 10, 20, 30

print(x)
print(y)
print(z)
```

### Output

```text theme={null}
10
20
30
```

### Explanation

Assign multiple values in a single line.

***

## Same Value to Multiple Variables

```python theme={null}
a = b = c = 100

print(a)
print(b)
print(c)
```

### Output

```text theme={null}
100
100
100
```

### Explanation

All variables point to the same value.

***

# 4. Output Variables

```python theme={null}
name = "John"

print(name)
```

### Output

```text theme={null}
John
```

### Explanation

`print()` displays the value stored in a variable.

***

## Printing Multiple Variables

```python theme={null}
name = "John"
age = 20

print(name, age)
```

### Output

```text theme={null}
John 20
```

### Explanation

Separate multiple values using commas.

***

# 5. Global Variables

```python theme={null}
name = "John"

def show():
    print(name)

show()
```

### Output

```text theme={null}
John
```

### Explanation

Global variables can be accessed inside functions.

***

# 6. Local Variables

```python theme={null}
def show():
    message = "Hello"
    print(message)

show()
```

### Output

```text theme={null}
Hello
```

### Explanation

Local variables exist only inside the function.

***

# 7. Python Comments

## Single-line Comment

```python theme={null}
# This is a comment

print("Hello")
```

### Output

```text theme={null}
Hello
```

### Explanation

Python ignores comments.

***

## Multi-line Comment

```python theme={null}
"""
This
is
a comment
"""

print("Python")
```

### Output

```text theme={null}
Python
```

### Explanation

Triple quotes are commonly used for multi-line comments or docstrings.

***

# 8. Indentation

```python theme={null}
if True:
    print("Correct")
```

### Output

```text theme={null}
Correct
```

### Explanation

Indentation defines code blocks in Python.

***

# 9. Basic Input

```python theme={null}
name = input("Enter your name: ")

print(name)
```

### Sample Input

```text theme={null}
Alice
```

### Output

```text theme={null}
Enter your name: Alice
Alice
```

### Explanation

`input()` always returns a string.

***

# 10. Taking Integer Input

```python theme={null}
age = int(input("Enter age: "))

print(age)
```

### Sample Input

```text theme={null}
20
```

### Output

```text theme={null}
Enter age: 20
20
```

### Explanation

Use `int()` to convert input into an integer.

***

# 11. Basic Output

```python theme={null}
print("Welcome")
```

### Output

```text theme={null}
Welcome
```

### Explanation

`print()` displays text or values.

***

# 12. Escape Characters

```python theme={null}
print("Hello\nPython")
print("She said \"Hi\"")
```

### Output

```text theme={null}
Hello
Python
She said "Hi"
```

### Explanation

Escape characters create special formatting.

| Escape | Meaning      |
| ------ | ------------ |
| `\n`   | New Line     |
| `\t`   | Tab          |
| `\\`   | Backslash    |
| `\"`   | Double Quote |

***

# 13. Python Data Types

## Integer

```python theme={null}
x = 25

print(type(x))
```

### Output

```text theme={null}
<class 'int'>
```

### Explanation

Stores whole numbers.

***

## Float

```python theme={null}
x = 3.14

print(type(x))
```

### Output

```text theme={null}
<class 'float'>
```

### Explanation

Stores decimal numbers.

***

## String

```python theme={null}
name = "Python"

print(type(name))
```

### Output

```text theme={null}
<class 'str'>
```

### Explanation

Stores text.

***

## Boolean

```python theme={null}
x = True

print(type(x))
```

### Output

```text theme={null}
<class 'bool'>
```

### Explanation

Stores True or False.

***

# 14. Python Numbers

```python theme={null}
x = 10
y = 5.5
z = 2 + 3j

print(type(x))
print(type(y))
print(type(z))
```

### Output

```text theme={null}
<class 'int'>
<class 'float'>
<class 'complex'>
```

### Explanation

Python supports integers, floats, and complex numbers.

***

# 15. Type Conversion (Casting)

```python theme={null}
x = int(4.9)
y = float(5)
z = str(100)

print(x)
print(y)
print(z)
```

### Output

```text theme={null}
4
5.0
100
```

### Explanation

Casting converts one data type into another.

***

# 16. Strings

```python theme={null}
text = "Hello"

print(text)
```

### Output

```text theme={null}
Hello
```

### Explanation

Strings store characters enclosed in quotes.

***

## String Indexing

```python theme={null}
text = "Python"

print(text[0])
print(text[-1])
```

### Output

```text theme={null}
P
n
```

### Explanation

Index starts from **0**.

***

## String Slicing

```python theme={null}
text = "Python"

print(text[0:3])
print(text[2:])
```

### Output

```text theme={null}
Pyt
thon
```

### Explanation

Slicing extracts part of a string.

***

## Common String Methods

```python theme={null}
text = "python programming"

print(text.upper())
print(text.lower())
print(text.title())
print(text.replace("python", "Java"))
print(text.split())
```

### Output

```text theme={null}
PYTHON PROGRAMMING
python programming
Python Programming
Java programming
['python', 'programming']
```

### Explanation

Methods perform operations on strings.

***

# 17. Booleans

```python theme={null}
print(10 > 5)
```

### Output

```text theme={null}
True
```

### Explanation

Boolean values are either True or False.

***

# 18. Arithmetic Operators

### Addition

```python theme={null}
print(5 + 3)
```

Output

```text theme={null}
8
```

***

### Subtraction

```python theme={null}
print(10 - 4)
```

Output

```text theme={null}
6
```

***

### Multiplication

```python theme={null}
print(6 * 4)
```

Output

```text theme={null}
24
```

***

### Division

```python theme={null}
print(10 / 2)
```

Output

```text theme={null}
5.0
```

***

### Floor Division

```python theme={null}
print(10 // 3)
```

Output

```text theme={null}
3
```

***

### Modulus

```python theme={null}
print(10 % 3)
```

Output

```text theme={null}
1
```

***

### Exponent

```python theme={null}
print(2 ** 3)
```

Output

```text theme={null}
8
```

***

# 19. Order of Operations

```python theme={null}
print(2 + 3 * 4)
print((2 + 3) * 4)
```

### Output

```text theme={null}
14
20
```

### Explanation

Parentheses have the highest priority.

***

# 20. Comparison Operators

```python theme={null}
print(5 > 3)
print(5 < 3)
print(5 == 5)
print(5 != 3)
print(5 >= 5)
print(3 <= 5)
```

### Output

```text theme={null}
True
False
True
True
True
True
```

### Explanation

Compare two values and return True or False.

***

# 21. Assignment Operators

```python theme={null}
x = 5

x += 2
print(x)

x *= 3
print(x)
```

### Output

```text theme={null}
7
21
```

### Explanation

Assignment operators modify existing values.

***

# 22. Logical Operators

```python theme={null}
print(True and False)
print(True or False)
print(not True)
```

### Output

```text theme={null}
False
True
False
```

### Explanation

Logical operators combine Boolean expressions.

***

# 23. Identity Operators

```python theme={null}
x = [1, 2]
y = x
z = [1, 2]

print(x is y)
print(x is z)
```

### Output

```text theme={null}
True
False
```

### Explanation

`is` checks whether two variables refer to the same object.

***

# 24. Membership Operators

```python theme={null}
numbers = [1, 2, 3]

print(2 in numbers)
print(5 not in numbers)
```

### Output

```text theme={null}
True
True
```

### Explanation

Check whether a value exists inside a collection.

***

# 25. Bitwise Operators

```python theme={null}
print(5 & 3)
print(5 | 3)
print(5 ^ 3)
print(~5)
print(5 << 1)
print(5 >> 1)
```

### Output

```text theme={null}
1
7
6
-6
10
2
```

### Explanation

Bitwise operators work directly on the binary representation of integers.

***

# 26. Getting the Data Type

```python theme={null}
name = "Alice"

print(type(name))
```

### Output

```text theme={null}
<class 'str'>
```

### Explanation

`type()` returns the data type of a variable.

***

# 27. Multiple print() Arguments

```python theme={null}
name = "Alice"
age = 22

print("Name:", name)
print("Age:", age)
```

### Output

```text theme={null}
Name: Alice
Age: 22
```

### Explanation

`print()` can display multiple values separated by commas.

***

# 28. f-Strings (Formatted Strings)

```python theme={null}
name = "John"
age = 20

print(f"My name is {name} and I am {age} years old.")
```

### Output

```text theme={null}
My name is John and I am 20 years old.
```

### Explanation

f-Strings make it easy to insert variables into strings.

***

# 29. Keywords

```python theme={null}
import keyword

print(keyword.kwlist)
```

### Output (Partial)

```text theme={null}
['False', 'None', 'True', 'and', 'as', 'assert', ...]
```

### Explanation

Keywords are reserved words and cannot be used as variable names.

***
