|
| 1 | +--- |
| 2 | +description: Introduction to Python variables. |
| 3 | +--- |
| 4 | + |
| 5 | +# Introduction to Variables |
| 6 | + |
| 7 | +??? note "Brief on Computer RAM" |
| 8 | + |
| 9 | + In this chapter, we will also be talking about Random Access Memory (RAM) to better understand |
| 10 | + variables, but first, let's quickly cover what it is. RAM is where your computer stores the data of |
| 11 | + a running program. It is fast, but volatile (wiped when power is lost, unlike disk storage). |
| 12 | + Whenever you run your Python program, it loads it into the RAM and the instructions are then fed to |
| 13 | + the CPU by your operating system. |
| 14 | + |
| 15 | +Variables give programmers an easy way to work with the computer's memory - they act as a bridge |
| 16 | +that lets us use and manipulate memory without dealing with it directly. That is all variables do. |
| 17 | +Different languages have different syntax, rules, types of variables, etc., but in the end, they are |
| 18 | +all doing the same task. As far as we are concerned, Python does not have multiple types of |
| 19 | +variables. |
| 20 | + |
| 21 | +Let's take a look at how we can define a variable in python: |
| 22 | + |
| 23 | +```python |
| 24 | +name = "Alan" |
| 25 | +``` |
| 26 | + |
| 27 | +Let's break this down. Over here, `name` is what we call a variable. `"Alan"` is the data we're |
| 28 | +assigning to that variable through the assignment operator (`=`). |
| 29 | + |
| 30 | +!!! note |
| 31 | + |
| 32 | + We will learn more about data types and operators in later chapters. We will be focusing mostly on |
| 33 | + variables for now. |
| 34 | + |
| 35 | +So, where does the memory that we discussed before come into play here? When we create a variable in |
| 36 | +Python, it automatically reserves some space in memory and places the data there, which the variable |
| 37 | +then points to. |
| 38 | + |
| 39 | +<figure markdown="span"> |
| 40 | + |
| 41 | +{ width="600" } |
| 42 | + |
| 43 | +<figcaption> |
| 44 | + The numbers indicate memory addresses and `~` indicate some random data from other programs, |
| 45 | + irrelevant to us. You can imagine memory addresses in a similar concept to your house address. |
| 46 | + They both are used to locate the whereabouts. |
| 47 | +</figcaption> |
| 48 | + |
| 49 | +</figure> |
| 50 | + |
| 51 | +!!! note |
| 52 | + |
| 53 | + The above image is only an example of how variables point to memory addresses. Note that it is not a |
| 54 | + complete representation of how computer memory looks like. Only meant for understanding purposes. |
| 55 | + |
| 56 | +In the diagram above, the variable `name` points to memory addresses `3` - `6`; but notice we never |
| 57 | +had to specify that in our code. That's because Python handles this tracking for us automatically. |
| 58 | +This is really the whole point of variables: instead of manually keeping track of which data lives |
| 59 | +at which memory address, we can just use a variable name as a clear, readable label for that piece |
| 60 | +of memory. |
| 61 | + |
| 62 | +Now that we understand why we use variables, we must also know the rules for declaring variables in |
| 63 | +Python. There are 4 simple rules which follow: |
| 64 | + |
| 65 | +1. Variables cannot contain any special characters other than underscore (`_`). |
| 66 | +1. Variables cannot have spaces in them, use underscores instead. |
| 67 | +1. Variables cannot start with a number but can contain numbers inside or at the end of them. |
| 68 | +1. Variables cannot share the same names as Python keywords (keywords are reserved words with |
| 69 | + special functionality in Python). |
| 70 | + |
| 71 | +!!! note "Case Sensitivity" |
| 72 | + |
| 73 | + Python is a case sensitive language, meaning, in this piece of code: |
| 74 | + |
| 75 | + ```python |
| 76 | + name = "Zeed" |
| 77 | + Name = "Stun" |
| 78 | + ``` |
| 79 | + |
| 80 | + There now exists two variables, `name` and `Name` instead of one. A good rule of thumb is to always |
| 81 | + use `snake_casing` convention for your variables which is to keep your variables in all lower cases |
| 82 | + and use underscores for word separation. |
| 83 | + |
| 84 | +## Examples |
| 85 | + |
| 86 | +Here is a list of **invalid** python variables: |
| 87 | + |
| 88 | +```python |
| 89 | +full name = "James Bond" # Contains a space |
| 90 | +age! = 90 # Contains a special character |
| 91 | +finally = "yes" # Conflicts with the Python keyword "finally" |
| 92 | +20money = 900 # Starts with a number |
| 93 | +``` |
| 94 | + |
| 95 | +Below is a list of **valid** python variables: |
| 96 | + |
| 97 | +```python |
| 98 | +full_name = "Bonding Jameful" |
| 99 | +age = 90 |
| 100 | +answer_1 = "yes" |
| 101 | +answer_2 = "no" |
| 102 | +``` |
0 commit comments