Tutorial 8
Tutorial 8
1. Exercise 3 Feedback
1. Quizizz!
List Comprehension
Purpose: Simplifies code for creating new lists by applying an expression to each element in an iterable.
& this?
Best Practices
• Functions should do one thing and do it well.
• Use descriptive names for functions and parameters.
• Keep functions short and readable.
Functions Cheat Sheet Creating Functions
• Syntax: Use the def keyword, followed by the function name and
Basic Function Structure parentheses ().
• Parameters: Optional variables passed inside the parentheses.
def function_name(parameters): They receive the values (arguments) passed into the function.
# Function body
return result • Body: The code block within the function that performs the task.
• Return Statement: Optionally returns a value to the caller.
Types of Functions
• Regular Functions: Defined using def. Calling Functions
• Anonymous Functions (Lambdas): Short, one-line • Call a function by its name followed by parentheses. Include
functions defined with the lambda keyword. arguments if the function requires them.
Scope
• Local Scope: Variables created inside a function. function_name(arguments)
• Global Scope: Variables defined outside any
function.
Return Value
Built-in Functions
• Common examples: print(), len(), range(), type(), • Use return to send a value back from the function. If return is not
input() used, the function returns None.
Functions: POLYMORPHISM
Polymorphism in programming refers to the ability of a function to process objects differently based on their
class or data type. The term "polymorphism" comes from Greek, meaning "many forms." In simple terms, it
allows functions to use entities of different types at different times.
def greet(input):
if isinstance(input, str):
# If the input is a string, greet by name
return f"Hello, {input}!"
elif isinstance(input, int) and input > 0: This function handles
# If the input is an integer, repeat the greeting cases where the input
return "Hello! " * input is a string or a
else: positive integer!
return "Invalid input!" That's polymorphism.
See! IT'S GREEK.
# Example
print(greet("Alice")) # Output: Hello, Alice!
print(greet(3)) # Output: Hello! Hello! Hello!
print(greet(-1)) # Output: Invalid input!
Build_A_Function
Objective: Fill in the missing parts of the Python function below to make it
work correctly.
Options:
def concatenate_strings(text, times):
• range
result = ""
for i in range(times): • +=
result += text • concatenate_strings
return result • result
https://realpython.com/lessons/navigate-namespaces-oveview/
NAMESPACES: Variable Scope
• Local Scope: if you use a variable (like x)
inside a function, Python first checks if it's
defined within that function.
• Global Scope: if the variable isn't in the
function, Python then looks for it outside the
function, in the overall file or program.
What is NLTK?
• A leading platform for building Python programs to work with human
language data.
• Comprehensive library with easy-to-use interfaces.
Key Features
• Text Processing Libraries
• Extensive collection of text processing libraries for
classification, tokenization, stemming, tagging, parsing, and
semantic reasoning.
• Corpora and Resources
• Includes a rich set of diverse text corpora and lexical
resources.
https://www.codecademy.com/learn/dscp-natural-language-processing/modules/dscp-text-preprocessing/cheatsheet
SpaCy
Key Features:
• Efficient Text Processing: Fast and streamlined
What is SpaCy?
handling of large volumes of text.
• Open-Source NLP Library: Advanced and efficient
for processing text in Python. • Pre-Trained Statistical Models: Supports multiple
languages for various NLP tasks.
• Real-World Use: Ideal for information extraction and • Named Entity Recognition: Powerful tool for identifying
natural language understanding tasks entities like names and places.
• Part-of-Speech Tagging: Accurate grammatical tagging
of words in a sentence.
• Dependency Parsing: Building dependency trees to
understand sentence structure.
• Extensibility and Customization: Easy to customize and
extend for specific needs.
• Integration with Python Ecosystem: Seamlessly works
with other Python libraries.
https://spacy.io/usage/spacy-101
JSON
JSON (JavaScript Object Notation) is a lightweight,
plain text format used for data interchange
between computers, and is language-independent.
Usage
• Text-based: JSON's text format is ideal for annotating and
storing linguistic data, including NER and sentiment analysis
results. {
• Lightweight: Its minimal data size makes JSON practical for "tokenized_text": ["I", "am", "learning", "NLP"],
"parsed_text": [
processing and sharing large NLP datasets.
{"token": "I", "lemma": "I", "pos": "PRON"},
{"token": "am", "lemma": "be", "pos": "AUX"},
{"token": "learning", "lemma": "learn", "pos": "VERB"},
JSON's structure is composed of two universally understood {"token": "NLP", "lemma": "NLP", "pos": "PROPN"}
data structures: ]
}
• Objects: JSON uses key-value pairs (like dictionaries!) for
easy readability and alignment with common coding patterns.
• Arrays: JSON arrays (like lists) allow for natural indexing and
iteration, simplifying data manipulation.
How to create a dictionary and convert it to JSON