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

week6

Week 6 covers graded programming assignments focusing on Numbers, Strings, and Collections in Python. It includes detailed notes on numerical data types, arithmetic operations, string manipulation, and collection data structures like lists, tuples, sets, and dictionaries, along with examples for each topic. The document also provides guidance on approaching practice assignments related to these topics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

week6

Week 6 covers graded programming assignments focusing on Numbers, Strings, and Collections in Python. It includes detailed notes on numerical data types, arithmetic operations, string manipulation, and collection data structures like lists, tuples, sets, and dictionaries, along with examples for each topic. The document also provides guidance on approaching practice assignments related to these topics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Detailed Notes for Week 6 Topics

Week 6 focuses on graded programming assignments related to Numbers, Strings, and


Collections. Below are detailed notes and explanations for each topic, along with examples.

Numbers

Numerical Data Types


1. Integer (int):
Whole numbers (e.g., 5, -10).
2. Float (float):
Decimal numbers (e.g., 3.14, -2.5).
3. Complex Numbers (complex):
Numbers with a real and imaginary part (e.g., 3 + 4j).

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

Built-in Functions for Numbers


1. abs(x) – Returns the absolute value of x.
2. pow(x, y) – Returns x raised to the power of y.
3. round(x) – Rounds the number to the nearest integer.
4. max() and min() – Find maximum/minimum values in a sequence.
Examples for Graded Assignments

Numbers Assignment Example:

# Sum of squares of first n natural numbers


def sum_of_squares(n):
return sum([i**2 for i in range(1, n + 1)])

n = int(input("Enter a number: "))


print("Sum of squares:", sum_of_squares(n))

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

3. Indexing and Slicing:


s = "Python"
print(s[^0]) # Output: P (first character)
print(s[-1]) # Output: n (last character)
print(s[1:4]) # Output: yth (substring)

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

Strings Assignment Example:

# Reverse a string and check if it's a palindrome


def is_palindrome(s):
reversed_s = s[::-1]
return s == reversed_s

string = input("Enter a string: ")


if is_palindrome(string):
print(f"{string} is a palindrome")
else:
print(f"{string} is not a palindrome")

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.

my_tuple = (1, "Python", True)


print(my_tuple[^0]) # Access element by index
Sets
Unordered collection of unique items.

my_set = {1, 2, "Python"}


my_set.add(3) # Adds an element to the set
my_set.discard(1) # Removes an element if it exists

Dictionaries
Key-value pairs for data storage.

my_dict = {"name": "Alice", "age": 25}


print(my_dict["name"]) # Access value by key

# Add or update key-value pairs:


my_dict["city"] = "New York"

Examples for Graded Assignments

Collections Assignment Example:

# Count frequency of elements in a list using a dictionary


def count_frequency(lst):
freq_dict = {}
for item in lst:
freq_dict[item] = freq_dict.get(item, 0) + 1
return freq_dict

lst = [1, 2, 2, 3, 3, 3]
print(count_frequency(lst))

Practice Assignments Breakdown


Here’s how you can approach each graded assignment:

Numbers Assignments:
Focus on mathematical operations like summation, factorial calculation, or number
manipulation.
Example:

# Find factorial using recursion


def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)

n = int(input("Enter a number: "))


print(f"Factorial of {n} is {factorial(n)}")

Strings Assignments:
Work on string manipulation tasks such as reversing strings or analyzing character
frequencies.

Example:

# Count vowels in a string


def count_vowels(s):
vowels = "aeiouAEIOU"
count = sum(1 for char in s if char in vowels)
return count

string = input("Enter a string: ")


print("Number of vowels:", count_vowels(string))

Collections Assignments:
Perform operations like sorting lists, counting frequencies in dictionaries, or performing set
operations.

Example:

# Find intersection of two sets


set_a = {1, 2, 3}
set_b = {3, 4, 5}
intersection = set_a & set_b
print("Intersection:", intersection)

Let me know if you need help solving specific assignments or additional examples!

You might also like