|
| 1 | +### Variables |
| 2 | +- A variable is a symbolic type associated with a value. |
| 3 | +- A variable can be named anything, but it has some rules: |
| 4 | + 1. It cannot start with an integer |
| 5 | + 2. It cannot contain spaces |
| 6 | + 3. It can only contain a symbol, which is the underscore |
| 7 | + |
| 8 | +Examples of variable names: |
| 9 | +- Correct: |
| 10 | +`response` |
| 11 | +`top500` |
| 12 | +`one_apple` |
| 13 | +`first_numbers` |
| 14 | +`_usernames` |
| 15 | +`_last_` |
| 16 | +`current_` |
| 17 | + |
| 18 | + |
| 19 | +- Wrong: |
| 20 | +`top 500` |
| 21 | +`one apple` |
| 22 | +`1st_numbers` |
| 23 | +`^last_` |
| 24 | +`$current` |
| 25 | +`response#` |
| 26 | + |
| 27 | +### Strings |
| 28 | +A string is a sequence of characters surrounded by both type of quotation marks: |
| 29 | +- Single quotes `''` |
| 30 | +- Double quotes `""` |
| 31 | + |
| 32 | +- Anything surrounded by these quotation marks indicate a string |
| 33 | +- Every opening quote must have a closing quote to form a string |
| 34 | +- If opening a string with a single quote, the closing quote must be a single quote as well |
| 35 | + |
| 36 | +Examples: |
| 37 | +- Correct: |
| 38 | + |
| 39 | +`"This is a sample string using double quotes"` |
| 40 | + |
| 41 | +`'This is a sample string using single quotes'` |
| 42 | + |
| 43 | +`'This is a normal string'` |
| 44 | + |
| 45 | +`'50 + 51'` |
| 46 | + |
| 47 | +- Wrong: |
| 48 | + |
| 49 | +`'This string will end here.' From here the continuation is not a string'` |
| 50 | + |
| 51 | +`'This is a wrong string"` |
| 52 | + |
| 53 | +`"Same as this one'` |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +### Integers |
| 58 | +An integer is a whole number of any length that can be positive or negative, written without a fractional element. |
| 59 | + |
| 60 | +Example: |
| 61 | + |
| 62 | +`-1` `0` `1` `2` `3` `86400` |
| 63 | + |
| 64 | +### Floats |
| 65 | +A float is a number that can be positive or negative written with a fractional element. |
| 66 | + |
| 67 | +Example: |
| 68 | + |
| 69 | +`-100.54` `1.0` `59.1` `10.99` |
| 70 | + |
| 71 | +### Booleans |
| 72 | +Computers only understand binary — `1's` and `0's`. |
| 73 | + |
| 74 | +This way we can determine if a value is `True` or `False`. |
| 75 | + |
| 76 | +Strings, integers and other data types can indicate a boolean value. |
| 77 | + |
| 78 | +False data types: |
| 79 | +- Empty string: `''` |
| 80 | +- Empty list: `[]` |
| 81 | +- Zero int: `0` |
| 82 | +- Zero float: `'0.0'` |
| 83 | + |
| 84 | +True data types: |
| 85 | +- A non-empty string: `This string` |
| 86 | +- A list populated with at least one element: `[100, 55]` |
| 87 | +- An integer higher than zero: `1` |
| 88 | +- A float of which decimal number is higher than zero: `0.1` |
| 89 | + |
| 90 | +### Lists |
| 91 | +- A list is an ordered sequential data type |
| 92 | +- A list is used to store multiple elements in one place |
| 93 | +- A list is created by a pair of square brackets |
| 94 | + |
| 95 | +`shopping_list = ['cereals', 'milk', 'cherries']` |
| 96 | + |
| 97 | +`midterm_grades = [4, 9, 6, 6]` |
| 98 | + |
| 99 | +- A list can contain different data types |
| 100 | + |
| 101 | +`phone_numbers = ['james', 8067366796]` |
| 102 | + |
| 103 | +- Each element in a list holds a position (index) through which it can be accessed |
| 104 | + |
| 105 | +`shopping_list[0] # cereals` |
| 106 | + |
| 107 | +`shopping_list[1] # milk` |
| 108 | + |
| 109 | +`shopping_list[2] # cherries` |
| 110 | + |
| 111 | +- Elements in a list can be accessed in a range: |
| 112 | + |
| 113 | +`shopping_list[0:2] # ['cereals', 'milk']` |
| 114 | + |
| 115 | +- If the list is too big to count the elements, the last one can be accessed with a shortcut: |
| 116 | +`shopping_list[-1] # cherries` |
| 117 | + |
| 118 | + |
| 119 | +- A list can be reversed: |
| 120 | + |
| 121 | +`shopping_list[::-1] # ['cherries', 'milk', 'cereals']` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +### Tuples |
| 126 | +- A tuple is very much like a list, however: |
| 127 | + - A tuple is an ordered sequential data type which is **unchangeable** |
| 128 | +- A tuple is created by a sequence of elements separated by a comma |
| 129 | +- For better readability, it is surrounded by parenthesis |
| 130 | + |
| 131 | +Example: |
| 132 | + |
| 133 | +`shopping_list = 'apples', 'milk', 'cherries'` |
| 134 | + |
| 135 | +`midterm_grades = 4, 9, 6, 6` |
| 136 | + |
| 137 | +`phone_numbers = 'james', 8067366796` |
| 138 | + |
| 139 | +`better_readability = ('with', 'parenthesis')` |
| 140 | + |
| 141 | +### Sets |
| 142 | +- A set is an **unordered** sequential data type |
| 143 | +- A set's element cannot be accessed by its index |
| 144 | +- A set is created by a pair of curly brackets: |
| 145 | + |
| 146 | +`shopping_list = {'apples', 'milk', 'cherries'}` |
| 147 | + |
| 148 | +### Dictionaries |
0 commit comments