Python scope of variables

Published: 14 July 2025
on channel: MR Robo
34
6

Sure! Here's a **simple and clear description of Python variables**:

---

🐍 *Python Variables - Explained*

A *variable* in Python is a name that refers to a value stored in memory. You use variables to store data that your program can use and manipulate.

---

📌 *Key Points:*

1. *No Declaration Needed:*

You don't need to declare the type of variable beforehand.
Example:

```python
x = 5 # integer
name = "Ali" # string
pi = 3.14 # float
```

2. *Dynamic Typing:*

The type of variable can change depending on the value assigned.
Example:

```python
x = 10 # x is an integer
x = "Hello" # now x is a string
```

3. *Variable Naming Rules:*

Must start with a letter or underscore `_`
Can contain letters, digits, and underscores
Case-sensitive (e.g., `age` and `Age` are different)

✅ Valid:

```python
user_name = "Ahmed"
_count = 10
```

❌ Invalid:

```python
1name = "Ali" # starts with a number
user-name = "Zara" # contains a hyphen
```

4. *Assigning Multiple Variables:*

```python
a, b, c = 1, 2, 3
```

5. *Global vs Local Variables:*

**Global**: declared outside functions, accessible everywhere.
**Local**: declared inside functions, only accessible inside them.

---

📘 Example:

```python
name = "Sara" # global variable

def greet():
local_msg = "Hello"
print(local_msg, name) # access global inside function

greet()
```

---

Let me know if you'd like notes in PDF, or examples with input/output!


On this page of the site you can watch the video online Python scope of variables with a duration of hours minute second in good quality, which was uploaded by the user MR Robo 14 July 2025, share the link with friends and acquaintances, this video has already been watched 34 times on youtube and it was liked by 6 viewers. Enjoy your viewing!