0% found this document useful (0 votes)
13 views

week 2

Maths
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)
13 views

week 2

Maths
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/ 6

Detailed Notes for Week 2 Topics

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.

L2.2: Variables: A Programmer's Perspective

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)

L2.3: Variables Revisited: Dynamic Typing


Dynamic Typing in Python
Python allows variables to change their type during runtime.
Example:
x = 10 # Integer
x = "Hello" # String
print(x) # Output: Hello

Benefits:
Flexibility in coding.
No need for explicit type declarations.

Drawbacks:
Can lead to runtime errors if types are mixed incorrectly.

L2.4: More on Variables, Operators, and Expressions

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

\' Single quote

\" Double quote

Example:

print("Hello\nWorld") # Output: Hello (newline) World


print("It\'s Python") # Output: It's Python

Types of Quotes
1. Single quotes ('text')
2. Double quotes ("text")
3. Triple quotes ('''text''' or """text""") for multi-line strings.

L2.6: String Methods

Common String Methods


1. .upper() – Converts string to uppercase.
2. .lower() – Converts string to lowercase.
3. .strip() – Removes leading/trailing whitespace.
4. .replace(old, new) – Replaces occurrences of a substring.
5. .split(separator) – Splits string into a list.

Examples:

s = " Hello World "


print(s.strip()) # Output: "Hello World"
print(s.upper()) # Output: " HELLO WORLD "
L2.7: An Interesting Cipher: More on Strings

String Manipulation for Ciphers


Strings can be encoded or decoded using algorithms like Caesar Cipher.
Example of shifting characters:

def caesar_cipher(text, shift):


result = ""
for char in text:
if char.isalpha():
shifted = chr(((ord(char) - ord('a') + shift) % 26) + ord('a'))
result += shifted
else:
result += char
return result

print(caesar_cipher("hello", 3)) # Output: khoor

L2.8: Introduction to the if Statement

Conditional Statements
Allow decision-making in programs based on conditions.
Syntax:

if condition:
# Code block executed if condition is True

Example:

age = int(input("Enter your age: "))


if age >= 18:
print("You are eligible to vote.")

L2.9: Tutorial on if, else, and elif Conditions

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:

marks = int(input("Enter your marks: "))


if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")

L2.10: Introduction to "Import Library"

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.11: Different Ways to Import a Library

Methods of Importing Libraries


1. Import Entire Library:
import math
print(math.pi)

2. Import Specific Functions/Classes:


from math import sqrt, pi
print(sqrt(16), pi)
3. Rename Imported Library (Alias):
import math as m
print(m.sqrt(16))

4. Import All Functions (*):


from math import *
print(sqrt(16), pi)

L2.12: Conclusion
Week 2 covers intermediate Python concepts such as dynamic typing, advanced string
manipulation, conditional statements, and importing libraries.

You might also like