|
| 1 | +--- |
| 2 | +title: Python Fundamentals |
| 3 | +layout: single |
| 4 | +sidebar: |
| 5 | + nav: "python_sidebar" |
| 6 | +toc: true |
| 7 | +toc_sticky: true |
| 8 | +--- |
| 9 | + |
| 10 | +Python is designed to be **simple, readable, and expressive**. Unlike many programming languages, Python emphasizes clean syntax and uses **indentation instead of braces** to define code blocks. |
| 11 | + |
| 12 | +This guide introduces the fundamental syntax used in Python programs. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Running Python |
| 17 | + |
| 18 | +You can run Python in several ways with the easiest being via the command line with the interactive Interpreter. |
| 19 | + |
| 20 | +### Interactive Interpreter |
| 21 | + |
| 22 | +#### Windows |
| 23 | +Open the Command Prompt and type: |
| 24 | + |
| 25 | +```bash |
| 26 | +py |
| 27 | +``` |
| 28 | + |
| 29 | +#### MacOS |
| 30 | +Open the Terminal: |
| 31 | + |
| 32 | +```bash |
| 33 | +python |
| 34 | +``` |
| 35 | + |
| 36 | +Note: On some systems (especially macOS/Linux), you may need to use: |
| 37 | + |
| 38 | +```python |
| 39 | +python3 |
| 40 | +``` |
| 41 | + |
| 42 | +Python Syntax |
| 43 | +--- |
| 44 | +Practice writing and executing the following syntax in your python file. |
| 45 | + |
| 46 | +Note: to exit the Interactive Interpreter write and execute ```quit``` |
| 47 | + |
| 48 | +## Comments |
| 49 | + |
| 50 | +Comments are ignored by Python and help explain your code. |
| 51 | + |
| 52 | +### Single Line Comment |
| 53 | + |
| 54 | +```python |
| 55 | +# This is a comment |
| 56 | +print("This code will execute!") |
| 57 | +``` |
| 58 | + |
| 59 | +### Multi-line Comment (Docstring style) |
| 60 | + |
| 61 | +```python |
| 62 | +"""This is often used for |
| 63 | +documentation strings""" |
| 64 | +``` |
| 65 | + |
| 66 | +## Variables |
| 67 | + |
| 68 | +Variables store values. Python automatically determines the variable type. |
| 69 | + |
| 70 | +```python |
| 71 | +name = "Alice"age = 30 |
| 72 | +height = 5.6 |
| 73 | +``` |
| 74 | + |
| 75 | +Variables **do not require explicit type declarations**. |
| 76 | + |
| 77 | +## Basic Data Types |
| 78 | + |
| 79 | +### Strings |
| 80 | + |
| 81 | +```python |
| 82 | +message = "Hello" |
| 83 | +``` |
| 84 | + |
| 85 | +### Integers |
| 86 | + |
| 87 | +```python |
| 88 | +count = 10 |
| 89 | +``` |
| 90 | + |
| 91 | +### Floats |
| 92 | + |
| 93 | +```python |
| 94 | +temperature = 98.6 |
| 95 | +``` |
| 96 | + |
| 97 | +### Booleans |
| 98 | + |
| 99 | +```python |
| 100 | +is_active = True |
| 101 | +``` |
| 102 | + |
| 103 | +## Printing Output |
| 104 | + |
| 105 | +The print() function displays output. |
| 106 | + |
| 107 | +```python |
| 108 | +print("Hello World") |
| 109 | +``` |
| 110 | + |
| 111 | +Multiple values (and types) can be printed in the same ```print()``` function, just separate them with commas: |
| 112 | + |
| 113 | +```python |
| 114 | +name = "Alice" |
| 115 | +print("Hello", name) |
| 116 | +``` |
| 117 | + |
| 118 | +## User Input |
| 119 | + |
| 120 | +Python can accept user input using ```input()```. |
| 121 | + |
| 122 | +```python |
| 123 | +name = input("Enter your name: ") |
| 124 | +print("Hello", name) |
| 125 | +``` |
| 126 | + |
| 127 | +Note: ```input()``` always returns a **string**. |
| 128 | + |
| 129 | +## Type Conversion |
| 130 | + |
| 131 | +Convert between data types using built-in functions. |
| 132 | +Common conversions: |
| 133 | + |
| 134 | +| Function | Purpose | |
| 135 | +| --------- | ------------------ | |
| 136 | +| `int()` | convert to integer | |
| 137 | +| `float()` | convert to decimal | |
| 138 | +| `str()` | convert to string | |
| 139 | + |
| 140 | +```python |
| 141 | +age = input("Enter age: ") |
| 142 | +age = int(age) |
| 143 | +temperature = float("98.6") |
| 144 | +number = str(42) |
| 145 | +``` |
| 146 | + |
| 147 | +## Arithmetic Operators |
| 148 | + |
| 149 | +Python supports standard mathematical operations. |
| 150 | + |
| 151 | +```python |
| 152 | + a = 10 |
| 153 | + b = 3 |
| 154 | + print(a + b) # addition |
| 155 | + print(a - b) # subtraction |
| 156 | + print(a * b) # multiplication |
| 157 | + print(a / b) # division |
| 158 | + print(a ** b) # exponent |
| 159 | + print(a % b) # remainder |
| 160 | + ``` |
| 161 | + |
| 162 | +## Comparison Operators |
| 163 | + |
| 164 | +Used to compare values. |
| 165 | + |
| 166 | +```python |
| 167 | +x = 10 |
| 168 | +y = 5 |
| 169 | +print(x > y) |
| 170 | +print(x < y) |
| 171 | +print(x == y) |
| 172 | +print(x != y) |
| 173 | +``` |
| 174 | + |
| 175 | +## Conditional Statements |
| 176 | + |
| 177 | +Conditional statements control program flow. |
| 178 | + |
| 179 | +### If Statement |
| 180 | + |
| 181 | +```python |
| 182 | +age = 18 |
| 183 | +if age >= 18: |
| 184 | + print("You are an adult") |
| 185 | +``` |
| 186 | + |
| 187 | +### If / Else |
| 188 | + |
| 189 | +```python |
| 190 | +age = 16 |
| 191 | +if age >= 18: |
| 192 | + print("Adult") |
| 193 | +else: |
| 194 | + print("Minor") |
| 195 | +``` |
| 196 | + |
| 197 | +### If / Elif / Else |
| 198 | + |
| 199 | +```python |
| 200 | +score = 85 |
| 201 | +if score >= 90: |
| 202 | + print("A") |
| 203 | +elif score >= 80: |
| 204 | + print("B") |
| 205 | +else: |
| 206 | + print("C") |
| 207 | +``` |
| 208 | + |
| 209 | +## Indentation |
| 210 | + |
| 211 | +Python uses **indentation to define blocks of code**. |
| 212 | + |
| 213 | +```python |
| 214 | +if True: |
| 215 | + print("This runs") |
| 216 | +``` |
| 217 | + |
| 218 | +Incorrect indentation will cause an error: |
| 219 | + |
| 220 | +```python |
| 221 | +if True: |
| 222 | + print("Error") |
| 223 | +``` |
| 224 | + |
| 225 | +Standard practice is **4 spaces per indentation level**. |
| 226 | + |
| 227 | +## Loops |
| 228 | + |
| 229 | +Loops allow code to run multiple times. |
| 230 | + |
| 231 | +### For Loop |
| 232 | + |
| 233 | +Used to iterate over sequences. |
| 234 | + |
| 235 | +```python |
| 236 | +for i in range(5): |
| 237 | + print(i) |
| 238 | +``` |
| 239 | + |
| 240 | +Output: |
| 241 | + |
| 242 | +```python |
| 243 | +0 |
| 244 | +1 |
| 245 | +2 |
| 246 | +3 |
| 247 | +4 |
| 248 | +``` |
| 249 | + |
| 250 | +### While Loop |
| 251 | + |
| 252 | +Runs until a condition becomes false. |
| 253 | + |
| 254 | +```python |
| 255 | +count = 0 |
| 256 | +while count < 5: |
| 257 | + print(count) |
| 258 | +count += 1 |
| 259 | +``` |
| 260 | + |
| 261 | +## Lists |
| 262 | + |
| 263 | +Lists store multiple values. |
| 264 | + |
| 265 | +```python |
| 266 | +fruits = ["apple", "banana", "orange"] |
| 267 | +``` |
| 268 | + |
| 269 | +Access elements: |
| 270 | + |
| 271 | +```python |
| 272 | +print(fruits[0]) |
| 273 | +``` |
| 274 | + |
| 275 | +Add an item: |
| 276 | + |
| 277 | +```python |
| 278 | +fruits.append("grape") |
| 279 | +``` |
| 280 | + |
| 281 | +Loop through a list: |
| 282 | + |
| 283 | +```python |
| 284 | +for fruit in fruits: print(fruit) |
| 285 | +``` |
| 286 | + |
| 287 | +## Dictionaries |
| 288 | + |
| 289 | +Dictionaries store **key-value pairs**. |
| 290 | + |
| 291 | +```python |
| 292 | +person = { "name": "Joe","age": 30,"city": "Denver"} |
| 293 | +``` |
| 294 | + |
| 295 | +Access values: |
| 296 | + |
| 297 | +```python |
| 298 | +print(person["name"]) |
| 299 | +``` |
| 300 | + |
| 301 | +Add a new key: |
| 302 | + |
| 303 | +```python |
| 304 | +person["email"] = "joe@example.com" |
| 305 | +``` |
| 306 | + |
| 307 | +Functions |
| 308 | +========= |
| 309 | + |
| 310 | +Functions organize reusable code. |
| 311 | + |
| 312 | +```python |
| 313 | +def greet(name): |
| 314 | + print("Hello", name) |
| 315 | +``` |
| 316 | + |
| 317 | +Call the function: |
| 318 | + |
| 319 | +```python |
| 320 | + greet("Alice") |
| 321 | + ``` |
| 322 | + |
| 323 | +Functions can return values: |
| 324 | + |
| 325 | +```python |
| 326 | +def add(a, b): |
| 327 | + return a + b |
| 328 | + |
| 329 | +result = add(3, 4) |
| 330 | +print(result) |
| 331 | +``` |
| 332 | + |
| 333 | +## Importing Libraries |
| 334 | + |
| 335 | +Python includes many built-in libraries. |
| 336 | + |
| 337 | +```python |
| 338 | +import mathprint(math.sqrt(16)) |
| 339 | +``` |
| 340 | + |
| 341 | +Import specific functions: |
| 342 | + |
| 343 | +```python |
| 344 | +from math import sqrt |
| 345 | +``` |
| 346 | + |
| 347 | +## Writing Your First Script |
| 348 | + |
| 349 | +Example program: |
| 350 | + |
| 351 | +```python |
| 352 | +name = input("What is your name? ") |
| 353 | +if name: |
| 354 | + print("Hello", name) |
| 355 | +else: |
| 356 | + print("Hello stranger") |
| 357 | +``` |
| 358 | + |
| 359 | +Save the file as: |
| 360 | + |
| 361 | +```bash |
| 362 | +hello.py |
| 363 | +``` |
| 364 | + |
| 365 | +Run it: |
| 366 | + |
| 367 | +```bash |
| 368 | + python hello.py |
| 369 | + ``` |
0 commit comments