Python fundamentals
Variables, types, conditions and loops.
Level 101 · 23 lessons · 24 practice labs
Lessons
Getting started
- Running Python: the interpreter and scripts: You will learn to run Python both in the interactive interpreter and as a script from a file, and see its output.
- Setting up a code editor for Python: VS Code: You install VS Code, configure it for Python, run your script from the editor and jump straight to the line that failed.
- Identifiers and reserved words: You learn the naming rules for variables and functions, which words Python reserves for itself, and how to avoid clashes.
- Variables and types: You will learn how variables store data and tell apart basic types such as numbers, text and true/false.
- Numbers, operators and precedence: You will do maths in Python and understand the order in which operators run, so you can predict a result.
- Strings: indexing, slicing, methods, formatting: You will learn to take a piece of a text, change it and format values into new text.
- Text encoding: Unicode, UTF-8 and bytes: You understand how text is stored as bytes and can fix garbled-character errors using encode and decode.
- Input, output, comments and naming conventions (PEP 8): You will learn to take input, print results, and use comments and naming rules so your code is easy to read.
- Short task: a script that totals a till receipt
Flow and functions
- Conditions: if, elif, else and match-case: You will write programs that choose different paths depending on a situation using if, elif, else and match-case.
- Loops: for, while, break and continue: You will write loops that repeat a task and steer them with break and continue.
- Built-in functions: len, range, sorted, zip, enumerate: You get to know the functions that ship with Python and use them to shorten loops and data-handling code.
- Functions: parameters, return values, scope: You will put repeated work into a named function, pass data in, get a result back and understand where variables are visible.
- Flexible parameters: defaults, *args and **kwargs: You can write functions that accept any number of arguments and avoid the classic default-value pitfalls.
- Lambda expressions and map, filter, the sorted key: You write one-line anonymous functions and use them to filter, transform and sort lists by a custom rule.
- Docstrings and type hints: You document functions with docstrings and add parameter and return types so others can read your code.
- The script entry point: a main function and the __name__ check: You set up the standard structure that lets one file be both run directly and imported safely.
- Short task: a function that checks password rules
Robust code
- Errors, tracebacks and logging: You will read an error message and its traceback to find where a problem is, and record what a program does in a log.
- Reading and writing files: You will open a file, read it line by line and write new information to it safely.
- Modules and imports: You will learn how to bring ready-made modules into your program and how to split your own code across files.
- Short task: split a script into modules and make it error-proof
Capstone
- Capstone: write a log line counter: You write a small Python script that reads a fictional company's log file and counts certain lines.
Practice labs
- Asking again on bad input: Write an age prompt function that asks again up to three times on non-numeric or out-of-range input and then returns the default.
- Extracting the province code from a licence plate: Write a function that uses slicing and split to separate the province code, letters and digits of plates like '34 ABC 123'.
- Formatting names for name badges: Write a function that formats carelessly typed names as a capitalised first name and an all-caps surname.
- Zero-padding invoice numbers: Complete the functions that turn Defne Finans invoice numbers into the 'FTR-000042' form and build report lines with f-string alignment.
- Displaying a masked card number: Write a function that cleans card numbers containing spaces and dashes and shows only the last four digits, rejecting bad lengths and passing randomised tests.
- Splitting a bill per person: Write a function that takes an amount, a tip rate and a number of people and returns each share to two decimal places.
- Turning seconds into hours and minutes: Write a function that uses // and % to turn a duration in seconds into 'H:MM:SS' and pass the boundary-value tests.
- Fixing the cent rounding bug: Fix a till total that comes out one cent short because of float addition, using integer cents or Decimal, and pass tests with random amounts.
- Calculating shipping fees by weight: Write a function with if-elif that returns Sarıkız Kargo's fee by weight band and pass the tests at the band edges.
- Classifying status codes with match-case: Write a function that uses match-case patterns to sort HTTP status codes into success, redirect, client error and server error groups.
- Bounding a retry loop: Fix a retry loop that sometimes spins forever using an attempt counter, break and for-else, and pass tests that cover the failure cases too.
- Building a greeting with default parameters: Write a greeting function with default values for language and style and pass tests that call it with positional and keyword arguments.
- Sorting products with a lambda key: Write a function that sorts product dictionaries by stock status and then price using sorted with a lambda key.
- Finding the mutable default argument bug: Find and fix the list default that makes a function pile up earlier results on every call, and add a test that catches it.
- Measuring the byte length of text: Write a function that compares the character and UTF-8 byte lengths of messages with Turkish letters and flags those over 160 bytes.
- Reading a file in a legacy encoding: Complete a script that decodes a customer list saved as cp1254, rewrites it as UTF-8 and reports lines with invalid bytes.
- Truncating UTF-8 text at a byte limit: Write a function that shortens text to at most N bytes without splitting a character and pass tests with random multi-byte input.
- Falling back to defaults when a file is missing: Write a function that returns default values when the settings file is missing or unreadable and records the error with logging.
- Summing while skipping bad lines: Write a function that sums a file of numbers, skipping lines that are not numbers with a warning that gives the line number.
- Stopping code that runs on import: Fix a helper module that deletes files when imported by adding a main function and the __name__ check, and pass the test that proves importing is harmless.
- Updating a counter file safely: Harden a script that keeps a visit counter in a file with a with block, exception handling and a corrupt-content check, passing the empty, corrupt and missing file tests.
- Accepting a typed date in several formats: Write a function that accepts a typed date in three different formats, rejects invalid days and months and asks again with a clear message; an empty entry cancels the operation.
- Gathering a script into a main function: Move the lines scattered at the top level of a file into a main() function and show with the test that running the script directly still gives the same output.
- Returning an exit code from main: Rework main(argv) to take the argument list as a parameter and return 0 on success and 2 on bad usage, with the entry point passing that value to sys.exit.
About this course
Variables, types, conditions and loops.
What you will learn
- Getting started
- Flow and functions
- Robust code
- Capstone
Prerequisites
No prior knowledge is needed; the course starts from zero.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at beginner level; every term is explained where it first appears.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.