Skip to content

Python Basics

Copy the code from the exercise below and play around with the different basic building blocks of Python. Create new cells with # %% as necessary.


# %%
# Types:
# Boolean (bool), Integer (int), Range (range), Float (float),
# Complex Number (complex), String (str), List (list), Tuple (tuple),
# Dictionary (dict), Set (set), Absence of value (None)
# %%
# Boolean (bool)
# %%
# Integer (int)
# %%
# Range (range)
# %%
# Float (float)
# %%
# Complex Number (complex)
# %%
# String (str)
# %%
# List (list)
# %%
# Tuple (tuple)
# %%
# Dictionary (dict)
# %%
# Set (set)
# %%
# Absence of value (None)
# %%
# Variables and Assignment (=)
# %%
# Mathematical Operations (+,-,*,**,/,//)
# %%
# Comparisons (==,!=,<,<=,>,>=)
# %%
# Boolean Operators (and/or/not)
# %%
# String Expressions (+,*)
# %%
# List Expressions (+,*)
# %%
# Conditional Execution (if/elif/else)
# %%
# For Loops (for)
# %%
# While Loops (while)
# %%
# List Comprehensions [...]
# %%
# Dictionary Comprehensions {...}
# %%
# Functions (def)
# %%
# Anonymous Functions (lambda)
# %%
# Import Packages (import)
# %%
# List Indexing
# %%
# Dictionary Indexing
# %%
# Print various patterns using the print() function, for loops, and string manipulation.
# %%
# Print the pattern:
# *
# **
# ***
# ****
# *****
# %%
# Print the pattern:
# *
# **
# ***
# ****
# *****
# %%
# Print the pattern:
# *
# ***
# *****
# *******
# *********
# %%
# Print the pattern:
# *
# ***
# *****
# *******
# *********
# ***********
# *********
# *******
# *****
# ***
# *
# %%
# Guessing Game
# -------------
# 1. Generate a random number between 1 and 20
# 2. Give the player 3 attempts to guess
# 3. Provide feedback if the guess is too high or too low
# 4. Congratulate the player if they guess correctly
# 5. Reveal the answer if they run out of guesses
# %%
# Create a fortune-telling program that gives random responses to yes/no questions.
# - Store 10 different responses in a list (mix of positive, negative, and uncertain)
# - Repeatedly ask for questions until user types 'quit'
# - Select and display a random response for each question
# - Handle empty input with an error message
# ---
# Example Responses
# - Positive: "Yes, definitely", "It is certain", "Outlook good"
# - Negative: "Don't count on it", "Very doubtful"
# - Uncertain: "Ask again later", "Cannot predict now"
# ---
# Sample Run
# Ask a question: Will it rain tomorrow?
# ✨ Most likely
#
# Ask a question:
# Need a question!
#
# Ask a question: quit
# Goodbye!
# ---
# Hints
# - random.choice(list) to pick random element
# - Use while True with break or check for 'quit' in loop condition
# - .lower() for case-insensitive quit command
# %%
# %%
# Create a betting game where players guess if two dice will sum to odd or even.
# - Start with $100, win at $200, lose at $0
# - Each round: bet money → guess odd/even → roll two dice → win or lose bet
# - Correct guess doubles your bet, wrong guess loses it
# ---
# 1 Game loop that runs while balance is between $0 and $200
# 2 Get bet amount (1 to current balance)
# 3 Get odd/even guess from player
# 4 Roll two dice (1-6 each) and show result
# 5 Update balance based on outcome
# 6 Display win/loss messages at game end
# ---
# Sample Run
# Balance: $100. Bet: 50
# Odd or Even? (o/e): e
# Rolled 3 + 4 = 7 (odd)
# You lost $50!
# ---
# Hints
# random.randint(1, 6) for dice
# total % 2 == 0 to check if even
# while 0 < balance < 200 for game loop
# %%