Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Important Coding Functions Summary

Uploaded by

Duy Bui
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Important Coding Functions Summary

Uploaded by

Duy Bui
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Week 2-DataTypesVariables

1. Variable Declaration and Assignment:


- Variables are declared and assigned when you first assign a value using the equals sign
(=).
Example:
my_name = "Al"
x = 3.2

2. Input Function:
- The input() function assigns user input to a variable.
Example:
some_value = input("Enter some value: ")

3. Printing Values:
- The print() function outputs the value of a variable to the console.
Example:
print(x)

4. Deleting Variables:
- Use del() to delete a variable.
Example:
del(x)

5. Mathematical Operations:
- Basic operations: addition (+), subtraction (-), multiplication (*), division (/).
Example:
z = x + 2 * y - 12

6. Augmented Assignments:
- Shortcuts for applying operations to variables:
Example:
x += 5 # Same as x = x + 5
x *= 5 # Same as x = x * 5

7. Type Conversion Functions:


- int(): Converts a value to an integer.
Example: int("5.7") # Returns 5
- float(): Converts a value to a floating-point number.
Example: float("5.7") # Returns 5.7
- str(): Converts a value to a string.
Example: str(5.7) # Returns "5.7"

8. String Operations:
- Concatenation: Join strings using +.
Example: greeting = "Hello, " + "World!"
- Multiply strings: Repeat strings using *.
Example: dash_line = "-" * 5 # Output: "-----"

9. String Slicing:
- Access parts of a string using index notation.
Example:
example_string = "Example!"
first_char = example_string[0] # Output: 'E'
last_char = example_string[-1] # Output: '!'

Week3-DataStructures
1. List Functions and Methods:

- Lists in Python are ordered, mutable sequences of elements. They can contain elements of
different data types.

Basic list operations:


- list_name = [1, 2, 3, 4]

Accessing Elements:
- list_name[0]: Accesses the first element of the list.
Example:
first_element = list_name[0] # first_element = 1

Slicing:
- list_name[start:end]: Returns a new list containing elements from the start index to end-1.
Example:
sublist = list_name[1:3] # sublist = [2, 3]

Modifying Elements:
- list_name[2] = 10: Modifies the element at index 2.
Example:
list_name[2] = 10 # list_name becomes [1, 2, 10, 4]
Concatenation:
- list1 + list2: Concatenates two lists.
Example:
combined_list = [1, 2] + [3, 4] # combined_list = [1, 2, 3, 4]

Repetition:
- list1 * 3: Repeats the list elements three times.
Example:
repeated_list = [1, 2] * 3 # repeated_list = [1, 2, 1, 2, 1, 2]

List Methods:
- x.append(element): Adds the element to the end of the list.
Example:
list_name.append(5) # list_name = [1, 2, 10, 4, 5]

- x.pop(): Removes and returns the last element of the list.


Example:
last_element = list_name.pop() # list_name = [1, 2, 10, 4], last_element = 5

- x.insert(index, element): Inserts the element at the specified index.


Example:
list_name.insert(1, 8) # list_name = [1, 8, 2, 10, 4]

- x.remove(element): Removes the first occurrence of the element.


Example:
list_name.remove(8) # list_name = [1, 2, 10, 4]

- x.reverse(): Reverses the order of elements in the list.


Example:
list_name.reverse() # list_name = [4, 10, 2, 1]

- x.sort(): Sorts the list in ascending order.


Example:
list_name.sort() # list_name = [1, 2, 4, 10]

Other useful functions:


- len(list_name): Returns the number of elements in the list.
Example:
length = len(list_name) # length = 4

- sum(list_name): Returns the sum of all elements in the list (numeric).


Example:
total = sum([1, 2, 3]) # total = 6

2. Tuple Functions and Methods:

- Tuples are ordered, immutable sequences of elements. They cannot be modified after
creation.

Basic tuple operations:


- tuple_name = (1, 2, 3)

Accessing Elements:
- tuple_name[0]: Accesses the first element of the tuple.
Example:
first_element = tuple_name[0] # first_element = 1

Slicing:
- tuple_name[start:end]: Returns a new tuple containing elements from start index to end-1.
Example:
sub_tuple = tuple_name[1:3] # sub_tuple = (2, 3)

Concatenation:
- tuple1 + tuple2: Concatenates two tuples.
Example:
combined_tuple = (1, 2) + (3, 4) # combined_tuple = (1, 2, 3, 4)

Repetition:
- tuple1 * 2: Repeats the tuple elements twice.
Example:
repeated_tuple = (1, 2) * 2 # repeated_tuple = (1, 2, 1, 2)

Tuple Methods:
- x.index(element): Returns the index of the first occurrence of the element.
Example:
index = tuple_name.index(2) # index = 1

- x.count(element): Returns the number of occurrences of the element in the tuple.


Example:
count = tuple_name.count(2) # count = 1
3. Set Functions and Methods:

- Sets are unordered collections of unique elements. They do not allow duplicate values.

Basic set operations:


- set_name = {1, 2, 3, 4}

Adding Elements:
- set_name.add(element): Adds the element to the set.
Example:
set_name.add(5) # set_name = {1, 2, 3, 4, 5}

