python

A shared tag with AI prompts and code snippets

From workspace: FreeCodeCamp

Team: Main

Total snippets: 19

FreeCodeCamp

python

19 snippets

Replace Vowels in Each Word with * (Asterisk)

Replace all vowels in words using nested list comprehension and join.

sentence = "code with python" vowels = 'aeiou' masked = [''.join(['*' if ch in vowels else ch for ch in word]) for word in sentence.split()] print(masked) # ['c*d*', 'w*th', 'pyth*n']

Reverse Each Word in a Sentence

Reverse characters in each word using slicing inside list comprehension.

sentence = "list comprehension python" reversed_words = [word[::-1] for word in sentence.split()] print(reversed_words) # ['tsil', 'noisneherpmoc', 'nohtyp']

Filter Words by Length

Return only words longer than 4 characters using list comprehension.

sentence = "this is a list comprehension demo" filtered = [word for word in sentence.split() if len(word) > 4] print(filtered) # ['list', 'comprehension']

Toggle Case for Each Word

Flip case of each word using .swapcase() in list comprehension.

sentence = "HeLLo WoRLD" toggled = [word.swapcase() for word in sentence.split()] print(toggled) # ['hEllO', 'wOrld']

Convert Words to Title Case

Convert each word to title case using list comprehension.

sentence = "hello world from python" titled = [word.title() for word in sentence.split()] print(titled) # ['Hello', 'World', 'From', 'Python']

Convert Words to Uppercase Using List Comprehension

Use list comprehension to convert all words in a sentence to uppercase.

sentence = "hello world from python" uppercased = [word.upper() for word in sentence.split()] print(uppercased) # ['HELLO', 'WORLD', 'FROM', 'PYTHON']

Sort Expenses Descending Using Lambda Key

Sort expenses in descending order using lambda as the sorting key.

expenses = [10, 250, 75, 19] sorted_expenses = sorted(expenses, key=lambda x: -x) print(sorted_expenses) # Output: [250, 75, 19, 10]

Label Each Expense as Low or High

Use lambda with map and conditional expression to tag each expense.

expenses = [120.50, 32.00, 5.75, 50.00] labels = list(map(lambda x: "High" if x > 50 else "Low", expenses)) print(labels) # Output: ['High', 'Low', 'Low', 'Low']

Total Expenses Using Lambda and Reduce

Calculate the total cost using reduce and a lambda expression.

from functools import reduce expenses = [120.50, 32.00, 5.75, 50.00] total = reduce(lambda x, y: x + y, expenses) print(total) # Output: 208.25

Apply Tax to All Expenses Using Lambda and Map

Add 21% tax to each expense using a lambda inside a map function.

expenses = [100, 40, 10] with_tax = list(map(lambda x: round(x * 1.21, 2), expenses)) print(with_tax) # Output: [121.0, 48.4, 12.1]

Filter High Expenses Using Lambda and Filter

Use a lambda function to filter all expenses greater than a threshold (e.g. 50).

expenses = [120.50, 32.00, 5.75, 50.00, 89.99] high = list(filter(lambda x: x > 50, expenses)) print(high) # Output: [120.5, 89.99]

Explain Each Step of the Luhn Algorithm in Comments

A commented version of the Luhn validator to explain how numbers and strings are processed.

def luhn_verbose(card_number): digits = [int(d) for d in str(card_number)][::-1] # reverse digits for i in range(1, len(digits), 2): # double every second digit doubled = digits[i] * 2 digits[i] = doubled if doubled < 10...

Generate a Random Valid Credit Card Number (Luhn-Compliant)

Use the Luhn algorithm to generate test card numbers for simulations.

import random def generate_card(): base = [random.randint(0, 9) for _ in range(15)] digits = base[::-1] for i in range(1, len(digits), 2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 ...

Format Card Number as XXXX-XXXX-XXXX-XXXX

Write a helper function to format card numbers using string manipulation.

def format_card(number): s = str(number).replace(" ", "") return "-".join([s[i:i+4] for i in range(0, len(s), 4)])

Add a Check Digit to a Number Using Luhn Algorithm

Extend the Luhn logic to generate a valid check digit for any number.

def luhn_generate(number): digits = [int(d) for d in str(number)] digits.append(0) for i in range(len(digits) - 2, -1, -2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 checksum =...

Validate a Credit Card Number Using the Luhn Algorithm

Build a function that takes a number as input and checks if it passes the Luhn checksum validation.

def luhn_check(card_number): digits = [int(d) for d in str(card_number)][::-1] for i in range(1, len(digits), 2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 return sum(digits) % 10 == 0

Test With Multiple Assertions

Group multiple assertions in a single test function.

def test_numbers(): assert 1 + 1 == 2 assert 2 * 2 == 4 assert 3 - 1 == 2

Use Custom Test File Naming

Follow Pytest’s naming convention for test discovery.

# Use files starting with 'test_' # Example: test_math.py

Write a Basic Pytest Function

A minimal test function using Pytest with assert.

def test_addition(): assert 2 + 2 == 4

FreeCodeCamp - python - AI Prompts & Code Snippets | Snippets AI