|
| 1 | +--- |
| 2 | +id: python-variables |
| 3 | +title: Python Variables |
| 4 | +sidebar_label: Python Variables #displays in sidebar |
| 5 | +sidebar_position: 3 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + Introduction of python, |
| 10 | + Python Syntax, |
| 11 | + Python Variables, |
| 12 | + |
| 13 | + ] |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +# Python Variables |
| 18 | + |
| 19 | +In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable is inferred based on the value assigned. |
| 20 | + |
| 21 | +Variables act as placeholders for data. They allow us to store and reuse values in our program. |
| 22 | + |
| 23 | + |
| 24 | +### 1. What is a Variable? |
| 25 | + |
| 26 | +A variable is like a container for storing data values. You don’t need to declare its type explicitly — Python handles it dynamically. |
| 27 | + |
| 28 | +```python |
| 29 | +x = 5 |
| 30 | +y = "Hello" |
| 31 | +```` |
| 32 | + |
| 33 | +Here: |
| 34 | + |
| 35 | +* `x` is of type `int` |
| 36 | +* `y` is of type `str` |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +### 2. How to Declare and Assign Variables |
| 41 | + |
| 42 | +You simply write a variable name, use the `=` sign, and assign a value. |
| 43 | + |
| 44 | +```python |
| 45 | +a = 10 |
| 46 | +name = "GeeksForGeeks" |
| 47 | +price = 99.99 |
| 48 | +is_active = True |
| 49 | +``` |
| 50 | + |
| 51 | +Python automatically understands the type of variable. |
| 52 | + |
| 53 | + |
| 54 | +### 3. Multiple Assignments in One Line |
| 55 | + |
| 56 | +Python allows assigning values to multiple variables in a single line. |
| 57 | + |
| 58 | +```python |
| 59 | +x, y, z = 1, 2, 3 |
| 60 | +``` |
| 61 | + |
| 62 | +You can also assign the **same value to multiple variables**: |
| 63 | + |
| 64 | +```python |
| 65 | +a = b = c = 100 |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +### 4. Variable Naming Rules |
| 70 | + |
| 71 | +* Must start with a letter (a–z, A–Z) or an underscore (\_) |
| 72 | +* Can contain letters, digits, and underscores |
| 73 | +* Are case-sensitive (`name` and `Name` are different) |
| 74 | +* Cannot use reserved keywords like `if`, `class`, `def`, etc. |
| 75 | + |
| 76 | +```python |
| 77 | +# Valid |
| 78 | +my_var = 1 |
| 79 | +_var = 2 |
| 80 | +var3 = 3 |
| 81 | + |
| 82 | +# Invalid |
| 83 | +3var = 10 # starts with digit |
| 84 | +my-var = 20 # hyphen not allowed |
| 85 | +def = 30 # 'def' is a keyword |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +### 5. Standard Data Types in Python |
| 90 | + |
| 91 | +Python variables can hold different types of data: |
| 92 | + |
| 93 | + |
| 94 | + <img src="./assets/data-type.png"/> |
| 95 | + |
| 96 | +### 6. Type Checking with `type()` |
| 97 | + |
| 98 | +```python |
| 99 | +x = 10 |
| 100 | +print(type(x)) # <class 'int'> |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +### 7. Changing Variable Type Dynamically |
| 106 | + |
| 107 | +Python allows dynamic typing: |
| 108 | + |
| 109 | +```python |
| 110 | +x = 5 |
| 111 | +print(type(x)) # <class 'int'> |
| 112 | + |
| 113 | +x = "Hello" |
| 114 | +print(type(x)) # <class 'str'> |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +### 8. Deleting a Variable with `del` |
| 119 | + |
| 120 | +You can remove a variable from memory using `del`. |
| 121 | + |
| 122 | +```python |
| 123 | +x = 100 |
| 124 | +del x |
| 125 | + |
| 126 | +print(x) # Raises NameError |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +### 9. Scope of Variables |
| 131 | + |
| 132 | +There are **two types of variable scope**: |
| 133 | + |
| 134 | +### 10. Global Variable |
| 135 | + |
| 136 | +Declared outside functions, accessible anywhere. |
| 137 | + |
| 138 | +```python |
| 139 | +x = "global" |
| 140 | + |
| 141 | +def show(): |
| 142 | + print(x) |
| 143 | + |
| 144 | +show() # Output: global |
| 145 | +``` |
| 146 | + |
| 147 | +### 11. Local Variable |
| 148 | + |
| 149 | +Declared inside functions and accessible only inside them. |
| 150 | + |
| 151 | +```python |
| 152 | +def greet(): |
| 153 | + msg = "Hello" |
| 154 | + print(msg) |
| 155 | + |
| 156 | +greet() |
| 157 | +print(msg) # Error: NameError |
| 158 | +``` |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +### 🟢 The `global` Keyword |
| 163 | + |
| 164 | +Use `global` to modify global variables inside a function. |
| 165 | + |
| 166 | +```python |
| 167 | +x = 10 |
| 168 | + |
| 169 | +def update(): |
| 170 | + global x |
| 171 | + x = 20 |
| 172 | + |
| 173 | +update() |
| 174 | +print(x) # Output: 20 |
| 175 | +``` |
| 176 | + |
| 177 | + |
| 178 | +### 12. Memory Management in Python |
| 179 | + |
| 180 | +* Python variables are **names** bound to **objects in memory**. |
| 181 | +* Use `id()` to get the memory address (or reference ID) of a variable. |
| 182 | + |
| 183 | +```python |
| 184 | +x = 5 |
| 185 | +print(id(x)) |
| 186 | +``` |
| 187 | + |
| 188 | +If two variables have the same immutable value, they may share the same memory. |
| 189 | + |
| 190 | +```python |
| 191 | +a = 100 |
| 192 | +b = 100 |
| 193 | +print(id(a) == id(b)) # True |
| 194 | +``` |
| 195 | + |
| 196 | + |
| 197 | +### 🟢 Example Program |
| 198 | + |
| 199 | +```python |
| 200 | +# Python Variable Example |
| 201 | + |
| 202 | +name = "Dhruba" |
| 203 | +age = 22 |
| 204 | +price = 49.99 |
| 205 | +is_valid = True |
| 206 | +items = ["pen", "notebook"] |
| 207 | + |
| 208 | +print("Name:", name) |
| 209 | +print("Age:", age) |
| 210 | +print("Price:", price) |
| 211 | +print("Valid:", is_valid) |
| 212 | +print("Items:", items) |
| 213 | +``` |
| 214 | + |
| 215 | +**Output:** |
| 216 | + |
| 217 | +``` |
| 218 | +Name: Dhruba |
| 219 | +Age: 22 |
| 220 | +Price: 49.99 |
| 221 | +Valid: True |
| 222 | +Items: ['pen', 'notebook'] |
| 223 | +``` |
| 224 | + |
| 225 | + |
| 226 | +## Summary |
| 227 | + |
| 228 | +* Python variables store different types of data without explicit declaration. |
| 229 | +* Variables are case-sensitive and follow naming rules. |
| 230 | +* Scope determines where a variable is accessible. |
| 231 | +* `global` and `del` are important keywords for variable handling. |
| 232 | +* Python handles memory management internally but allows inspection via `id()`. |
| 233 | + |
| 234 | + |
| 235 | +### Highlights |
| 236 | + |
| 237 | +* Covers both global and local variables |
| 238 | +* Explains `del`, `global`, and `id()` functions |
| 239 | +* Includes formatted tables and output blocks |
| 240 | +* Beginner-friendly explanation with examples |
0 commit comments