|
| 1 | +# Welcome to Python! |
| 2 | + |
| 3 | +Hello reader, |
| 4 | + |
| 5 | +This document will jumpstart your understanding of Python. It will cover all the foundational aspects of the programming language, and provide examples of how to use each of them in practice. Whenever you get stuck on the exercises in this book, feel free to come back to this section as well - after all, even the most experienced programmers have to review the basics sometimes! |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | +**[TODO]** put in links |
| 9 | + |
| 10 | +1. [Data Types](#data-types) |
| 11 | +2. [Control Flow]() |
| 12 | +3. [Functions]() |
| 13 | +4. [Container Types]() |
| 14 | +5. [Credits](#credits) |
| 15 | + |
| 16 | +## Data Types |
| 17 | + |
| 18 | +### Math Operators |
| 19 | + |
| 20 | +From **Highest** to **Lowest** precedence: |
| 21 | + |
| 22 | +| Operators | Operation | Example | |
| 23 | +| --------- | ---------------- | --------------- | |
| 24 | +| ** | Exponent | `2 ** 3 = 8` | |
| 25 | +| % | Modulus/Remainder | `22 % 8 = 6` | |
| 26 | +| // | Integer division | `22 // 8 = 2` | |
| 27 | +| / | Division | `22 / 8 = 2.75` | |
| 28 | +| * | Multiplication | `3 * 3 = 9` | |
| 29 | +| - | Subtraction | `5 - 2 = 3` | |
| 30 | +| + | Addition | `2 + 2 = 4` | |
| 31 | + |
| 32 | +Examples of expressions in the interactive shell: |
| 33 | + |
| 34 | +```python |
| 35 | +2 + 3 * 6 |
| 36 | +``` |
| 37 | + |
| 38 | +```python |
| 39 | +(2 + 3) * 6 |
| 40 | +``` |
| 41 | + |
| 42 | +```python |
| 43 | +2 ** 8 |
| 44 | +``` |
| 45 | + |
| 46 | +```python |
| 47 | +23 // 7 |
| 48 | +``` |
| 49 | + |
| 50 | +```python |
| 51 | +23 % 7 |
| 52 | +``` |
| 53 | + |
| 54 | +```python |
| 55 | +(5 - 1) * ((7 + 1) / (3 - 1)) |
| 56 | +``` |
| 57 | + |
| 58 | +### Data Types |
| 59 | + |
| 60 | +| Data Type | Examples | |
| 61 | +| ---------------------- | ----------------------------------------- | |
| 62 | +| Integers | `-2, -1, 0, 1, 2, 3, 4, 5` | |
| 63 | +| Floating-point numbers | `-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25` | |
| 64 | +| Strings | `'a', 'aa', 'aaa', 'Hello!', '11 cats'` | |
| 65 | + |
| 66 | +### String Concatenation and Replication |
| 67 | + |
| 68 | +String concatenation: |
| 69 | + |
| 70 | +```python |
| 71 | +'Alice' 'Bob' |
| 72 | +``` |
| 73 | + |
| 74 | +Note: Avoid `+` operator for string concatenation. Prefer string formatting. |
| 75 | + |
| 76 | +String Replication: |
| 77 | + |
| 78 | +```python |
| 79 | +'Alice' * 5 |
| 80 | +``` |
| 81 | + |
| 82 | +### Variables |
| 83 | + |
| 84 | +You can name a variable anything as long as it obeys the following rules: |
| 85 | + |
| 86 | +1. It can be only one word. |
| 87 | +2. It can use only letters, numbers, and the underscore (`_`) character. |
| 88 | +3. It can’t begin with a number. |
| 89 | +4. Variable name starting with an underscore (`_`) are considered as "unuseful`. |
| 90 | + |
| 91 | +Example: |
| 92 | + |
| 93 | +```python |
| 94 | +spam = 'Hello' |
| 95 | +``` |
| 96 | + |
| 97 | +```python |
| 98 | +_spam = 'Hello' |
| 99 | +``` |
| 100 | + |
| 101 | +`_spam` should not be used again in the code. |
| 102 | + |
| 103 | +### Comments |
| 104 | + |
| 105 | +Inline comment: |
| 106 | + |
| 107 | +```python |
| 108 | +# This is a comment |
| 109 | +``` |
| 110 | + |
| 111 | +Multiline comment: |
| 112 | + |
| 113 | +```Python |
| 114 | +# This is a |
| 115 | +# multiline comment |
| 116 | +``` |
| 117 | + |
| 118 | +Code with a comment: |
| 119 | + |
| 120 | +```python |
| 121 | +a = 1 # initialization |
| 122 | +``` |
| 123 | + |
| 124 | +Please note the two spaces in front of the comment. |
| 125 | + |
| 126 | +Function docstring: |
| 127 | + |
| 128 | +```python |
| 129 | +def foo(): |
| 130 | + """ |
| 131 | + This is a function docstring |
| 132 | + You can also use: |
| 133 | + ''' Function Docstring ''' |
| 134 | + """ |
| 135 | +``` |
| 136 | + |
| 137 | +### The print Function |
| 138 | + |
| 139 | +```python |
| 140 | +print('Hello world!') |
| 141 | +``` |
| 142 | + |
| 143 | +```python |
| 144 | +a = 1 |
| 145 | +print('Hello world!', a) |
| 146 | +``` |
| 147 | + |
| 148 | +### The input Function |
| 149 | + |
| 150 | +Example Code: |
| 151 | + |
| 152 | +```python |
| 153 | +myName = input('What is your name?') # ask for their name |
| 154 | +print('It is good to meet you, {}'.format(myName)) |
| 155 | +``` |
| 156 | + |
| 157 | +### The len Function |
| 158 | + |
| 159 | +Evaluates to the integer value of the number of characters in a string: |
| 160 | + |
| 161 | +```python |
| 162 | +len('hello') |
| 163 | +``` |
| 164 | + |
| 165 | +Note: test of emptiness of strings, lists, dictionary, etc, should **not** use len, but prefer direct |
| 166 | +boolean evaluation. |
| 167 | + |
| 168 | +```python |
| 169 | +a = [1, 2, 3] |
| 170 | +if a: |
| 171 | + print("the list is not empty!") |
| 172 | +``` |
| 173 | + |
| 174 | +### The str, int, and float Functions |
| 175 | + |
| 176 | +Integer to String or Float: |
| 177 | + |
| 178 | +```python |
| 179 | +str(29) |
| 180 | +``` |
| 181 | + |
| 182 | +```python |
| 183 | +print('I am {} years old.'.format(str(29))) |
| 184 | +``` |
| 185 | + |
| 186 | +```python |
| 187 | +str(-3.14) |
| 188 | +``` |
| 189 | + |
| 190 | +Float to Integer: |
| 191 | + |
| 192 | +```python |
| 193 | +int(7.7) |
| 194 | +``` |
| 195 | + |
| 196 | +```python |
| 197 | +int(7.7) + 1 |
| 198 | +``` |
| 199 | + |
| 200 | +## Credits |
| 201 | + |
| 202 | +This section would not be possible if not for the following sources: |
| 203 | + |
| 204 | +1. *[Automate the Boring Stuff with Python](https://automatetheboringstuff.com/)*, a book by Al Sweigart under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/). |
| 205 | +2. The [Python Cheatsheet](https://www.pythoncheatsheet.org/) website and [GitHub repository](https://github.com/wilfredinni/python-cheatsheet) by wilfredinni. |
0 commit comments