Introduction to Python Data Types
Think of data types in Python as different containers specifically designed to hold different kinds of information. Just as you wouldn't store soup in a colander or try to carry water in a shopping bag with holes, each Python data type has been carefully crafted to store and operate on specific kinds of data.
In this tutorial, we'll explore how to solve real problems with Python's core data types:
- Strings - Text data (like names, addresses, or paragraphs)
- Numbers - Integers and floating-point values
- Lists - Ordered collections of items
- Dictionaries - Key-value pairs for efficient lookups
- Booleans - True/False values for decision making
Each section will include practical examples with complete code explanations, real-world applications, and exercises to deepen your understanding.
Project Files Structure
For this tutorial, we recommend creating the following folder structure:
python_basics/ |-- string_scripts/ | |-- name_formatter.py | |-- text_analyzer.py |-- number_scripts/ | |-- temperature_converter.py | |-- simple_calculator.py |-- list_scripts/ | |-- shopping_list.py | |-- grade_analyzer.py |-- dictionary_scripts/ | |-- contact_manager.py | |-- word_counter.py |-- boolean_scripts/ | |-- eligibility_checker.py | |-- password_validator.py
Working with Strings
Strings are Python's way of representing text. Think of them as a sequence of characters that flow together like beads on a string.
Example One: Name Formatter
Imagine you're building a form for a website. When users enter their names in different formats, you want to standardize how they appear in your database.
File location: python_basics/string_scripts/name_formatter.py
def format_name(first_name, last_name):
"""
Takes a first and last name, formats them properly with
capitalization and returns the full name.
Args:
first_name (str): The person's first name
last_name (str): The person's last name
Returns:
str: Properly formatted full name
"""
# Strip any leading/trailing whitespace and capitalize
formatted_first = first_name.strip().capitalize()
formatted_last = last_name.strip().capitalize()
# Join with a space
full_name = f"{formatted_first} {formatted_last}"
return full_name
# Test cases
print(format_name("john", "SMITH")) # John Smith
print(format_name(" mary ", "jones")) # Mary Jones
# Interactive usage
if __name__ == "__main__":
first = input("Enter your first name: ")
last = input("Enter your last name: ")
print(f"Formatted name: {format_name(first, last)}")
Code Explanation
This script demonstrates several string operations:
strip()- Removes any whitespace at the beginning or endcapitalize()- Makes the first letter uppercase and the rest lowercasef-strings- A modern way to format strings in Python (available in Python 3.6+)
Think of strip() like a gardener trimming the edges of a hedge, while capitalize() is like a teacher correcting case in a student's writing.
Real-World Application
This type of string processing is essential in:
- User registration systems
- Customer relationship management (CRM) software
- Database normalization
- Form processing on websites
Example Two: Text Analyzer
Let's build a more complex tool that analyzes a block of text, counting words, sentences, and providing statistics.
File location: python_basics/string_scripts/text_analyzer.py
def analyze_text(text):
"""
Analyzes a text sample and returns statistics.
Args:
text (str): The text to analyze
Returns:
dict: Dictionary containing text statistics
"""
# Handle empty text
if not text:
return {"error": "No text provided"}
# Count characters (excluding whitespace)
char_count = len(text.replace(" ", ""))
# Count words (simple split by spaces)
words = text.split()
word_count = len(words)
# Count sentences (split by .!?)
# This is a simplified approach
sentence_count = len([s for s in text.replace("!", ".").replace("?", ".").split(".") if s.strip()])
# Find the longest word
longest_word = max(words, key=len) if words else ""
# Calculate average word length
avg_word_length = char_count / word_count if word_count > 0 else 0
return {
"character_count": char_count,
"word_count": word_count,
"sentence_count": sentence_count,
"longest_word": longest_word,
"avg_word_length": round(avg_word_length, 2)
}
# Example usage
sample_text = """Python is an amazing programming language. It's versatile,
readable, and powerful! Would you like to learn it?"""
results = analyze_text(sample_text)
print("Text Analysis Results:")
for key, value in results.items():
print(f"{key}: {value}")
# Interactive usage
if __name__ == "__main__":
user_text = input("Enter text to analyze: ")
user_results = analyze_text(user_text)
print("\nAnalysis Results:")
for key, value in user_results.items():
print(f"{key}: {value}")
Code Explanation
This script showcases more advanced string manipulation techniques:
split()- Separates text into a list based on whitespace or specified delimiterreplace()- Substitutes specified characters- List comprehension - A concise way to create lists
max()with a key function - Finding an element with a specific property
Think of text analysis like a literary detective examining evidence - counting clues, measuring sentence lengths, and finding patterns that reveal the writing style.
Real-World Applications
- Content management systems
- SEO tools that analyze readability
- Educational software that evaluates writing
- Sentiment analysis in social media monitoring
Extension Ideas
This analyzer could be expanded to include:
- Readability scoring (Flesch-Kincaid, etc.)
- Common word detection
- Sentiment analysis (positive/negative tone)
Working with Numbers
Python has two main numeric types: integers (whole numbers) and floats (decimal numbers). Think of integers as steps on a staircase (discrete jumps) and floats as positions on a ramp (continuous values).
Example One: Temperature Converter
A practical application where numerical calculations are essential.
File location: python_basics/number_scripts/temperature_converter.py
def celsius_to_fahrenheit(celsius):
"""
Convert Celsius temperature to Fahrenheit.
Args:
celsius (float): Temperature in Celsius
Returns:
float: Temperature in Fahrenheit
"""
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
"""
Convert Fahrenheit temperature to Celsius.
Args:
fahrenheit (float): Temperature in Fahrenheit
Returns:
float: Temperature in Celsius
"""
return (fahrenheit - 32) * 5/9
def kelvin_to_celsius(kelvin):
"""
Convert Kelvin temperature to Celsius.
Args:
kelvin (float): Temperature in Kelvin
Returns:
float: Temperature in Celsius
"""
return kelvin - 273.15
def celsius_to_kelvin(celsius):
"""
Convert Celsius temperature to Kelvin.
Args:
celsius (float): Temperature in Celsius
Returns:
float: Temperature in Kelvin
"""
return celsius + 273.15
# Example usage
celsius_temp = 25
fahrenheit_temp = celsius_to_fahrenheit(celsius_temp)
print(f"{celsius_temp}°C is equal to {fahrenheit_temp:.1f}°F")
fahrenheit_temp = 98.6
celsius_temp = fahrenheit_to_celsius(fahrenheit_temp)
print(f"{fahrenheit_temp}°F is equal to {celsius_temp:.1f}°C")
kelvin_temp = 300
celsius_temp = kelvin_to_celsius(kelvin_temp)
print(f"{kelvin_temp}K is equal to {celsius_temp:.1f}°C")
# Interactive converter
if __name__ == "__main__":
print("\nTemperature Converter")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Kelvin to Celsius")
print("4. Celsius to Kelvin")
choice = input("Enter your choice (1-4): ")
try:
if choice == "1":
temp = float(input("Enter temperature in Celsius: "))
print(f"{temp}°C = {celsius_to_fahrenheit(temp):.1f}°F")
elif choice == "2":
temp = float(input("Enter temperature in Fahrenheit: "))
print(f"{temp}°F = {fahrenheit_to_celsius(temp):.1f}°C")
elif choice == "3":
temp = float(input("Enter temperature in Kelvin: "))
print(f"{temp}K = {kelvin_to_celsius(temp):.1f}°C")
elif choice == "4":
temp = float(input("Enter temperature in Celsius: "))
print(f"{temp}°C = {celsius_to_kelvin(temp):.1f}K")
else:
print("Invalid choice")
except ValueError:
print("Please enter a valid number for temperature")
Code Explanation
This script demonstrates:
- Mathematical operations with numbers
- Floating-point arithmetic
- Format specifiers in f-strings (
:.1ffor rounding to 1 decimal place) - Exception handling with
try/exceptto validate numeric input
Think of these conversion functions as translation dictionaries between different temperature "languages." Each temperature scale has its own way of describing the same physical reality.
Real-World Applications
- Weather applications
- Scientific research tools
- Cooking and recipe converters
- Industrial monitoring systems
Example Two: Simple Calculator
Let's build a basic calculator that handles multiple operations and demonstrates more complex numeric processing.
File location: python_basics/number_scripts/simple_calculator.py
def add(x, y):
"""Add two numbers"""
return x + y
def subtract(x, y):
"""Subtract y from x"""
return x - y
def multiply(x, y):
"""Multiply two numbers"""
return x * y
def divide(x, y):
"""Divide x by y"""
if y == 0:
return "Error: Division by zero"
return x / y
def power(x, y):
"""Calculate x raised to power y"""
return x ** y
def modulo(x, y):
"""Calculate remainder when x is divided by y"""
if y == 0:
return "Error: Modulo by zero"
return x % y
def calculate(x, y, operation):
"""
Perform a calculation based on the operation specified.
Args:
x (float): First number
y (float): Second number
operation (str): Operation to perform (add, subtract, multiply, divide, power, modulo)
Returns:
float or str: Result of calculation or error message
"""
operations = {
"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide,
"power": power,
"modulo": modulo
}
if operation not in operations:
return f"Error: Unknown operation '{operation}'"
return operations[operation](x, y)
# Example usage
print(calculate(10, 5, "add")) # 15.0
print(calculate(10, 5, "subtract")) # 5.0
print(calculate(10, 5, "multiply")) # 50.0
print(calculate(10, 5, "divide")) # 2.0
print(calculate(10, 0, "divide")) # Error message
print(calculate(2, 3, "power")) # 8.0
print(calculate(10, 3, "modulo")) # 1.0
# Interactive calculator
if __name__ == "__main__":
print("\nSimple Calculator")
print("Operations: add, subtract, multiply, divide, power, modulo")
try:
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
operation = input("Enter operation: ").lower()
result = calculate(x, y, operation)
print(f"Result: {result}")
except ValueError:
print("Please enter valid numbers")
Code Explanation
This script demonstrates:
- Advanced function design with docstrings
- Using dictionaries to map operations to functions
- Error handling for mathematical edge cases (division by zero)
- Modular code organization with single-responsibility functions
Think of this calculator like a Swiss Army knife - each function is a specialized tool designed for a specific type of operation, all organized into a compact, easy-to-use package.
Real-World Applications
- Financial applications
- Engineering tools
- Scientific calculators
- Data analysis applications
Extension Ideas
This calculator could be expanded to include:
- Square root and other mathematical functions
- Memory functions (store/recall values)
- Support for complex numbers
- Handling of units and conversions
Working with Lists
Lists in Python are ordered collections that can hold items of any type. Think of a list as a train with cars (elements) connected in a specific order, where each car can carry different types of cargo.
Example One: Shopping List Manager
A practical application of lists for everyday task management.
File location: python_basics/list_scripts/shopping_list.py
def create_shopping_list():
"""Create an empty shopping list"""
return []
def add_item(shopping_list, item):
"""
Add an item to the shopping list if it's not already there.
Args:
shopping_list (list): The current shopping list
item (str): Item to add
Returns:
list: Updated shopping list
"""
item = item.strip().capitalize()
if item not in shopping_list:
shopping_list.append(item)
print(f"Added '{item}' to the list")
else:
print(f"'{item}' is already on the list")
return shopping_list
def remove_item(shopping_list, item):
"""
Remove an item from the shopping list.
Args:
shopping_list (list): The current shopping list
item (str): Item to remove
Returns:
list: Updated shopping list
"""
item = item.strip().capitalize()
if item in shopping_list:
shopping_list.remove(item)
print(f"Removed '{item}' from the list")
else:
print(f"'{item}' is not on the list")
return shopping_list
def show_list(shopping_list):
"""
Display the current shopping list.
Args:
shopping_list (list): The shopping list to display
"""
if not shopping_list:
print("Your shopping list is empty")
return
print("\nYour Shopping List:")
for index, item in enumerate(shopping_list, 1):
print(f"{index}. {item}")
def sort_list(shopping_list):
"""
Sort the shopping list alphabetically.
Args:
shopping_list (list): The shopping list to sort
Returns:
list: Sorted shopping list
"""
return sorted(shopping_list)
# Example usage
my_list = create_shopping_list()
my_list = add_item(my_list, "Bread")
my_list = add_item(my_list, "Milk")
my_list = add_item(my_list, "Eggs")
my_list = add_item(my_list, "Milk") # Try to add duplicate
show_list(my_list)
my_list = remove_item(my_list, "Bread")
show_list(my_list)
my_list = sort_list(my_list)
show_list(my_list)
# Interactive shopping list manager
if __name__ == "__main__":
shopping_list = create_shopping_list()
while True:
print("\nShopping List Manager")
print("1. Add item")
print("2. Remove item")
print("3. Show list")
print("4. Sort list")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == "1":
item = input("Enter item to add: ")
shopping_list = add_item(shopping_list, item)
elif choice == "2":
item = input("Enter item to remove: ")
shopping_list = remove_item(shopping_list, item)
elif choice == "3":
show_list(shopping_list)
elif choice == "4":
shopping_list = sort_list(shopping_list)
print("List sorted")
show_list(shopping_list)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice, please try again")
Code Explanation
This script demonstrates:
- Creating and manipulating lists with
append(),remove(), andsorted() - Checking for item existence with
inoperator - List iteration with
enumerate()to get both index and value - Building an interactive menu-driven application
Think of this shopping list manager as a personal assistant that helps you keep track of items, prevents duplicates, and organizes everything neatly.
Real-World Applications
- Task management apps
- Inventory systems
- Recipe ingredient trackers
- Simple productivity tools
Example Two: Grade Analyzer
A more complex list application that processes numerical data.
File location: python_basics/list_scripts/grade_analyzer.py
def calculate_average(grades):
"""
Calculate the average of a list of grades.
Args:
grades (list): List of numeric grades
Returns:
float: Average grade or 0 if list is empty
"""
if not grades:
return 0
return sum(grades) / len(grades)
def find_highest(grades):
"""
Find the highest grade in the list.
Args:
grades (list): List of numeric grades
Returns:
float: Highest grade or None if list is empty
"""
if not grades:
return None
return max(grades)
def find_lowest(grades):
"""
Find the lowest grade in the list.
Args:
grades (list): List of numeric grades
Returns:
float: Lowest grade or None if list is empty
"""
if not grades:
return None
return min(grades)
def count_passing(grades, passing_threshold=60):
"""
Count the number of passing grades.
Args:
grades (list): List of numeric grades
passing_threshold (float): Minimum grade to pass
Returns:
int: Number of passing grades
"""
return sum(1 for grade in grades if grade >= passing_threshold)
def get_letter_grades(grades):
"""
Convert numeric grades to letter grades.
Args:
grades (list): List of numeric grades
Returns:
list: List of letter grades
"""
def to_letter(grade):
if grade >= 90:
return 'A'
elif grade >= 80:
return 'B'
elif grade >= 70:
return 'C'
elif grade >= 60:
return 'D'
else:
return 'F'
return [to_letter(grade) for grade in grades]
def analyze_grades(grades):
"""
Provide a complete analysis of grades.
Args:
grades (list): List of numeric grades
Returns:
dict: Analysis results
"""
if not grades:
return {"error": "No grades provided"}
average = calculate_average(grades)
letter_grades = get_letter_grades(grades)
# Count each letter grade
grade_distribution = {}
for letter in letter_grades:
if letter in grade_distribution:
grade_distribution[letter] += 1
else:
grade_distribution[letter] = 1
return {
"count": len(grades),
"average": round(average, 2),
"highest": find_highest(grades),
"lowest": find_lowest(grades),
"passing_count": count_passing(grades),
"failing_count": len(grades) - count_passing(grades),
"letter_grades": letter_grades,
"grade_distribution": grade_distribution
}
# Example usage
student_grades = [88, 92, 65, 73, 94, 56, 78, 85, 91, 79]
analysis = analyze_grades(student_grades)
print("Grade Analysis:")
print(f"Total grades: {analysis['count']}")
print(f"Average: {analysis['average']}")
print(f"Highest: {analysis['highest']}")
print(f"Lowest: {analysis['lowest']}")
print(f"Passing: {analysis['passing_count']}")
print(f"Failing: {analysis['failing_count']}")
print("Grade distribution:")
for letter, count in analysis['grade_distribution'].items():
print(f" {letter}: {count}")
# Interactive grade analyzer
if __name__ == "__main__":
print("\nGrade Analyzer")
grades = []
while True:
try:
grade_input = input("Enter a grade (or 'done' to finish): ")
if grade_input.lower() == 'done':
break
grade = float(grade_input)
if 0 <= grade <= 100:
grades.append(grade)
else:
print("Grade must be between 0 and 100")
except ValueError:
print("Please enter a valid number or 'done'")
if grades:
analysis = analyze_grades(grades)
print("\nGrade Analysis Results:")
for key, value in analysis.items():
if key != "letter_grades": # Skip detailed letter grades list
print(f"{key}: {value}")
else:
print("No grades entered")
Code Explanation
This script demonstrates:
- Advanced list processing with functions like
sum(),max(), andmin() - List comprehensions for transforming data
- Inner function definitions for specialized purposes
- Building a dictionary of analysis results
- Input validation and error handling
Think of this grade analyzer as a classroom assistant that quickly processes raw scores and generates insights that would take a teacher hours to calculate manually.
Real-World Applications
- Educational management systems
- Performance evaluation tools
- Data analysis applications
- Educational research
Extension Ideas
This analyzer could be expanded to include:
- Calculation of median and mode
- Generation of visual representations (histograms, etc.)
- Comparison of performance across different tests
- Trend analysis over time
Working with Dictionaries
Dictionaries in Python are collections of key-value pairs that enable efficient lookups. Think of a dictionary as a phone book where you can instantly find a number (value) if you know the person's name (key).
Example One: Contact Manager
A practical application for storing and retrieving structured data.
File location: python_basics/dictionary_scripts/contact_manager.py
def create_contacts():
"""Create an empty contacts dictionary"""
return {}
def add_contact(contacts, name, phone, email=None, address=None):
"""
Add or update a contact.
Args:
contacts (dict): The contacts dictionary
name (str): Contact's name
phone (str): Contact's phone number
email (str, optional): Contact's email
address (str, optional): Contact's address
Returns:
dict: Updated contacts dictionary
"""
name = name.strip().title() # Normalize name format
# Create or update contact
contacts[name] = {
"phone": phone,
"email": email,
"address": address
}
print(f"Contact '{name}' added/updated")
return contacts
def remove_contact(contacts, name):
"""
Remove a contact.
Args:
contacts (dict): The contacts dictionary
name (str): Name of contact to remove
Returns:
dict: Updated contacts dictionary
"""
name = name.strip().title() # Normalize name format
if name in contacts:
del contacts[name]
print(f"Contact '{name}' removed")
else:
print(f"Contact '{name}' not found")
return contacts
def find_contact(contacts, name):
"""
Find a contact by name.
Args:
contacts (dict): The contacts dictionary
name (str): Name to search for
Returns:
dict or None: Contact information or None if not found
"""
name = name.strip().title() # Normalize name format
if name in contacts:
return contacts[name]
else:
return None
def search_contacts(contacts, query):
"""
Search contacts by partial name match.
Args:
contacts (dict): The contacts dictionary
query (str): Search query
Returns:
dict: Dictionary of matching contacts
"""
query = query.lower()
results = {}
for name, info in contacts.items():
if query in name.lower():
results[name] = info
return results
def display_contact(name, info):
"""
Display contact information in a formatted way.
Args:
name (str): Contact's name
info (dict): Contact's information
"""
print(f"\n--- {name} ---")
print(f"Phone: {info['phone']}")
if info['email']:
print(f"Email: {info['email']}")
if info['address']:
print(f"Address: {info['address']}")
def display_all_contacts(contacts):
"""
Display all contacts.
Args:
contacts (dict): The contacts dictionary
"""
if not contacts:
print("No contacts found")
return
print("\nAll Contacts:")
for name, info in sorted(contacts.items()):
display_contact(name, info)
# Example usage
my_contacts = create_contacts()
my_contacts = add_contact(my_contacts, "John Smith", "555-123-4567", "john@example.com", "123 Main St")
my_contacts = add_contact(my_contacts, "Jane Doe", "555-987-6543", "jane@example.com")
my_contacts = add_contact(my_contacts, "Bob Johnson", "555-555-5555")
# Find and display a contact
found = find_contact(my_contacts, "Jane Doe")
if found:
display_contact("Jane Doe", found)
else:
print("Contact not found")
# Search for contacts
results = search_contacts(my_contacts, "jo")
if results:
print("\nSearch results for 'jo':")
for name, info in results.items():
display_contact(name, info)
else:
print("No matching contacts")
# Remove a contact
my_contacts = remove_contact(my_contacts, "Bob Johnson")
# Display all contacts
display_all_contacts(my_contacts)
# Interactive contact manager
if __name__ == "__main__":
contacts = create_contacts()
while True:
print("\nContact Manager")
print("1. Add/Update contact")
print("2. Find contact")
print("3. Search contacts")
print("4. Remove contact")
print("5. Display all contacts")
print("6. Exit")
choice = input("Enter your choice (1-6): ")
if choice == "1":
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email (optional, press Enter to skip): ") or None
address = input("Enter address (optional, press Enter to skip): ") or None
contacts = add_contact(contacts, name, phone, email, address)
elif choice == "2":
name = input("Enter name to find: ")
found = find_contact(contacts, name)
if found:
display_contact(name.strip().title(), found)
else:
print(f"Contact '{name}' not found")
elif choice == "3":
query = input("Enter search term: ")
results = search_contacts(contacts, query)
if results:
print(f"\nFound {len(results)} contacts:")
for name, info in results.items():
display_contact(name, info)
else:
print("No matching contacts")
elif choice == "4":
name = input("Enter name to remove: ")
contacts = remove_contact(contacts, name)
elif choice == "5":
display_all_contacts(contacts)
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid choice, please try again")
Code Explanation
This script demonstrates:
- Creating and manipulating nested dictionaries
- Key-value access patterns
- Dictionary methods like
delfor removal - Searching and filtering dictionary data
- Sorting dictionaries with
sorted()
Think of this contact manager as a personal rolodex that lets you immediately access or update information about people in your network.
Real-World Applications
- Address books and contact management systems
- Customer databases
- User profile management
- Directory services
Example Two: Word Frequency Counter
A more complex dictionary application for text analysis.
File location: python_basics/dictionary_scripts/word_counter.py
import string
def count_words(text):
"""
Count frequency of each word in a text.
Args:
text (str): Text to analyze
Returns:
dict: Dictionary with words and their counts
"""
if not text:
return {}
# Convert to lowercase
text = text.lower()
# Remove punctuation
for char in string.punctuation:
text = text.replace(char, " ")
# Split into words and count
words = text.split()
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
def get_top_words(word_count, limit=10):
"""
Get the most frequent words.
Args:
word_count (dict): Dictionary with words and counts
limit (int): Maximum number of words to return
Returns:
list: List of (word, count) tuples sorted by frequency
"""
return sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:limit]
def filter_stop_words(word_count, stop_words=None):
"""
Remove common stop words from word count.
Args:
word_count (dict): Dictionary with words and counts
stop_words (list): List of stop words to remove
Returns:
dict: Filtered dictionary
"""
if stop_words is None:
# Common English stop words
stop_words = ["the", "and", "a", "to", "of", "in", "is", "it",
"that", "was", "for", "on", "with", "as", "at"]
return {word: count for word, count in word_count.items()
if word not in stop_words}
def display_word_stats(word_count):
"""
Display statistics about word usage.
Args:
word_count (dict): Dictionary with words and counts
"""
# Total words and unique words
total_words = sum(word_count.values())
unique_words = len(word_count)
print(f"Total words: {total_words}")
print(f"Unique words: {unique_words}")
# Most common words
print("\nMost common words:")
top_words = get_top_words(word_count, 5)
for word, count in top_words:
print(f" '{word}': {count} times")
# Word length distribution
length_dist = {}
for word in word_count:
length = len(word)
if length in length_dist:
length_dist[length] += 1
else:
length_dist[length] = 1
print("\nWord length distribution:")
for length, count in sorted(length_dist.items()):
print(f" {length} letters: {count} words")
# Example usage
sample_text = """
Python is a programming language that lets you work quickly and integrate systems
more effectively. Python is easy to learn, yet powerful and versatile enough to be
the primary language of companies like Google and Netflix.
"""
word_count = count_words(sample_text)
print("Original word count:")
for word, count in sorted(word_count.items()):
print(f" '{word}': {count}")
# Filter stop words
filtered_count = filter_stop_words(word_count)
print("\nFiltered word count:")
for word, count in sorted(filtered_count.items()):
print(f" '{word}': {count}")
# Display statistics
print("\nWord Statistics:")
display_word_stats(filtered_count)
# Interactive word counter
if __name__ == "__main__":
print("\nWord Frequency Counter")
text = input("Enter text to analyze (or press Enter to use sample text): ")
if not text:
text = sample_text
print("Using sample text...")
word_count = count_words(text)
while True:
print("\nWord Counter Menu")
print("1. Show all word counts")
print("2. Show top words")
print("3. Show word statistics")
print("4. Filter stop words")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == "1":
print("\nAll word counts:")
for word, count in sorted(word_count.items()):
print(f" '{word}': {count}")
elif choice == "2":
try:
limit = int(input("How many top words to show? "))
top_words = get_top_words(word_count, limit)
print(f"\nTop {len(top_words)} words:")
for word, count in top_words:
print(f" '{word}': {count}")
except ValueError:
print("Please enter a valid number")
elif choice == "3":
print("\nWord Statistics:")
display_word_stats(word_count)
elif choice == "4":
filter_choice = input("Use default stop words? (y/n): ")
if filter_choice.lower() == 'y':
word_count = filter_stop_words(word_count)
print("Filtered with default stop words")
else:
custom_stop_words = input("Enter custom stop words separated by spaces: ").split()
word_count = filter_stop_words(word_count, custom_stop_words)
print(f"Filtered {len(custom_stop_words)} custom stop words")
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice, please try again")
Code Explanation
This script demonstrates:
- Using dictionaries for counting and frequency analysis
- Dictionary comprehensions for filtering
- Sorting dictionaries with custom key functions
- Advanced text processing
- Statistical analysis with dictionaries
Think of this word counter like a microscope that examines the DNA of a text, revealing patterns in word usage that might not be visible to the naked eye.
Real-World Applications
- Content analysis tools
- SEO keyword analyzers
- Plagiarism detection systems
- Author attribution studies
- Text mining applications
Extension Ideas
This word counter could be expanded to include:
- N-gram analysis (pairs or triplets of words)
- Sentiment analysis based on word choice
- Visualization of word frequencies
- Comparison between different texts
Working with Booleans
Booleans in Python represent the truth values True and False. Think of them as simple on/off switches or yes/no questions that control the flow of your program.
Example One: Eligibility Checker
A practical application that uses boolean logic for decision making.
File location: python_basics/boolean_scripts/eligibility_checker.py
def check_voting_eligibility(age, citizenship, registration):
"""
Check if a person is eligible to vote.
Args:
age (int): Person's age
citizenship (bool): Whether the person is a citizen
registration (bool): Whether the person is registered
Returns:
tuple: (bool, str) - Eligibility status and reason
"""
if age < 18:
return False, "Must be at least 18 years old"
if not citizenship:
return False, "Must be a citizen"
if not registration:
return False, "Must be registered to vote"
return True, "Eligible to vote"
def check_loan_eligibility(income, credit_score, debt_ratio, employment_years):
"""
Check if a person is eligible for a loan.
Args:
income (float): Annual income
credit_score (int): Credit score (300-850)
debt_ratio (float): Debt-to-income ratio (0.0-1.0)
employment_years (float): Years at current employment
Returns:
tuple: (bool, str) - Eligibility status and reason or loan type
"""
# Basic validation
if income <= 0:
return False, "Income must be positive"
if not (300 <= credit_score <= 850):
return False, "Invalid credit score"
if not (0 <= debt_ratio <= 1):
return False, "Invalid debt ratio"
# Premium loan eligibility
if (income >= 100000 and credit_score >= 750 and
debt_ratio <= 0.3 and employment_years >= 2):
return True, "Eligible for premium loan"
# Standard loan eligibility
if (income >= 50000 and credit_score >= 650 and
debt_ratio <= 0.4 and employment_years >= 1):
return True, "Eligible for standard loan"
# Basic loan eligibility
if (income >= 30000 and credit_score >= 600 and
debt_ratio <= 0.5 and employment_years >= 0.5):
return True, "Eligible for basic loan"
return False, "Does not meet minimum requirements"
def check_discount_eligibility(is_member, purchase_total, has_coupon, is_senior):
"""
Determine what discount a customer is eligible for.
Args:
is_member (bool): Whether customer is a member
purchase_total (float): Total purchase amount
has_coupon (bool): Whether customer has a coupon
is_senior (bool): Whether customer is a senior
Returns:
float: Discount percentage (0.0-1.0)
"""
discount = 0.0
# Membership discount (5%)
if is_member:
discount += 0.05
# Large purchase discount (10% if over $100)
if purchase_total >= 100:
discount += 0.10
# Coupon discount (15%)
if has_coupon:
discount += 0.15
# Senior discount (5%)
if is_senior:
discount += 0.05
# Cap the maximum discount at 25%
return min(discount, 0.25)
# Example usage
print("Voting Eligibility Examples:")
print(check_voting_eligibility(20, True, True)) # (True, 'Eligible to vote')
print(check_voting_eligibility(17, True, True)) # (False, 'Must be at least 18 years old')
print(check_voting_eligibility(21, False, True)) # (False, 'Must be a citizen')
print(check_voting_eligibility(21, True, False)) # (False, 'Must be registered to vote')
print("\nLoan Eligibility Examples:")
print(check_loan_eligibility(120000, 780, 0.2, 5)) # Premium loan
print(check_loan_eligibility(60000, 700, 0.35, 2)) # Standard loan
print(check_loan_eligibility(35000, 620, 0.45, 1)) # Basic loan
print(check_loan_eligibility(25000, 580, 0.6, 0.2)) # Not eligible
print("\nDiscount Eligibility Examples:")
print(f"Discount: {check_discount_eligibility(True, 150, True, True) * 100}%") # 25% (capped)
print(f"Discount: {check_discount_eligibility(True, 150, False, False) * 100}%") # 15%
print(f"Discount: {check_discount_eligibility(False, 50, True, False) * 100}%") # 15%
print(f"Discount: {check_discount_eligibility(False, 25, False, False) * 100}%") # 0%
# Interactive eligibility checker
if __name__ == "__main__":
print("\nEligibility Checker")
print("1. Check voting eligibility")
print("2. Check loan eligibility")
print("3. Check discount eligibility")
choice = input("Enter your choice (1-3): ")
if choice == "1":
try:
age = int(input("Enter age: "))
citizenship = input("Is a citizen? (y/n): ").lower() == 'y'
registration = input("Is registered to vote? (y/n): ").lower() == 'y'
eligible, reason = check_voting_eligibility(age, citizenship, registration)
print(f"Voting eligibility: {'Eligible' if eligible else 'Not eligible'}")
print(f"Reason: {reason}")
except ValueError:
print("Please enter a valid age")
elif choice == "2":
try:
income = float(input("Enter annual income: $"))
credit_score = int(input("Enter credit score (300-850): "))
debt_ratio = float(input("Enter debt-to-income ratio (0.0-1.0): "))
employment_years = float(input("Enter years at current job: "))
eligible, reason = check_loan_eligibility(income, credit_score, debt_ratio, employment_years)
print(f"Loan eligibility: {'Eligible' if eligible else 'Not eligible'}")
print(f"Details: {reason}")
except ValueError:
print("Please enter valid numeric values")
elif choice == "3":
try:
is_member = input("Is a member? (y/n): ").lower() == 'y'
purchase_total = float(input("Enter purchase total: $"))
has_coupon = input("Has a coupon? (y/n): ").lower() == 'y'
is_senior = input("Is a senior? (y/n): ").lower() == 'y'
discount = check_discount_eligibility(is_member, purchase_total, has_coupon, is_senior)
print(f"Discount percentage: {discount * 100:.1f}%")
print(f"Final price: ${purchase_total * (1 - discount):.2f}")
except ValueError:
print("Please enter a valid purchase total")
else:
print("Invalid choice")
Code Explanation
This script demonstrates:
- Boolean logic with
and,or, andnotoperators - Conditional statements for decision making
- Converting user input to boolean values
- Using boolean values to control program flow
- Returning tuple results with both status and explanation
Think of this eligibility checker as a gatekeeper that applies specific rules to determine if someone should be allowed to proceed. Each condition is like a checkpoint that must be passed.
Real-World Applications
- Membership management systems
- Loan application processors
- E-commerce checkout systems
- Government service eligibility checkers
- Health insurance qualification systems
Example Two: Password Validator
A more complex boolean application for security validation.
File location: python_basics/boolean_scripts/password_validator.py
def has_minimum_length(password, min_length=8):
"""
Check if password meets minimum length requirement.
Args:
password (str): Password to check
min_length (int): Minimum required length
Returns:
bool: True if password is long enough
"""
return len(password) >= min_length
def has_uppercase(password):
"""
Check if password contains at least one uppercase letter.
Args:
password (str): Password to check
Returns:
bool: True if password contains uppercase
"""
for char in password:
if char.isupper():
return True
return False
def has_lowercase(password):
"""
Check if password contains at least one lowercase letter.
Args:
password (str): Password to check
Returns:
bool: True if password contains lowercase
"""
for char in password:
if char.islower():
return True
return False
def has_digit(password):
"""
Check if password contains at least one digit.
Args:
password (str): Password to check
Returns:
bool: True if password contains a digit
"""
for char in password:
if char.isdigit():
return True
return False
def has_special_char(password):
"""
Check if password contains at least one special character.
Args:
password (str): Password to check
Returns:
bool: True if password contains a special character
"""
special_chars = "!@#$%^&*()-_=+[]{}\\|;:'\",.<>/?"
for char in password:
if char in special_chars:
return True
return False
def validate_password(password, min_length=8, require_uppercase=True,
require_lowercase=True, require_digit=True,
require_special=True):
"""
Validate a password against multiple criteria.
Args:
password (str): Password to validate
min_length (int): Minimum required length
require_uppercase (bool): Require uppercase letter
require_lowercase (bool): Require lowercase letter
require_digit (bool): Require digit
require_special (bool): Require special character
Returns:
tuple: (bool, list) - Valid status and list of failed checks
"""
failed_checks = []
# Check minimum length
if not has_minimum_length(password, min_length):
failed_checks.append(f"Password must be at least {min_length} characters long")
# Check for uppercase
if require_uppercase and not has_uppercase(password):
failed_checks.append("Password must contain at least one uppercase letter")
# Check for lowercase
if require_lowercase and not has_lowercase(password):
failed_checks.append("Password must contain at least one lowercase letter")
# Check for digit
if require_digit and not has_digit(password):
failed_checks.append("Password must contain at least one digit")
# Check for special character
if require_special and not has_special_char(password):
failed_checks.append("Password must contain at least one special character")
# Password is valid if no checks failed
is_valid = len(failed_checks) == 0
return is_valid, failed_checks
def calculate_password_strength(password):
"""
Calculate a password strength score (0-100).
Args:
password (str): Password to evaluate
Returns:
tuple: (int, str) - Strength score and rating
"""
score = 0
# Length contributes up to 30 points
length_score = min(30, len(password) * 3)
score += length_score
# Character variety contributes up to 70 points
if has_lowercase(password):
score += 10
if has_uppercase(password):
score += 15
if has_digit(password):
score += 15
if has_special_char(password):
score += 20
# Check for a mix of character types
char_type_count = sum([
has_lowercase(password),
has_uppercase(password),
has_digit(password),
has_special_char(password)
])
if char_type_count >= 3:
score += 10
# Assign a rating
if score >= 90:
rating = "Excellent"
elif score >= 70:
rating = "Strong"
elif score >= 50:
rating = "Good"
elif score >= 30:
rating = "Weak"
else:
rating = "Very Weak"
return score, rating
# Example usage
print("Password Validation Examples:")
passwords = [
"password", # Weak, fails multiple checks
"Password1", # Missing special char
"Abcd1234!", # Strong password
"a1B!", # Too short
"UPPERCASE123!", # Missing lowercase
"lowercase123!" # Missing uppercase
]
for password in passwords:
is_valid, failed_checks = validate_password(password)
score, rating = calculate_password_strength(password)
print(f"\nPassword: {password}")
print(f"Valid: {is_valid}")
if failed_checks:
print("Failed checks:")
for check in failed_checks:
print(f" - {check}")
print(f"Strength: {score}/100 ({rating})")
# Interactive password validator
if __name__ == "__main__":
print("\nPassword Validator and Strength Checker")
while True:
password = input("\nEnter a password to validate (or 'exit' to quit): ")
if password.lower() == 'exit':
print("Goodbye!")
break
# Check validity
is_valid, failed_checks = validate_password(password)
# Calculate strength
score, rating = calculate_password_strength(password)
# Display results
print(f"\nPassword: {'Valid' if is_valid else 'Invalid'}")
if failed_checks:
print("Issues to fix:")
for check in failed_checks:
print(f" - {check}")
print(f"Strength: {score}/100 ({rating})")
# Suggestion for improvement
if score < 70:
print("\nSuggestion for a stronger password:")
if len(password) < 10:
print(" - Make your password longer")
if not has_uppercase(password):
print(" - Add uppercase letter(s)")
if not has_lowercase(password):
print(" - Add lowercase letter(s)")
if not has_digit(password):
print(" - Add digit(s)")
if not has_special_char(password):
print(" - Add special character(s)")
Code Explanation
This script demonstrates:
- Using boolean functions to check individual conditions
- Combining boolean results to evaluate complex criteria
- Building a scoring system based on boolean checks
- Providing detailed feedback based on failed conditions
- Implementing security best practices
Think of this password validator as a security guard checking multiple forms of ID before allowing access. Each check verifies a different aspect of the password's "identity."
Real-World Applications
- User registration systems
- Security compliance tools
- Password management applications
- Enterprise security systems
Extension Ideas
This validator could be expanded to include:
- Dictionary word detection
- Common password pattern recognition
- Password history comparison
- Entropy calculation for more accurate strength assessment
Conclusion and Next Steps
In this tutorial, we've explored how to solve real-world problems using Python's core data types:
-
Strings: For text processing and manipulation
- Name formatting and standardization
- Text analysis and statistics
-
Numbers: For mathematical operations and calculations
- Temperature conversion between different scales
- Building a versatile calculator
-
Lists: For ordered collections and sequences
- Shopping list management
- Grade analysis and statistics
-
Dictionaries: For key-value mappings and lookups
- Contact management system
- Word frequency analysis
-
Booleans: For decision making and validation
- Eligibility criteria checking
- Password validation and security assessment
Each data type serves a specific purpose, much like specialized tools in a toolkit. By understanding when and how to use each one, you can build increasingly sophisticated programs to solve real-world problems.
Where to Go from Here
To continue your Python journey, consider exploring:
- Object-Oriented Programming: Learn to create your own custom data types
- File I/O: Save and load data from files
- Libraries and Frameworks: Use pre-built tools like requests, pandas, or flask
- Web Development: Build interactive web applications with Django or Flask
- Data Analysis: Dive deeper with pandas, numpy, and matplotlib
- API Integration: Connect your programs to external services
Remember, programming is like learning a musical instrument—regular practice leads to mastery. Try modifying these examples and creating your own programs to solidify your understanding.
Happy coding!