Master Python collections, functions, and string manipulation through hands-on coding challenges.
Build practical Python skills with lists, functions, and string operations.
fruits = ["apple", "banana", "cherry"][] to create a listfruits.append("orange")append() adds ONE item to the end of the listextend() or + operatorprint(fruits[0])fruits[-1] is the last itemIndexError occurs if index is out of rangelen(fruits) to count itemslen() returns the number of items in a listfor fruit in fruits:
print(fruit)if "apple" in fruits:fruits.remove("banana")del fruits[0] or fruits.pop(0)fruits[1:3] gets items at index 1 and 2fruits.sort() or sorted(fruits)Original list: ['apple', 'banana', 'cherry']
After append: ['apple', 'banana', 'cherry', 'orange']
First fruit: apple
List length: 4
All fruits:
apple
banana
cherry
orange
fruits = ["apple", "banana", "cherry"]fruits.append("orange")fruits[0] (zero-based indexing!)fruits[-1] (negative index)len(fruits)for fruit in fruits:Review feedback below
def add_numbers(a, b):
return a + b
• def keyword defines a function: is required after the parenthesesreturn sends a value back to the callerdef multiply_numbers(a, b):
return a * bdef greet(name="World"):
return f"Hello, {name}!"
• Default parameters have a fallback valuegreet() returns "Hello, World!"greet("Alice") returns "Hello, Alice!"print(add_numbers(5, 3)) # Output: 8
print(multiply_numbers(4, 7)) # Output: 28
print(greet()) # Output: Hello, World!
print(greet("Python")) # Output: Hello, Python!return implicitly return None"""This function adds two numbers."""def sum_all(*args):def func(**kwargs):square = lambda x: x ** 2Sum: 8
Product: 28
Hello, World!
Hello, Python!
def function_name(param1, param2):return resultdef greet(name="World"):result = add_numbers(5, 3)return f"Hello, {name}!"def name():Review feedback below
text = " Hello, Python World! "cleaned = text.strip()strip() removes leading AND trailing whitespacelstrip() removes only leading (left) whitespacerstrip() removes only trailing (right) whitespaceupper() and lower():text.upper() → "HELLO, PYTHON WORLD!"text.lower() → "hello, python world!"text.title() → "Hello, Python World!"text.capitalize() → "Hello, python world!"text.replace("Python", "Java") → "Hello, Java World!"text.replace("l", "L", 1)"a,b,c".split(",") → ["a", "b", "c"]"Hello World".split() → ["Hello", "World"] (splits on whitespace)text.find("Python") returns the starting index-1 if not foundtext.index("Python") is similar but raises ValueError if not foundtext[0:5] gets first 5 characterstext[::-1]text.startswith("Hello"), text.endswith("!")text.isalpha(), text.isdigit(), text.isalnum()", ".join(["a", "b", "c"]) → "a, b, c"Original: ' Hello, Python World! '
Stripped: 'Hello, Python World!'
Uppercase: 'HELLO, PYTHON WORLD!'
Lowercase: 'hello, python world!'
Replaced: 'Hello, Java World!'
Split: ['Hello,', 'Python', 'World!']
Find 'Python': 7
text.strip()text.upper()text.lower()text.replace("old", "new")text.split() or text.split(",")text.find("substring") returns index or -1Review feedback below