Python is a language where you give step-by-step instructions to a computer. Everything comes down to three core
ideas: storing information (variables), making decisions (conditions), and repeating actions (loops).
Variables are like labeled boxes that store data. You assign values using “=”. There are different data types: strings
(text), integers (whole numbers), floats (decimals), and booleans (True/False). Strings can be modified using methods
like upper(), lower(), and can include special characters like \n (new line) and \t (tab).
Lists are used to store multiple items in one variable. They are ordered, changeable, and allow duplicates. Items in a
list are indexed starting at 0. You can access items using list[index]. You can add items with append(), remove them
with remove() or pop(), and find the length using len(). Lists can store different data types.
Python has four main collection types: lists (ordered, changeable), tuples (ordered, unchangeable), sets (unordered,
no duplicates), and dictionaries (ordered, key-value pairs).
Conditions allow your program to make decisions. The if statement runs code only if a condition is true. Use == to
compare values, not = (which assigns values). Other comparison operators include !=, <, >, <=, >=. Logical operators
include and, or, and not. An if-else structure allows one of two paths to run. If-elif-else allows multiple possible paths,
but only one will run.
Indentation is critical in Python. It defines blocks of code. All code inside an if statement or loop must be indented.
Improper indentation breaks the program.
Loops allow you to repeat code. A while loop repeats as long as a condition is true. It is useful when you don’t know
how many times the loop should run. You must ensure the condition eventually becomes false, or you will create an
infinite loop. A for loop repeats a fixed number of times and is commonly used with the range() function.
The range() function generates a sequence of numbers. range(5) produces 0 to 4. range(1,5) produces 1 to 4. In a for
loop, each value in the range is assigned to a variable one at a time.
For loops are best when you know how many times something should repeat. While loops are best when repetition
depends on a condition.
Algorithms are step-by-step procedures that combine variables, conditions, and loops to solve problems. Examples
include checking if a number is prime, calculating factorials, or repeatedly prompting a user for valid input.
Nested structures are when you place loops inside loops or if statements inside other if statements. This allows for
more complex logic.
When deciding which structure to use: use if when code should run only in certain situations, use if-else when exactly
one of two paths should run, use if-elif-else when multiple exclusive options exist, use for loops when repeating a
known number of times, and use while loops when repeating until a condition is met.
All Python programs are built from combining variables (state), conditions (logic), and loops (repetition).