week6
week6
Numbers
Arithmetic Operations
Basic operations:
a, b = 10, 5
print(a + b) # Addition: 15
print(a - b) # Subtraction: 5
print(a * b) # Multiplication: 50
print(a / b) # Division: 2.0
print(a % b) # Modulus: 0
print(a // b) # Floor Division: 2
print(a ** b) # Exponentiation: 100000
Strings
String Basics
Strings are sequences of characters enclosed in quotes (' ', " ").
Strings are immutable.
String Operations
1. Concatenation:
s1 = "Hello"
s2 = "World"
print(s1 + " " + s2) # Output: Hello World
2. Repetition:
print("Hi" * 3) # Output: HiHiHi
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 for Graded Assignments
Collections
Collections Overview
Python collections include data structures like lists, tuples, sets, and dictionaries.
Lists
Ordered, mutable collection of items.
Common methods:
my_list = [1, 2, 3]
my_list.append(4) # Adds an element at the end
my_list.remove(2) # Removes the first occurrence of an element
my_list.sort() # Sorts the list in ascending order
Tuples
Ordered, immutable collection of items.
Dictionaries
Key-value pairs for data storage.
lst = [1, 2, 2, 3, 3, 3]
print(count_frequency(lst))
Numbers Assignments:
Focus on mathematical operations like summation, factorial calculation, or number
manipulation.
Example:
Strings Assignments:
Work on string manipulation tasks such as reversing strings or analyzing character
frequencies.
Example:
Collections Assignments:
Perform operations like sorting lists, counting frequencies in dictionaries, or performing set
operations.
Example:
Let me know if you need help solving specific assignments or additional examples!
⁂