Removing Elements:
- set_name.remove(element): Removes the element from the set (raises KeyError if not
found).
Example:
set_name.remove(3) # set_name = {1, 2, 4, 5}

- set_name.discard(element): Removes the element if present, does nothing if not.


Example:
set_name.discard(6) # No error, set_name remains {1, 2, 4, 5}

Mathematical Operations on Sets:


- Union:
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # union_set = {1, 2, 3, 4, 5}

- Intersection:
Example:
intersection_set = set1 & set2 # intersection_set = {3}

- Difference:
Example:
difference_set = set1 - set2 # difference_set = {1, 2}

- Symmetric Difference:
Example:
sym_diff_set = set1 ^ set2 # sym_diff_set = {1, 2, 4, 5}

Other useful functions:


- len(set_name): Returns the number of elements in the set.
Example:
length = len(set_name) # length = 4

4. Dictionary Functions and Methods:

- Dictionaries are collections of key-value pairs, where keys are unique.

Basic dictionary operations:


- dict_name = {"name": "John", "age": 30}

Accessing Elements:
- dict_name["key"]: Accesses the value associated with the key.
Example:
name = dict_name["name"] # name = "John"

Modifying Elements:
- dict_name["key"] = value: Updates the value associated with the key.
Example:
dict_name["age"] = 31 # dict_name becomes {"name": "John", "age": 31}

Adding Key-Value Pairs:


- dict_name["new_key"] = value: Adds a new key-value pair.
Example:
dict_name["location"] = "New York" # {"name": "John", "age": 31, "location": "New York"}

Dictionary Methods:
- x.update({key: value}): Updates or adds key-value pairs to the dictionary.
Example:
dict_name.update({"age": 32}) # {"name": "John", "age": 32, "location": "New York"}

- x.keys(): Returns a view object containing the dictionary’s keys.


Example:
keys = dict_name.keys() # dict_keys(["name", "age", "location"])

- x.values(): Returns a view object containing the dictionary’s values.


Example:
values = dict_name.values() # dict_values(["John", 32, "New York"])

- x.items(): Returns a view object containing the dictionary’s key-value pairs as tuples.
Example:
items = dict_name.items() # dict_items([("name", "John"), ("age", 32), ("location", "New
York")])

Other useful functions:


- len(dict_name): Returns the number of key-value pairs in the dictionary.
Example:
length = len(dict_name) # length = 3

Week 4 ControlFlow
1. Comparison Operators:

- Comparison operators are used to compare values and return True or False.
- ==: equal to
- !=: not equal to
- >: greater than
- <: less than
- >=: greater than or equal to
- <=: less than or equal to
Example:
x=5
y=3
print(x > y) # True

2. Logical Operators:

- Logical operators combine comparison statements.


- and: returns True if both conditions are True.
- or: returns True if at least one condition is True.
- not: returns True if the condition is False.
Example:
x=2
y=5
print((x > 1) and (y < 7)) # True
print((y >= 5) and not (x == 3)) # True
3. Identity Operators:

- Identity operators check if two objects are the same object in memory.
- is: returns True if both operands are the same object.
- is not: returns True if both operands are not the same object.
Example:
a = [1, 2, 3]
b=a
print(a is b) # True, because they refer to the same object.

4. Membership Operators:

- Membership operators test if a value is in a sequence (like a list, tuple, or string).


- in: returns True if the value is found in the sequence.
- not in: returns True if the value is not found in the sequence.
Example:
x = [1, 2, 3]
print(1 in x) # True
print(4 not in x) # True

5. Conditional Statements (if, else, elif):

- if statements are used to conditionally execute code based on a test expression.


Example:
x = 10
if x > 5:
print("x is greater than 5")

- if...else statements are used when you have two possible outcomes.
Example:
x=3
if x % 2 == 0:
print("Even number")
else:
print("Odd number")

- if...elif...else statements are used for multiple conditions.


Example:
x = -1
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

6. The range() Function:

- The range() function generates a sequence of numbers.


Syntax:
range(start, stop, step)
Example:
for i in range(1, 10, 2):
print(i)
# Outputs: 1, 3, 5, 7, 9

7. match-case Statements:

- match-case is an alternative to if...elif...else, useful for matching multiple patterns.


Example:
value = 4
match value:
case 0:
print("Zero")
case 1 | 2 | 3:
print("One, Two, or Three")
case _:
print("Other")

Week 5-Looping
1. The range() Function:

- The range() function generates a sequence of numbers. It is typically used with loops.
Syntax:
range(start, stop, step)
- start: Starting number (default is 0)
- stop: End of the range (exclusive)
- step: Increment between each number (default is 1)
Example:
for i in range(1, 10, 2):
print(i)
# Outputs: 1, 3, 5, 7, 9
- The range function is commonly used in both for loops and while loops.

2. For Loops:

- A for loop iterates over a sequence (list, tuple, dictionary, set, or string) a fixed number of
times.
- The <holder> variable refers to the current element being accessed.
Syntax:
for <holder> in <sequence>:
<action block>
Example:
for elem in [1, 2, 3, 4]:
print(elem)
# Outputs: 1, 2, 3, 4
- You can also use the range function to loop through a sequence of numbers.
Example:
for i in range(5):
print("Hello")
# Outputs "Hello" five times.

