Lesson 3 notes 1015
Lesson 3 notes 1015
• What it is: A way to solve problems step by step and turn those solutions into code.
• Two parts:
1. Language-independent: Learn how to break big problems into smaller,
manageable steps.
2. Language-dependent: Turn those steps into programming languages like Python
or Java.
• Important: Never start by writing code directly. First, understand and simplify the
problem.
Built-in Functions
• Common functions:
o min(): Finds the smallest value. Example: min(3, 5) → 3.
o max(): Finds the largest value. Example: max(3, 5) → 5.
o abs(): Finds the absolute value. Example: abs(-7) → 7.
o pow(): Calculates power. Example: pow(2, 3) → 8.
o id(): Shows where a variable is stored in memory.
o input(): Lets you type something. Example:
• What it is: A type with only two values: True and False.
• Operators:
o ==: Checks if two things are the same.
o < or >: Checks which is smaller or bigger.
o Works with strings too: 'a' < 'b'.
• Logical operators:
o AND: True only if both conditions are True.
o OR: True if at least one condition is True.
o NOT: Makes True into False and vice versa.
• In a nutshell/lazy way:
NOT comes first. Then AND. Finally, OR. That’s why to avoid confusion use
BRACKETS ()
String Representation
• What is it?
o An R String tells Python to treat \ as a normal character.
String Operations
• Indexing:
o Positions in a string start from 0.
o Use negative numbers to count from the end (-1 is the last character).
• Slicing:
o [start:end]: Get part of a string. Example: 'Hello'[0:2] → He.
o [start:end:step]: Skip characters using a step.
String Methods
Type Conversion
• Change types:
o int(): Turns a string or float into a number. Example: int('5') → 5.
o str(): Turns numbers into text. Example: str(5) → '5'.
o float(): Turns a string or number into a decimal.
• Errors: Converting text that isn’t a number (e.g., int("abc")) causes an error.