Python Programming Basics
Python Programming Fundamentals Study Notes
Why Learn Programming?
Programming allows you to take an idea in your head and turn it into a series of steps that a computer can understand and execute. Every app, website, or digital tool runs because someone programmed it. Learning to program is a powerful skill that enables you to create games, build internal company tools, automate tasks, or perform scientific analysis.
Why Python?
Python is a general-purpose programming language — it can be used for web apps, games, internal tools, data analysis, and more. It is concise, easy to read, and beginner-friendly, and it consistently ranks among the most popular programming languages. The concepts you learn in Python (variables, loops, functions, conditionals) transfer directly to other languages. Each programming language has a different syntax (rules and structure), but the underlying logic is the same.
Getting Started: Installing Python
Key concept: Python interprets code line by line at runtime, so there is no separate compilation step.
Check if Python is already installed: open Command Prompt (Windows) or Terminal (macOS/Linux) and type
python --version. If the version number appears, Python is present.If not installed, go to python.org, click Downloads, and download the recommended version for your operating system (the video used Python 3.9.2 but newer versions are fine).
During installation, check the box that says Add Python to PATH — this lets your system find Python from any command line. Then click Install Now.
After installation, verify by running
python --versionagain.
Using the Python Terminal
Open Python from the Start menu (Windows) or type python in the terminal. You can type code directly and Python will interpret and evaluate it immediately:
1 + 2→ outputs3print("Hello World")→ prints the string
The print function sends output to the screen. To exit the Python terminal, type exit() or close the window.
Writing Code in a Text Editor
You can write Python code in any text editor (e.g., Notepad). Save the file with the .py extension (e.g., hello_world.py). Then run it from the command prompt by typing python and dragging the file into the terminal, or typing the full path. The command python hello_world.py executes the file.
Setting Up an IDE: Visual Studio Code
An IDE (Integrated Development Environment) makes coding easier by providing syntax highlighting, line numbers, code suggestions, and debugging tools. Visual Studio Code (VS Code) is a free, cross-platform IDE that works well with Python.
Download from code.visualstudio.com
Install the Python extension by Microsoft (search in the Extensions panel)
Set the Python interpreter: press
Ctrl+Shift+P, type Python: Select Interpreter, and choose the Python version you installed.Open a
.pyfile: you'll see line numbers, colored syntax, and helpful tooltips when you hover over functions.
Running Code in VS Code
Right-click in the editor and select Run Python File in Terminal.
Or click the play button (▶) in the top-right corner.
Operators and Numbers
Python supports all standard arithmetic operators:
Operator | Meaning | Example |
|---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division (float) |
|
| Floor division (integer result) |
|
| Modulus (remainder) |
|
| Exponentiation |
|
Whitespace (spaces) does not affect arithmetic; use it for readability:
1 + 2is fine.Python follows the order of operations (PEMDAS): parentheses, exponents, multiplication/division (left to right), addition/subtraction (left to right).
Do not use commas in numbers (e.g., write
1000000, not1,000,000).
Working with Strings
A string is a sequence of characters enclosed in quotes. You can use double quotes (") or single quotes ('):
"what's up"works when you need an apostrophe inside the string.Use single quotes to include double quotes inside:
'He said "hello"'.
Whitespace inside strings is preserved in the output.
Comments
A comment is a line of code that Python ignores — used to explain code or temporarily disable lines.
Start a comment with the
#symbol.Comments can be on their own line or at the end of a line of code.
Example:
# This prints textorprint("Hello") # greeting
Comments are valuable for documenting code and for troubleshooting (commenting out lines).
Variables
A variable is like a named container that holds a value. You create a variable by giving it a name and assigning a value with a single equal sign (=).
Variable Naming Rules
Must contain only letters, numbers, and underscores.
Must not start with a digit.
Cannot contain spaces.
Cannot be a reserved keyword (e.g.,
if,while,break).Should be short and descriptive.
Variable names are case-sensitive (
RedBucketandredbucketare different).
Dynamic Typing
Python automatically determines the data type of a variable. You do not need to declare whether it is a string, integer, etc. Use the type() function to check:
red_bucket = "Kevin"→type(red_bucket)returns<class 'str'>red_bucket = 10→type(red_bucket)returns<class 'int'>
Reassigning a variable overwrites its previous value.
Deleting a Variable
Use del variable_name to completely remove a variable from memory. Trying to use it afterward raises a NameError.
Getting User Input
Use the input() function to ask the user for a value:
red_bucket = input("What do you want to put in the bucket? ")The input is always captured as a string, so numbers need conversion later if arithmetic is needed.
Conditional Logic (if/elif/else)
Comparison operators compare two values and return True or False:
Operator | Meaning | Example (True) |
|---|---|---|
| Equal to |
|
| Not equal to |
|
| Less than |
|
| Greater than |
|
| Less than or equal |
|
| Greater than or equal |
|
Important: Use
==for equality testing;=is for assignment.
An if statement allows execution of code only when a condition is true:
if age < kindergarten_age:
print("Thomas should be in preschool")Use elif (short for "else if") to add additional conditions.
Use else as a catch-all for when no condition is true.
Each condition block must be indented (VS Code does this automatically after a colon).
Flow: Python evaluates conditions from top to bottom. The first true condition's block runs, then it exits the entire if/elif/else structure. else runs only if none of the previous conditions were true.
Functions
A function is a reusable block of code that performs a specific task. You've already used built-in functions like print() and input().
Defining a Function
Use the def keyword:
def function_name(parameter1, parameter2):
# code block
return valueThe function name should follow the same rules as variable names.
A colon ends the definition line; the function body is indented.
Parameters are inputs the function expects when called. You can have zero or more.
The
returnstatement sends a value back to the caller. If omitted, the function returnsNone.
Calling a Function
result = add_ten_to_age(3)
print(result) # outputs 13You must define a function before you call it. Typically, function definitions appear at the top of a file.
Functions can accept arguments (the values you pass in) and return a value.
Example: A Function with Parameters and If/Elif/Else
def school_age_calculator(age, name):
if age < 5:
print(f"Enjoy the time, {name} is only {age}.")
elif age == 5:
print(f"Enjoy kindergarten, {name}.")
else:
print("They grow up so fast!")Benefits
Modularity — break complex tasks into smaller parts.
Reusability — write once, use many times.
Maintainability — update the function, and every call gets the fix.
Loops
Loops allow you to execute a block of code repeatedly.
While Loop
Repeats as long as the condition is True:
x = 0
while x < 5:
print(x)
x = x + 1 # increment to avoid infinite loopThe condition is checked at the start of each iteration.
If you forget to update the variable, the loop runs forever (infinite loop).
For Loop
Iterates over a sequence (e.g., a range of numbers, items in a list):
for x in range(5, 10): # start at 5, go up to but not including 10
print(x)range(start, stop)produces numbers fromstartup tostop - 1.You can also loop directly over a list:
for d in days: print(d)
Break and Continue
break— immediately exits the loop.continue— skips the rest of the current iteration and moves to the next.
for d in days:
if d == "Thursday":
break # stops and no further days printed
print(d)for d in days:
if d == "Thursday":
continue # skips Thursday, continues with Friday...
print(d)Libraries and Modules
A library (or module) is pre-written code that you can import to add functionality. Instead of writing everything from scratch, you build on others' work.
Import a module with import module_name:
import math
print("Value of pi is:", math.pi)Use the module name followed by a dot and the specific function/constant.
Popular Python libraries:
math,random,datetime,os, and many more.
Common Errors and Debugging
1. Syntax Errors
Caused by violating Python's grammar rules (e.g., missing quotes, missing colon).
Example:
print(Hello World)— error:NameError: name 'Hello' is not defined(should be"Hello World").Look at the error message; it often points to the line and problem.
2. Runtime Errors
Occur while the program is running (e.g., division by zero).
Example:
10 / 0→ZeroDivisionError: division by zero.The error message (e.g., Traceback) tells you the line and type of error.
3. Semantic Errors (Logic Bugs)
The code runs without errors but produces wrong results.
Example:
print("Hello", name)wherename = "Kevin"might produce "Hello Kevin" if you forgot to concatenate properly. But if you mistakenly wroteprint("Hello name"), it prints "Hello name" literally.These are harder to find; you must trace the logic step-by-step. Use print statements to check values.
Debugging tip: Search for error messages on sites like Stack Overflow to see how others solved similar problems.
These fundamentals — variables, conditional logic, functions, loops, and libraries — form the core of Python programming. Mastering them gives you a solid foundation to build any application you can imagine.

Ask anything
We've read this whole study set, so questions about it get the best answers.