- Nested for loops are used for multi-dimensional sequences (e.g., lists of lists).
Example:
x = [[1, 2, 3], [4, 5, 6]]
for out_list in x:
for elem in out_list:
print(elem)
# Outputs: 1, 2, 3, 4, 5, 6
3. While Loops:

- A while loop repeats as long as a condition is True.


Syntax:
while <condition>:
<action block>
- The loop runs as long as the condition remains True. If the condition becomes False, the
loop stops.
Example:
n=5
factorial = 1
i=1
while i < n:
i += 1
factorial *= i
print(f"{n}! = {factorial}")
# Outputs: 5! = 120

4. Loop Control Statements:

- Loop control statements modify the flow of loops:


- break: Terminates the loop and exits immediately.
- continue: Skips the current iteration and continues to the next.
- pass: Does nothing, acts as a placeholder.
Example:
for s in "Python":
if s == "h":
break
print(s)
# Outputs: P, y, t

Example with continue:


for s in "Python":
if s == "h":
continue
print(s)
# Outputs: P, y, t, o, n

Example with pass:


for i in range(5):
pass
print("Loop completed")
# Outputs: Loop completed

5. Do-While Loop Equivalent in Python:

- Python does not have a native do-while loop, but we can simulate it with a while loop.
Example:
i=0
while True:
i += 1
if i >= 1:
break
print(i)
# Outputs: 1

6. Infinite Loops:

- An infinite loop occurs when the condition in a while loop is always True and there is no
exit condition.
- To stop an infinite loop, use Control + C in Python.
Example of an infinite loop:
while True:
print("This will run forever")

7. Improving Efficiency with Loops:

- Loops help eliminate repetitive code by executing similar actions multiple times.
- They also reduce errors and make future changes easier.
- Use loops to gather user inputs, validate inputs, or process large amounts of data.
Example:
for _ in range(3):
score = input("Enter score: ")
# Process score and store it

Week 6:Functions
What are Functions?
Functions are reusable blocks of code designed to perform a specific task. They consist of a
function definition (block of code) and a function call (to execute the code). Functions
reduce redundancy, simplify code, and promote modular programming.

Structure of a Function
A function generally follows this format:
def function_name(arguments):
"""Optional docstring explaining the function."""
action_block
return value (optional)

Example 1: Simple Function without Arguments


This example demonstrates a simple function that prints a welcome message.

Code:

def greeting():

print("Welcome!")

greeting() # Calling the function

Output:
Welcome!

Example 2: Function with Arguments


This example demonstrates a function that takes two arguments, multiplies them, and
returns the result.

Code:

def multiply_values(val_one, val_two):

return val_one * val_two

result = multiply_values(4, 5)
print(result)
Output:
20

Explanation of Parameters and Return


In this example, val_one and val_two are the function’s parameters. These parameters
accept values when the function is called. The function multiplies the two values and uses
the return statement to send the result back to the caller.

Example 3: Function with a Docstring


Docstrings provide an explanation of what a function does. This is optional but
recommended for clarity.

Code:

def multiply_values(val_one, val_two):

"""This function returns the product of two values."""

return val_one * val_two

print(multiply_values.__doc__)

Output:
This function returns the product of two values.

Example 4: Using Arbitrary Arguments (*args)


This example shows how to pass a variable number of arguments to a function using *args.

Code:

def sum_values(*args):
return sum(args)
print(sum_values(1, 2, 3, 4))

Output:
10

Example 5: Using Arbitrary Keyword Arguments (**kwargs)


This example demonstrates the use of **kwargs for handling named arguments as key-value
pairs.

Code:
def print_kwargs(**kwargs):

return kwargs
print(print_kwargs(a=1, b=2, c=3))

Output:
{'a': 1, 'b': 2, 'c': 3}

Function Scoping
The scope of a variable determines where it can be accessed. Variables defined inside a
function are local to
that function and cannot be accessed outside it, whereas global variables are accessible
from any function.

Example 6: Local and Global Scope


Code:

a = 10 # Global variable
def print_value():

a = 5 # Local variable

print(a)

print_value()

print(a)

Output:
5
10

Functions
Lambda functions are small anonymous functions used for simple operations. They are
often used in higher-order functions.

Example 7: Lambda Function


Code:

add = lambda x, y: x + y

print(add(2, 3))
Output:
5

Example 8: Using map()


The map() function applies a function to all items in an input list and returns a new list with
the results.

Code:

numbers = [1, 2, 3, 4]

squares = list(map(lambda x: x ** 2, numbers))

print(squares)

Output:
[1, 4, 9, 16]

Example 9: Using filter()


The filter() function filters items based on a condition and returns a list of elements that
satisfy the condition.

Code:

numbers = [1, 2, 3, 4, 5, 6]

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)

Output:
[2, 4, 6]

Week 7-Modules
1. What are Modules?
Modules in Python refer to a file containing Python code, such as functions, classes, and
variables. Modular programming involves breaking down a large program into smaller,
manageable modules. This makes the code simpler, more maintainable, and reusable.
2. Modular Programming
Modular programming refers to breaking down a large task or application into smaller parts
called modules.
Each module contains specific code to perform part of the task, improving simplicity and
reusability.

