0% found this document useful (0 votes)
0 views

Python Veriable

This document provides a comprehensive guide on Python variables, covering their syntax, naming rules, types, and best practices. It explains dynamic typing, variable scope, and the use of global and local variables, along with examples for clarity. Key takeaways emphasize the lack of explicit type declaration and the importance of following naming conventions.

Uploaded by

Amadi Samuel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Veriable

This document provides a comprehensive guide on Python variables, covering their syntax, naming rules, types, and best practices. It explains dynamic typing, variable scope, and the use of global and local variables, along with examples for clarity. Key takeaways emphasize the lack of explicit type declaration and the importance of following naming conventions.

Uploaded by

Amadi Samuel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

# **Python Variables: A Complete Guide**

Variables in Python are used to store data values. Unlike some other programming
languages, Python is **dynamically typed**, meaning you don’t need to declare a
variable’s type explicitly.

---

## **1. Variable Basics**


### **Syntax:**
```python
variable_name = value
```
- No need for keywords like `var`, `let`, or `const` (as in JavaScript).
- Variable names are **case-sensitive** (`age ≠ Age`).

### **Example:**
```python
x = 10 # Integer
name = "Alice" # String
is_active = True # Boolean
```

---

## **2. Variable Naming Rules**


✅ **Allowed:**
- Letters (`a-z`, `A-Z`)
- Digits (`0-9`) but **not at the start**
- Underscore (`_`)

❌ **Not Allowed:**
- Start with a digit (`1var` ❌)
- Special characters (`@var` ❌)
- Python keywords (`if`, `for`, `while` ❌)

### **Examples:**
```python
age = 25 # Valid
user_name = "Bob" # Valid
1st_name = "Tom" # ❌ Invalid (starts with a digit)
```

---

## **3. Variable Types (Dynamic Typing)**


Python infers the type automatically.
You can check the type using `type()`.

| Type | Example |
|------|---------|
| `int` | `x = 10` |
| `float` | `y = 3.14` |
| `str` | `name = "Alice"` |
| `bool` | `is_valid = True` |
| `list` | `nums = [1, 2, 3]` |
| `tuple` | `point = (4, 5)` |
| `dict` | `person = {"name": "Bob"}` |
### **Example:**
```python
x = 5
print(type(x)) # Output: <class 'int'>

x = "Hello"
print(type(x)) # Output: <class 'str'> (type changed dynamically)
```

---

## **4. Multiple Assignment**


You can assign multiple variables in one line.

### **Example:**
```python
a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3

# Swap variables easily


x, y = 10, 20
x, y = y, x
print(x, y) # Output: 20 10
```

---

## **5. Constants (Convention)**


Python doesn’t enforce constants, but by **convention**, use uppercase names.

```python
PI = 3.14159
MAX_USERS = 100
```

---

## **6. Global vs Local Variables**


- **Local**: Inside a function (accessible only there).
- **Global**: Outside functions (accessible everywhere).

### **Example:**
```python
z = 100 # Global variable

def my_func():
z = 50 # Local variable (shadows global z)
print(z)

my_func() # Output: 50 (local)


print(z) # Output: 100 (global)
```

### **Modifying a Global Variable Inside a Function**


Use the `global` keyword.

```python
count = 0
def increment():
global count
count += 1

increment()
print(count) # Output: 1
```

---

## **7. Deleting a Variable**


Use `del` to remove a variable.

```python
x = 10
print(x) # Output: 10
del x
print(x) # ❌ Error: name 'x' is not defined
```

---

## **8. Variable Scope (LEGB Rule)**


Python resolves variable names in this order:
1. **L**ocal (inside function)
2. **E**nclosing (nested functions)
3. **G**lobal (module-level)
4. **B**uilt-in (Python keywords like `print`, `len`)

### **Example:**
```python
x = "global"

def outer():
x = "enclosing"
def inner():
x = "local"
print(x) # Output: "local"
inner()
print(x) # Output: "enclosing"

outer()
print(x) # Output: "global"
```

---

## **9. Best Practices**


✔ Use **descriptive names** (`user_age` instead of `ua`).
✔ Follow **snake_case** for variables (`user_name`).
✔ Avoid single letters (`x`, `y`) unless in loops/math.
✔ Use `UPPERCASE` for constants (`MAX_SIZE = 100`).

---

## **Summary Table**
| Concept | Example |
|---------|---------|
| **Declare** | `x = 10` |
| **Type** | `type(x)` → `<class 'int'>` |
| **Multiple Assign** | `a, b = 1, 2` |
| **Global** | `global var` |
| **Delete** | `del x` |
| **Constant** | `PI = 3.14` |

---

### **Key Takeaways**


✅ No explicit type declaration (dynamic typing).
✅ Case-sensitive names (`var ≠ Var`).
✅ `global` keyword modifies global variables inside functions.
✅ `del` removes a variable.
✅ Follow **snake_case** naming convention.

Would you like more details on any part? 😊

You might also like