Python Programming Labs

Master Python programming through hands-on coding challenges. Write real code, get instant feedback, and build practical skills.

Python Fundamentals - Module 1

Write actual Python code to solve each challenge. Your code is validated for correctness.

Lab 1: Variables & Data Types
Beginner
Coding Challenge
Your Task: Create a Python program that stores and displays personal profile information using different data types.

Detailed Requirements:
1. String variable "name" - Store a person's full name (e.g., "Alice Johnson"). In Python, strings can be created using either single quotes 'text' or double quotes "text". Strings are used for text data and are immutable (cannot be changed after creation).

2. Integer variable "age" - Store the person's age as a whole number (e.g., 28). Unlike some languages, Python integers have unlimited precision - they can be as large as your memory allows! Use type(age) to verify it's an int.

3. Float variable "height" - Store height in meters with decimal precision (e.g., 1.65). Floats in Python use 64-bit double precision. Be aware that 0.1 + 0.2 may not exactly equal 0.3 due to floating-point representation.

4. Boolean variable "is_student" - Store True or False to indicate student status. Note: Python booleans are capitalized (True/False), not lowercase like in some other languages. Booleans are actually a subtype of integers where True == 1 and False == 0.

Output Requirements:
Use print() to display each variable with a descriptive label. You can use f-strings (formatted string literals) for clean output: print(f"Name: {name}"). Alternatively, use string concatenation with + or comma separation.

💡 Pro Tips:
• Python uses snake_case for variable names (e.g., is_student not isStudent)
• No semicolons needed at the end of lines
• No type declarations required - Python is dynamically typed
• Use type(variable) to check a variable's type

Expected Output:
Name: Alice Johnson Age: 28 Height: 1.65 Is Student: True

Requirements Checklist

Declare a string variable named "name" with any value
Declare an integer variable named "age"
Declare a float variable named "height"
Declare a boolean variable named "is_student"
Print all variables using print()
Code runs without errors
Output
# Click "Run Code" to execute your Python code # Your program output will appear here
Hints & Tips
• String declaration: name = "Alice" or name = 'Alice'
• Integer declaration: age = 28 (no type keyword needed)
• Float declaration: height = 1.65 (include decimal point)
• Boolean declaration: is_student = True (capital T/F)
• F-string print: print(f"Age: {age}")
• Check type: print(type(name)) outputs <class 'str'>
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 2: Conditionals & Logic
Beginner
Coding Challenge
Your Task: Build a grade calculator that converts numeric scores to letter grades using Python conditional statements.

Detailed Requirements:
1. Create a variable "score" - Initialize with a test value between 0-100. Try different values (95, 82, 71, 65, 45) to test all grade paths.

2. Create a variable "grade" - This will store the letter grade ('A', 'B', 'C', 'D', or 'F'). Initialize it as an empty string grade = "" or assign it within the conditionals.

3. Implement grading logic using if-elif-else:
   • if score >= 90: → Grade A (Excellent - top performers)
   • elif score >= 80: → Grade B (Good - above average)
   • elif score >= 70: → Grade C (Average - meets expectations)
   • elif score >= 60: → Grade D (Below Average - needs improvement)
   • else: → Grade F (Failing - below 60)

⚠️ Important Python Syntax:
• Python uses elif not else if
• Colons : are required after if/elif/else statements
• Indentation matters! Use 4 spaces for the code block under each condition
• No curly braces {} - Python uses indentation to define code blocks

4. Add pass/fail status: Any score of 60 or above is passing. Use a separate if-else to print "PASSED" or "FAILED".

Key Concept: The order of conditions matters! Check from highest to lowest. Once a condition is true, remaining elif/else blocks are skipped.

💡 Pro Tips:
• You can chain comparisons: 60 <= score < 70
• Ternary expression: status = "PASSED" if score >= 60 else "FAILED"
• Use and, or, not for logical operators (not &&, ||, !)

Expected Output:
Score: 85 Grade: B Status: PASSED

Requirements Checklist

Declare a variable "score" with a test value
Use if statement to check for grade A (score >= 90)
Include elif for grades B, C, D
Include else block for grade F
Print pass/fail status using a conditional
Code runs correctly
Output
# Click "Run Code" to execute your Python code
Hints & Tips
• Start with highest grade: if score >= 90: (don't forget the colon!)
• Chain conditions: elif score >= 80:
• Comparison operators: >= > < <= == !=
• Indentation: Use exactly 4 spaces under each if/elif/else
• Ternary shortcut: result = "A" if score >= 90 else "F"
• Logical operators: and, or, not (lowercase)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 3: Loops & FizzBuzz
Intermediate
Coding Challenge
Your Task: Implement the famous FizzBuzz challenge - one of the most common programming interview questions that tests your understanding of loops, conditionals, and the modulo operator.

Detailed Requirements:
1. Create a for loop that iterates from 1 to 20 (inclusive).
   Syntax: for i in range(1, 21):
   • range(1, 21) generates numbers 1 through 20 (end value is exclusive!)
   • range(start, stop) - stop is NOT included
   • Alternative: range(21) gives 0-20, but we want 1-20

2. For each number, apply these rules IN THIS EXACT ORDER:
   • If divisible by BOTH 3 AND 5 → print "FizzBuzz"
   • Elif divisible by 3 only → print "Fizz"
   • Elif divisible by 5 only → print "Buzz"
   • Else → print the number itself

Key Concept - Modulo Operator (%):
The % operator returns the remainder after division. If i % 3 == 0, then i divides evenly by 3 (no remainder), meaning i IS divisible by 3.
Examples: 9 % 3 = 0 (divisible), 10 % 3 = 1 (not divisible), 15 % 5 = 0 (divisible)

⚠️ Critical: You MUST check FizzBuzz (both 3 AND 5) FIRST! Why? Numbers like 15 are divisible by both 3 and 5. If you check "Fizz" first, you'll print "Fizz" instead of "FizzBuzz" because the Fizz condition will match before reaching FizzBuzz.

💡 Pro Tips:
• Python uses and not &&: if i % 3 == 0 and i % 5 == 0:
• You can also check i % 15 == 0 (since 15 = 3 × 5)
• Remember the colon after each if/elif/else!
• Don't forget proper indentation (4 spaces)

Expected Output (first 15 numbers):
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

Requirements Checklist

Create a for loop using range(1, 21)
Check FizzBuzz first (divisible by both 3 AND 5)
Check for Fizz (divisible by 3)
Check for Buzz (divisible by 5)
Print the number if none of the above
Output matches expected FizzBuzz pattern
Output
# Expected: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz
Hints & Tips
• For loop: for i in range(1, 21): (21 not included, so goes 1-20)
• Check both: if i % 3 == 0 and i % 5 == 0:
• Alternative: if i % 15 == 0: (15 = 3 × 5)
• Print number: print(i)
• Don't forget colons after if/elif/else!
• Use 4 spaces for indentation inside the loop
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below