week 2
week 2
Below are detailed notes based on the topics listed in the attached image for Week 2 of your
Python course.
L2.1: Introduction
The second week focuses on advanced Python concepts, including dynamic typing, string
manipulation, conditional statements, and importing libraries.
It builds upon the basics covered in Week 1.
Understanding Variables
Variables are named memory locations used to store data.
Python is dynamically typed, meaning you don’t need to declare the type of a variable
explicitly.
Key Points
1. Variable Naming Rules:
Must start with a letter or underscore (_).
Cannot contain spaces or special characters.
Case-sensitive (myVar ≠ MyVar).
2. Example:
name = "Alice"
age = 25
print(name, age)
Benefits:
Flexibility in coding.
No need for explicit type declarations.
Drawbacks:
Can lead to runtime errors if types are mixed incorrectly.
Operators Recap
1. Arithmetic Operators:
+, -, *, /, %, //, **.
2. Comparison Operators:
==, !=, <, >, <=, >=.
3. Logical Operators:
and, or, not.
Expressions
Combine variables and operators to perform calculations.
Example:
a = 5
b = 3
result = (a + b) * b
print(result) # Output: 24
L2.5: Escape Characters and Types of Quotes
Escape Characters
Special characters used to represent actions like newlines or tabs within strings.
Common escape sequences:
Escape Sequence Meaning
\n Newline
\t Tab
Example:
Types of Quotes
1. Single quotes ('text')
2. Double quotes ("text")
3. Triple quotes ('''text''' or """text""") for multi-line strings.
Examples:
Conditional Statements
Allow decision-making in programs based on conditions.
Syntax:
if condition:
# Code block executed if condition is True
Example:
if-else Syntax
if condition:
# Code block executed if condition is True
else:
# Code block executed if condition is False
elif Syntax
if condition1:
# Code block executed if condition1 is True
elif condition2:
# Code block executed if condition2 is True
else:
# Code block executed if none of the conditions are True
Example:
What is a Library?
A library is a collection of pre-written code that can be reused in programs.
Importing Libraries
1. Use the import keyword to include libraries in your code.
import math
print(math.sqrt(16)) # Output: 4.0
L2.12: Conclusion
Week 2 covers intermediate Python concepts such as dynamic typing, advanced string
manipulation, conditional statements, and importing libraries.
⁂