Advantages:
- **Simplicity**: Focus on smaller parts of the problem.
- **Maintainability**: Modify a module without affecting other parts.
- **Reusability**: Share modules across projects.

3. Types of Modules in Python


There are two types of modules:

1. **Built-in Modules**: Modules that come with Python (standard library). Examples
include os, math, random, and datetime.
2. **Custom Modules**: Python files (.py) created by users that contain their custom code.

4. Importing Modules
You can import modules using the following ways:

- **Importing an entire module**: This makes all functions and variables in the module
available for use.
Example:
```python
import math
print(math.sqrt(16)) # Output: 4.0
```

- **Importing specific functions**: Only the specified functions are imported from the
module.
Example:
```python
from math import sqrt
print(sqrt(16)) # Output: 4.0
```

- **Using an alias**: Shorten the name of the module for easier reference.
Example:
```python
import math as m
print(m.sqrt(16)) # Output: 4.0
```

5. Common Built-in Modules

os Module
The os module provides functions to interact with the operating system. Some commonly
used functions include:

- **os.getcwd()**: Get the current working directory.


- **os.chdir(path)**: Change the current working directory.
- **os.mkdir(path)**: Create a new directory.
- **os.rmdir(path)**: Remove an existing directory.

Example:
```python
import os
os.chdir('/path/to/directory')
print(os.getcwd()) # Output: /path/to/directory
```

math Module
The math module provides mathematical functions beyond basic arithmetic, such as
trigonometric and logarithmic functions.

- **math.pi**: Value of π.
- **math.e**: Value of Euler’s number.
- **math.sin(x)**: Returns the sine of x (angle in radians).
- **math.sqrt(x)**: Returns the square root of x.

Example:
```python
import math
print(math.pi) # Output: 3.141592653589793
print(math.sqrt(16)) # Output: 4.0
```
random Module
The random module is used to generate random numbers and perform operations like
shuffling a sequence.

- **random.random()**: Returns a random float between 0.0 and 1.0.


- **random.randint(a, b)**: Returns a random integer between a and b.
- **random.choice(sequence)**: Returns a random element from the sequence.

Example:
```python
import random
print(random.randint(1, 10)) # Random integer between 1 and 10
print(random.choice([1, 2, 3])) # Randomly selects an element
```

datetime Module
The datetime module provides classes for working with dates and times.

- **datetime.date.today()**: Returns the current date.


- **datetime.datetime.now()**: Returns the current date and time.
- **strftime()**: Converts a date/time to a formatted string.

Example:
```python
import datetime
print(datetime.date.today()) # Output: current date
print(datetime.datetime.now()) # Output: current date and time

```

Comprehensive Date Formatting: datetime Module


The `strftime` method in the datetime module allows formatting date and time objects into
readable strings
using specific format codes. Below are the common format codes for date and time:

- **%a**: Abbreviated weekday name (e.g., 'Mon').


- **%A**: Full weekday name (e.g., 'Monday').
- **%b**: Abbreviated month name (e.g., 'Jan').
- **%B**: Full month name (e.g., 'January').
- **%d**: Day of the month as a zero-padded decimal (01 to 31).
- **%m**: Month as a zero-padded decimal (01 to 12).
- **%Y**: Year with century (e.g., '2024').
- **%H**: Hour in 24-hour format (00 to 23).
- **%I**: Hour in 12-hour format (01 to 12).
- **%p**: AM or PM.
- **%M**: Minute (00 to 59).
- **%S**: Second (00 to 59).
- **%f**: Microsecond (000000 to 999999).
- **%z**: UTC offset in the form ±HHMM.
- **%Z**: Time zone name.

Example of formatting a date using `strftime`:


```python
import datetime
now = datetime.datetime.now()
formatted_date = now.strftime('%A, %B %d, %Y')
print(formatted_date) # Output: Monday, October 21, 2024
```

You can also use `strptime` to parse strings into datetime objects based on these format
codes.

Example of parsing a string using `strptime`:


```python
date_string = '21-10-2024'
date_obj = datetime.datetime.strptime(date_string, '%d-%m-%Y')
print(date_obj) # Output: 2024-10-21 00:00:00

Detailed Explanation: os Module


The os module provides functions for interacting with the operating system.

- **os.getcwd()**: Returns the current working directory.


- **os.chdir(path)**: Changes the current working directory.
- **os.mkdir(path)**: Creates a new directory at the specified path.
- **os.rmdir(path)**: Removes the directory at the specified path.
- **os.listdir(path)**: Lists all files and directories in the specified path.
- **os.path.exists(path)**: Checks if the specified path exists.

Example:
```python
import os
os.mkdir('new_folder') # Creates a directory named 'new_folder'
print(os.listdir()) # Lists the content of the current directory
```

Detailed Explanation: math Module


The math module offers many mathematical functions:

- **math.sqrt(x)**: Returns the square root of x.


- **math.pow(x, y)**: Returns x raised to the power of y.
- **math.log(x, base)**: Returns the logarithm of x to the specified base.
- **math.sin(x)**: Returns the sine of x (x is in radians).
- **math.factorial(x)**: Returns the factorial of x.
- **math.pi**: Returns the value of pi (3.141592...).
- **math.e**: Returns the value of Euler's number (2.718...).

Example:
```python
import math
print(math.sqrt(16)) # Output: 4.0
print(math.pow(2, 3)) # Output: 8.0
print(math.log(10, 10)) # Output: 1.0
```

Detailed Explanation: random Module


The random module is useful for generating random numbers:

- **random.random()**: Returns a random float between 0 and 1.


- **random.randint(a, b)**: Returns a random integer between a and b.
- **random.randrange(a, b, step)**: Returns a random number in the specified range with
a step.
- **random.choice(sequence)**: Returns a random element from the sequence.
- **random.shuffle(sequence)**: Shuffles the elements in the sequence.

Example:
```python
import random
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list) # Output: Shuffled list
print(random.randint(10, 20)) # Output: Random integer between 10 and 20
```
Week 8-Packages

1. External Packages

Packages are collections of modules that provide tools for performing complex
programming tasks. Python has an extensive set of external packages that can be used for
tasks such as data analysis, machine learning, and scientific computing. These packages are
typically found in the Python Package Index (PyPI) and can be installed using package
managers like pip or conda.

2. Python Package Repository and Managers

To manage external packages, Python uses package managers like pip and conda.

- **pip**: The most common Python package manager used to install and manage
packages from PyPI.
Example:
```bash
pip install package_name --user
```

- **conda**: A modern package manager that can install and manage packages across
different systems.
Example:
```bash
conda install package_name
```

3. numpy Package

numpy (Numerical Python) is a fundamental package for scientific computing in Python. It


provides support for
large, multi-dimensional arrays and matrices, along with mathematical functions to
operate on these arrays.

- To install numpy:
```bash
pip install numpy
```
- Importing numpy:
```python
import numpy as np
```

4. n-dimensional Arrays

numpy arrays are collections of elements of the same type in n dimensions. They improve
performance over traditional
Python lists by storing data more efficiently.

Example:
```python
import numpy as np
array_2d = np.array([[1, 2], [3, 4]])
print(array_2d)
```

5. ndarray Class

The ndarray class is used to represent numpy arrays. Some of its attributes include:

- **ndim**: Number of dimensions (axes).


- **shape**: Size of the array in each dimension.
- **size**: Total number of elements.
- **dtype**: Data type of the elements.
- **itemsize**: Size of each element in bytes.

Example:
```python
numpy_array = np.array([1, 2, 3, 4])
print(numpy_array.shape) # Output: (4,)
```

6. Creating numpy Arrays

There are several ways to create numpy arrays:

- **np.array()**: Converts a Python list into an array.


- **np.zeros()**: Creates an array filled with zeros.
- **np.ones()**: Creates an array filled with ones.
- **np.arange()**: Creates an array with a range of values.
- **np.linspace()**: Creates an array with evenly spaced values.
Example:
```python
array_zeros = np.zeros((2, 3))
array_range = np.arange(0, 10, 2)
```

7. pandas Package

pandas is a powerful data analysis and manipulation package built on top of numpy. It
provides data structures like Series
and DataFrames for handling large datasets, including tabular data.

- To install pandas:
```bash
pip install pandas
```
- Importing pandas:
```python
import pandas as pd
```

8. pandas Data Structures

pandas provides two primary data structures:

- **Series**: A one-dimensional labeled array.


- **DataFrame**: A two-dimensional, tabular data structure.

Example:
```python
import pandas as pd
series_example = pd.Series([1, 2, 3, 4])
dataframe_example = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
```

9. Exploring DataFrames

Once a DataFrame is created, it can be explored using various functions:

- **head()**: View the first few rows.


- **tail()**: View the last few rows.
- **describe()**: Get summary statistics.
- **columns**: List the column names.

Example:
```python
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
print(df.head())
print(df.describe())
```

Week 9: Sorting and Aggregation in


Pandas
1. Sorting
Sorting helps in understanding data by determining the order of elements.
The .sort_values() function sorts a DataFrame by specific attributes in ascending or
descending order.

Function: the_df.sort_values(by, ascending=True)


- "by": column(s) to sort by (string or list)
- "ascending": sort order (True for ascending, False for descending)

Example:
the_df = the_df.sort_values("Name", ascending=False) # sorts by "Name" in descending
order

2. Grouping and Aggregation


Grouping allows for categorizing data based on attributes. Aggregation summarizes groups
with statistical functions.

Function: the_df.groupby(by).agg(func)
- "by": column(s) to group by (string or list)
- "func": function to apply to grouped data (e.g., sum, count)

Example:
the_df = the_df.groupby("category").sum() # groups by "category" and sums numerical
columns

3. Deriving Data
Deriving data creates new columns by performing operations on existing ones.
Example:
the_df["NewCol"] = the_df["ColA"] + the_df["ColB"] * 0.1 # derives a new column

4. Data Type Conversion


Data types are often converted for compatibility using the .astype() function.

Function: the_df[col].astype(dtype)
- "col": column to convert
- "dtype": target data type (e.g., int, float, string)

Example:
the_df["Amount"] = the_df["Amount"].astype(float) # converts "Amount" to float

5. Mapping Functions
Apply functions row-wise, column-wise, or element-wise to modify data.

Functions:
- apply(): row/column-wise
- applymap(): element-wise across DataFrame
- map(): element-wise across Series

Examples:
the_df.apply(lambda x: x * 2) # doubles each element row/column-wise
the_df['Col'].map(lambda x: x.lower()) # converts strings to lowercase in a specific column

6. Renaming and Filtering Columns


Renaming and filtering are useful for clarity and data reduction.

Function for renaming: the_df.rename(columns={old_name: new_name})


Filtering columns: the_df = the_df[['keep_column1', 'keep_column2']]

Example:
the_df = the_df.rename(columns={"old_col": "new_col"}) # renames a column
the_df = the_df[["col1", "col2"]] # filters the DataFrame to keep only certain columns

7. Pivoting and Shaping Data


Pivoting and reshaping rearranges data based on unique values or structure needs.

Function:
- pivot(): reshapes based on unique values in columns
- melt(): converts wide data to long format
Example:
the_df.pivot(index="ID", columns="category", values="value") # pivots to show categories
as columns

8. Combining Data
DataFrames can be merged using functions like concat(), join(), and merge().

Function:
- concat(): joins DataFrames along a particular axis
- merge(): SQL-style joins on common columns

Example:
result_df = pandas.concat([df1, df2], axis=1) # concatenates along columns

9. Text Data Manipulation


String functions can be applied to manipulate text data.

Function: the_df[col].str.function()
- Functions: len(), lower(), upper(), replace()

Example:
the_df["Name"] = the_df["Name"].str.upper() # converts names to uppercase

10. Time Series Data


Time-based data is handled with the datetime64 data type and accessor functions.

Function:
- pd.to_datetime(): converts strings to datetime objects
- dt accessor: extracts components (e.g., year, month, day)

Example:
the_df["date"] = pd.to_datetime(the_df["date"]) # converts string to datetime
the_df["year"] = the_df["date"].dt.year # extracts the year component

Week 10: Detailed Code for Data


Visualisation with Python
1. Single Line Plot Example

import matplotlib.pyplot as plt

x = list(range(10))
y = list(range(0, 20, 2))
z = list(range(20, 40, 2))

fig = plt.figure(figsize=(3.5, 2.5)) # Create figure


ax = fig.subplots() # Add subplot to the figure

# Plot two datasets on the same axes


ax.plot(x, y, 'r', label='set_1') # Red line for first set
ax.plot(x, z, 'b', label='set_2') # Blue line for second set

# Set title and axis labels


ax.set_title("Number")
ax.set_xlabel("X")
ax.set_ylabel("Y")

# Set limits for X and Y axes


ax.set_xlim((0, 12))
ax.set_ylim((0, 50))

# Add legend
ax.legend()

# Show the plot


plt.show()

2. Dual Plot Example

import matplotlib.pyplot as plt

x = list(range(10))
y = list(range(0, 20, 2))
z = list(range(20, 40, 2))

fig = plt.figure(figsize=(3.5, 2.5)) # Create figure


ax = fig.subplots(2, 1, sharex=True) # Create two subplots sharing the x-axis
# Plot in the first subplot
ax[0].plot(x, y, 'r--', label='Y') # Dashed red line
ax[0].set_title("set_1") # Set title
ax[0].set_xlim((0, 12)) # Set limits
ax[0].set_ylim((0, 25)) # Set y-limits
ax[0].legend()

# Plot in the second subplot


ax[1].plot(x, z, 'ob', label='Z') # Blue circle markers
ax[1].set_title("set_2") # Set title
ax[1].set_xlim((0, 12)) # Same x-axis limits
ax[1].set_ylim((0, 50)) # Set y-limits
ax[1].legend()

# Adjust layout to avoid overlap


plt.tight_layout()
plt.show()

3. Scatter Plot

import numpy as np
import matplotlib.pyplot as plt

x = list(range(100))
y = list(range(100)) + 50 * np.random.random(100) # Random values added

fig = plt.figure(figsize=(5, 5))


ax = fig.subplots()

# Create a scatter plot


ax.scatter(x, y, color='r')

# Set axis labels


ax.set_xlabel("X")
ax.set_ylabel("Y")

# Show plot
plt.show()
4. Bar Chart

import matplotlib.pyplot as plt

x = ["a", "b", "c", "d"]


y = [20, 4, 10, 15]

fig = plt.figure(figsize=(5, 5))


ax = fig.subplots()

# Create a bar chart


ax.bar(x, y, label="Score")

# Set axis labels and legend


ax.set_xlabel("Code")
ax.set_ylabel("Value")
ax.legend()

plt.tight_layout()
plt.show()

5. Pie Chart

import matplotlib.pyplot as plt

x = [2, 4, 10, 15]


labels = ["x1", "x2", "x3", "x4"]

fig = plt.figure(figsize=(5, 5))


ax = fig.subplots()

# Create a pie chart


ax.pie(x, labels=labels, autopct="%.1f%%")

ax.legend()

plt.tight_layout()
plt.show()
6. Histogram

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(1000) # Random normal data

fig = plt.figure(figsize=(5, 5))


ax = fig.subplots()

# Create a histogram
ax.hist(x, label="Random Data", color="r")

# Set axis labels and legend


ax.set_xlabel("Value")
ax.set_ylabel("Frequency")
ax.legend()

plt.tight_layout()
plt.show()

7. Pandas Plotting Example

import pandas as pd
import matplotlib.pyplot as plt

# Create a DataFrame
df = pd.DataFrame({
'length': [1.5, 0.5, 1.2, 0.9, 3],
'width': [0.7, 0.2, 0.15, 0.2, 1.1]
}, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

# Plot DataFrame
df.plot(title="DataFrame Plot")
plt.show()

8. Pandas Series Plotting Example

import pandas as pd
import matplotlib.pyplot as plt

# Create a Series
srs = pd.Series([1, 2, 3, 3])

# Plot the Series as a histogram


srs.plot(kind='hist', title="Series Plot")
plt.show()

Lecture 11: File Input and Output


1. Introduction to File I/O
When handling more data than can be processed through screen input/output, or when
interacting with other systems, we use files. Python's built-in `open()` function creates file
objects that allow reading and writing operations.

File objects are created using:

the_ptr = open(file_name, 'mode')

The optional third argument 'buffer' is used for handling large files.

2. Types of Files
Files are categorized into two types:

• Text files: Plain text or specialized text-based data (CSV, JSON).

• Binary files: Files containing binary data (images, videos, etc.).

Regardless of file type, the process is the same: Open the file, process it (read/write), and
close the file.

3. File Object Methods


Python provides several methods to operate on files:

• the_ptr = open(filename, 'mode'): Opens the file with the specified mode (read, write, etc.).

• the_ptr.read(size): Reads a specified number of bytes from the file.

• the_ptr.readline(): Reads the next line from the file.

• the_ptr.readlines(): Reads the entire file line-by-line as a list.


• the_ptr.write(line_str): Writes the string to the file.

• the_ptr.close(): Closes the file when done, saving changes.

4. Text Files – Access Modes


Access modes specify how a file will be handled:

• 'r': Opens a file for reading.

• 'w': Opens a file for writing (overwriting existing content).

• 'a': Opens a file for appending new data to the end.

5. Reading Example
file_ptr = open("example.txt", "r")
the_lines = file_ptr.readlines()
print(the_lines)
file_ptr.close()

for each_line in the_lines:


print(each_line.strip()) # Removes newline characters

Explanation: This code reads the file into a list and prints each line after stripping newline
characters using `strip()` method.

6. Writing Example
file_ptr = open("output.txt", "w")
file_ptr.write("I create this file using Python.")
file_ptr.write("I write headers and values.")
file_ptr.write("")
file_ptr.write("Name Age Number")
file_ptr.write("Jim 16 2090943")
file_ptr.close()

Explanation: This example demonstrates writing to a file. Escape sequences like '\n' are
used for newlines, and '\t' for tabs.

7. Binary Files – Access Modes


Binary files are accessed in the same way as text files, but with 'b' added to the mode:
• 'rb': Opens a binary file for reading.

• 'wb': Opens a binary file for writing (overwriting content).

• 'ab': Opens a binary file for appending.

8. Binary Files Example


file_ptr = open("example.bin", "wb")
the_list = list(range(12))
list_bin = bytearray(the_list)
file_ptr.write(list_bin)
file_ptr.close()

file_ptr = open("example.bin", "rb")


the_list = list(file_ptr.read())
print(the_list)
file_ptr.close()

Explanation: Binary data is written to and read from a binary file. A list is converted to a
bytearray before writing to the file.

9. CSV Files
CSV (Comma Separated Values) files are text files with a structured format for tabular data,
where rows are separated by newlines and columns by commas.

10. CSV Reading Example


import csv

file_ptr = open("example.csv", "r")


csv_reader = csv.reader(file_ptr)

for each_row in csv_reader:


print(each_row)

file_ptr.close()

Explanation: The `csv.reader()` function reads the contents of a CSV file row by row,
returning each row as a list.
11. CSV Writing Example
import csv

file_ptr = open("example.csv", "w")


csv_writer = csv.writer(file_ptr)

data = [["Name", "Age"], ["Jim", 16], ["Helen", 34]]


for each_row in data:
csv_writer.writerow(each_row)

file_ptr.close()

Explanation: The `csv.writer()` function writes rows of data to a CSV file, where each row is
a list.

12. JSON Files


JSON (JavaScript Object Notation) is a text-based format used to store and transmit key-
value data, similar to Python dictionaries.

13. JSON Example


import json

# JSON string
json_data = """
{
"number": 118,
"name": "Jim Smith",
"age": 42,
"city": ["Perth", "Melbourne"],
"Status": null
}
"""

# Convert JSON string to a dictionary


the_dict = json.loads(json_data)
print(the_dict)

# Modify and write the dictionary back to a JSON file


the_dict["Status"] = True
the_dict["Postcode"] = [6000, 3000]
with open("example.json", "w") as json_file:
json.dump(the_dict, json_file)

Explanation: This code converts a JSON string into a Python dictionary using `json.loads()`.
The dictionary is then modified and written back to a JSON file using `json.dump()`.

14. Excel Files


Excel files (.xlsx) can be read and written using the `pandas` library or `openpyxl` module.
Pandas provides an easy-to-use interface for working with tabular data.

15. Excel Example with Pandas


import pandas as pd

# Read from an Excel file


df = pd.read_excel("example.xlsx")

# Write to an Excel file


df.to_excel("output.xlsx", index=False)

Explanation: This example reads an Excel file into a Pandas DataFrame using
`pandas.read_excel()` and writes it back to another Excel file using `to_excel()`.

16. CSV DictReader Example


The `csv.DictReader` function reads a CSV file and maps each row into a Python dictionary
where the keys are taken from the first row (header). This is especially useful when
working with CSV files that have column headers, allowing easy access to values by their
field names.

import csv

# Open the CSV file in read mode


file_ptr = open("example.csv", "r")
csv_reader = csv.DictReader(file_ptr)

# Iterate over each row and print it as a dictionary


for each_row in csv_reader:
print(each_row)

file_ptr.close()
Explanation: In this example, the `csv.DictReader()` reads the CSV file row by row. Each row
is returned as an OrderedDict, where the keys correspond to the column headers. The `for`
loop iterates over each row, allowing you to access the data by field names (headers). This
method is useful for structured CSV files where each column represents a specific attribute,
such as 'Name', 'Age', and 'Score'.

Key points:

 • The first row of the CSV file must contain the headers (field names).
 • The values in the dictionary can be accessed by their respective header names (e.g.,
`each_row['Name']`).
 • This method simplifies accessing specific fields in CSV rows without needing to
manage index positions manually.

Week 12-Oriented Programming


1. Introduction to Object Orientation
Object-oriented programming (OOP) is a programming paradigm that focuses on modeling
real-world entities as 'objects'. Each object is an instance of a class, which encapsulates both
data (attributes) and behaviors (methods). The state of an object refers to its attributes,
while the methods define its behavior.

2. Key Features of OOP


OOP introduces several key features that make it a powerful paradigm for complex
problem-solving:

 • **Encapsulation**: Hiding internal details by exposing only a public interface


(methods) for interacting with an object's data (attributes).
 • **Inheritance**: Allows a new class (subclass) to inherit properties and behaviors
from an existing class (superclass). This promotes code reuse and reduces redundancy.
 • **Polymorphism**: The ability of an object to take on different forms, either by
method overloading (same method name with different parameters) or subclassing
(different implementation in child classes).

• **Abstraction**: Simplifies complex logic by hiding unnecessary details and showing only
essential features.

3. Creating a Class
A class serves as a blueprint for creating objects. It defines the structure (attributes and
methods) that will be shared by all objects created from that class.
Example:
class Pet:
"""A simple class to represent a pet."""

def __init__(self, species, breed):


self.species = species
self.breed = breed

Explanation: In this example, we define a `Pet` class with two attributes: `species` and
`breed`. The `__init__` method is called when an object is instantiated and is used to initialize
the object's attributes.

4. Instantiating an Object
Once a class is defined, you can create instances (objects) of that class by calling it like a
function.

Example:
my_pet = Pet("Dog", "Pug")

Explanation: Here, `my_pet` is an instance of the `Pet` class, and it represents a dog of the
breed 'Pug'.

5. Object Methods
Methods are functions defined within a class that operate on instances of the class. They can
access and modify an object's attributes.

Example:
class Student:
def __init__(self, first_name, last_name, student_number):
self.first_name = first_name
self.last_name = last_name
self.student_number = student_number

def full_name(self):
return f"{self.first_name} {self.last_name}"

Explanation: In this example, the `full_name()` method combines the `first_name` and
`last_name` attributes and returns the full name of the student.
6. Accessor Methods (Getters)
Accessor methods, or getters, are used to retrieve the value of an attribute while
maintaining encapsulation.

Example:
def get_first_name(self):
return self.first_name

Explanation: This accessor method returns the value of the `first_name` attribute.

7. Mutator Methods (Setters)


Mutator methods, or setters, are used to modify the value of an attribute. They help enforce
encapsulation by controlling how attributes are modified.

Example:
def set_first_name(self, first_name):
self.first_name = first_name

Explanation: This mutator method allows the `first_name` attribute to be updated.

8. Static Methods
Static methods do not operate on class attributes or require an instance of the class. They
are typically used as utility or helper functions within a class.

Example:
@staticmethod
def greeting():
return "Hello, world!"

Explanation: The `greeting()` method is defined as a static method using the


`@staticmethod` decorator. It does not access any instance-specific data.

9. Inheritance
Inheritance allows a class to inherit attributes and methods from another class. This
promotes code reuse by creating specialized subclasses from more general classes.

Example:
class Programmer(Student):
def __init__(self, first_name, last_name, student_number, language):
super().__init__(first_name, last_name, student_number)
self.language = language

def __str__(self):
return f"{self.first_name} {self.last_name} ({self.language})"

Explanation: In this example, `Programmer` is a subclass of `Student`. It inherits the


attributes and methods from `Student` and adds a new attribute, `language`.

10. Aggregation of Classes


Aggregation occurs when an object of one class contains objects from another class as part
of its attributes.

Example:
class Lecture:
def __init__(self, students, lecture_name):
self.students = students
self.lecture_name = lecture_name

def number_of_students(self):
return len(self.students)

Explanation: In this example, the `Lecture` class contains a list of `Student` objects as one of
its attributes, demonstrating aggregation.

You might also like