0% found this document useful (0 votes)
17 views4 pages

Lesson 3 notes 1015

Lesson 3 covers computational thinking, emphasizing the importance of breaking down problems before coding. It introduces Python's memory handling, built-in functions, Boolean data types, string representations, and operations, as well as indexing, slicing, and type conversion. Key concepts include the immutability of strings and the use of logical operators.

Uploaded by

bamese3331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Lesson 3 notes 1015

Lesson 3 covers computational thinking, emphasizing the importance of breaking down problems before coding. It introduces Python's memory handling, built-in functions, Boolean data types, string representations, and operations, as well as indexing, slicing, and type conversion. Key concepts include the immutability of strings and the use of logical operators.

Uploaded by

bamese3331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson 3: Computational Thinking

• 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.

Think Like a Python Interpreter

• How Python works with memory:


1. Example 1: a = b
▪ If b has no value (unbound), Python will show an error.
2. Example 2: 2x = 10
▪ This is invalid because variable names can’t start with a number
3. Swapping variables:
▪ To swap x = 5000 and y = 3000, you need a third variable(e.g. z):
▪ z = x
▪ x = y
▪ y = z

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:

name = input("Enter your name: ")


print(name)

o print(): Shows output on the screen.


Boolean Data Type

• 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:

o AND stops checking if the first condition is False.


o OR stops checking if the first condition is True.

Just like oder of operations in math, python follows an order:

NOT comes first. Then AND. Finally, OR. That’s why to avoid confusion use
BRACKETS ()

• Without parentheses: True and False or True → True.


• With parentheses: (True and False) or True → True.

String Representation

• Strings are pieces of text and can be written in:


o Single quotes: 'Hello'
o Double quotes: "Hello"
• Triple quotes: Use ''' or """ for multi-line text.
• Special characters:
o \': Single quote.
o \": Double quote.
o \n: New line.
o \t: Tab.
o \\: Backslash.
R Strings

• What is it?
o An R String tells Python to treat \ as a normal character.

Example With R String:

o print(r"This is a path: C:\folder\file")


o Output: This is a path: C:\folder\file
o No need for \\.

Example Without R String:

o print(r"This is a path: C:\\folder\\file")


o Output: This is a path: C:\folder\file

String Operations

• Combine strings: "Hello" + " World" → Hello World.


• Repeat strings: "abc" * 3 → abcabcabc.
• Check strings:
o 'abc' < 'abd': Compares in dictionary order.
o 'bc' in 'abcde': Checks if 'bc' exists in 'abcde'.

Indexing and Slicing

• 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

• What they do:


o .capitalize(): Makes the first letter uppercase.
o .lower(), .upper(): Changes to all lowercase or uppercase.
o .find(): Finds where a word is in the string.
o .replace(): Changes one word to another.
o
• Important: Strings can’t change directly. These methods make a new string. Strings are
IMMUTABLE.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy