|
| 1 | +## Introduction to Variables |
| 2 | + |
| 3 | +**Variables** are a fundamental concept in programming, serving as containers |
| 4 | +to store data that can be used and manipulated throughout a program. They are |
| 5 | +used in every programming language and are essential for handling data in |
| 6 | +applications. |
| 7 | + |
| 8 | +### Key Characteristics of Variables: |
| 9 | + |
| 10 | +- **Storage**: Variables store data temporarily in memory, allowing the program |
| 11 | + to access and manipulate it as needed. |
| 12 | +- **Dynamic Typing in Python**: Python variables are *dynamically typed*, |
| 13 | + meaning that you don't need to declare the type of a variable when you create |
| 14 | + it. The type is inferred based on the value assigned to it. |
| 15 | + |
| 16 | +### Example of Variables in Python: |
| 17 | + |
| 18 | +```python |
| 19 | +name = "John" |
| 20 | +# Output: 'John' |
| 21 | + |
| 22 | +number = 10 |
| 23 | +# Output: 10 |
| 24 | + |
| 25 | +# Checking the memory address of variables |
| 26 | +id(name) |
| 27 | +# Output: 1759753155568 |
| 28 | + |
| 29 | +id(number) |
| 30 | +# Output: 140720682367704 |
| 31 | +``` |
| 32 | + |
| 33 | +In this example: |
| 34 | + |
| 35 | +- `name` stores a string value `"John"`. |
| 36 | +- `number` stores an integer value `10`. |
| 37 | +- `id()` function returns the memory address where the variable is stored. |
| 38 | + |
| 39 | +### Updating Variables: |
| 40 | + |
| 41 | +Variables in Python can be reassigned new values, and each time this happens, |
| 42 | +the variable points to a new memory address: |
| 43 | + |
| 44 | +```python |
| 45 | +name = "Ali" |
| 46 | +id(name) |
| 47 | +# Output: 1759753175616 |
| 48 | +``` |
| 49 | + |
| 50 | +Here, the variable `name` is updated from `"John"` to `"Ali"`, resulting in a |
| 51 | +change in its memory address, as Python creates a new object for the updated |
| 52 | +value. |
| 53 | + |
| 54 | +Variables are crucial for storing and manipulating data in programs. Python’s |
| 55 | +dynamic typing and ease of use make handling variables straightforward, |
| 56 | +allowing for quick development and flexibility. |
0 commit comments