About These Assignments
This page contains practice assignments covering all the foundational concepts of Python programming. Each problem is designed to:
- Reinforce your understanding of Python fundamentals
- Apply concepts to real-world scenarios
- Build problem-solving skills step by step
- Prepare you for coding interviews
š” Tip: Try solving each problem on your own first. Click the "Show Solution" button only after you've attempted the problem. This approach will help you learn more effectively.
1. Character Set 3 Questions
print("Lowercase letters:", string.ascii_lowercase)
print("Uppercase letters:", string.ascii_uppercase)
print("Digits:", string.digits)
print("Punctuation:", string.punctuation)
# Output shows all characters Python supports
Python's character set includes letters (a-z, A-Z), digits (0-9), and special symbols. This exercise helps you understand what characters are valid in Python programs.
isalnum().
print(f"'{char}' is alphanumeric: {char.isalnum()}") # True
char = '#'
print(f"'{char}' is alphanumeric: {char.isalnum()}") # False
Python's character set includes letters and digits as alphanumeric characters. The isalnum() method checks if a character belongs to this set. This is used in validation systems.
letters = sum(1 for c in text if c.isalpha())
digits = sum(1 for c in text if c.isdigit())
special = len(text) - letters - digits
print(f"Letters: {letters}, Digits: {digits}, Special: {special}")
# Output: Letters: 5, Digits: 3, Special: 3
This problem demonstrates how to classify characters using Python's character set categories. This is used in text processing and data cleaning applications.
2. Tokens in Python 4 Questions
keyword module.
print("Python Keywords:")
print(keyword.kwlist)
print(f"Total keywords: {len(keyword.kwlist)}")
Keywords are reserved words in Python that have special meaning. They cannot be used as variable names. This exercise helps you identify and remember them.
isidentifier().
for name in names:
print(f"'{name}' is valid: {name.isidentifier()}")
# Output: my_var: True, 2var: False, var_123: True, if: False, hello_world: True
Identifiers are names given to variables, functions, and classes. They must start with a letter or underscore, followed by letters, digits, or underscores. This is used in code analysis tools.
from io import StringIO
code = "x = 10 + 20 * 3"
tokens = tokenize.generate_tokens(StringIO(code).readline)
for tok in tokens:
print(f"{tok.type} -> {tok.string}")
# Shows token types and their values
Python code is broken into tokens (keywords, identifiers, operators, literals, etc.) during compilation. This demonstrates how Python parses code. Used in code analysis and linters.
3. Variables & Identifiers 4 Questions
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
print(f"Name: {name}, Age: {age}, Height: {height}, Student: {is_student}")
Variables store different types of data. Python is dynamically typed ā you don't need to declare the type. This exercise demonstrates basic variable creation and usage.
b = 20
print(f"Before: a = {a}, b = {b}")
a, b = b, a # Swap using tuple unpacking
print(f"After: a = {a}, b = {b}")
# Output: Before: a = 10, b = 20
# After: a = 20, b = 10
Python's tuple unpacking allows elegant variable swapping without a temporary variable. This is a common Python idiom that demonstrates the power of Python's syntax.
4. Data Types 5 Questions
type().
y = 3.14
z = "Hello"
w = True
print(f"x is {type(x)}")
print(f"y is {type(y)}")
print(f"z is {type(z)}")
print(f"w is {type(w)}")
# Output: x is <class 'int'>, etc.
The type() function returns the data type of a variable. This is useful for debugging and understanding how Python handles different data types.
int_num = int(str_num)
float_num = 3.14
str_float = str(float_num)
int_to_float = float(int_num)
print(f"String to int: {int_num} ({type(int_num)})")
print(f"Float to string: '{str_float}' ({type(str_float)})")
print(f"Int to float: {int_to_float} ({type(int_to_float)})")
Type conversion (casting) is essential in Python programming. This exercise demonstrates how to convert between different data types using int(), float(), and str(). Used in data validation and user input processing.
5. Operators & Expressions 5 Questions
print(f"Sum: {a + b}")
print(f"Difference: {a - b}")
print(f"Product: {a * b}")
print(f"Quotient: {a / b}")
print(f"Remainder: {a % b}")
print(f"Floor Division: {a // b}")
print(f"Power: {a ** b}")
# Output: Sum: 19, Difference: 11, Product: 60, Quotient: 3.75, Remainder: 3, Floor Division: 3, Power: 50625
This problem demonstrates all arithmetic operators in Python. Used in everyday calculations in applications like calculators, billing systems, and scientific computing.
6. Constants 3 Questions
GRAVITY = 9.8
SPEED_OF_LIGHT = 299792458
radius = 5
area = PI * radius ** 2
print(f"Area of circle: {area:.2f}")
# Output: 78.54
Constants are values that don't change. By convention, constants are written in uppercase. This improves code readability and maintainability. Used in scientific and mathematical applications.
7. Assignment Statements 3 Questions
name = "Python"
# Multiple assignment
x, y, z = 10, 20, 30
print(f"Name: {name}, x: {x}, y: {y}, z: {z}")
Python supports both single and multiple assignment. Multiple assignment allows you to assign values to multiple variables in one line, improving code readability.
8. Input / Output 4 Questions
input() and print().
print(f"Hello, {name}! Welcome to Python!")
# Output: What's your name? (user types: Alice)
# Hello, Alice! Welcome to Python!
The input() function reads user input as a string, and print() displays output. This is the foundation of interactive programs.
9. Simple Python Scripts 4 Questions
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
perimeter = 2 * (length + width)
print(f"Area: {area:.2f}")
print(f"Perimeter: {perimeter:.2f}")
This complete script demonstrates input handling, calculation, and formatted output. Used in geometry, architecture, and design applications.
10. Namespace 3 Questions
def my_function():
x = 20 # Local variable
print(f"Local x: {x}")
my_function()
print(f"Global x: {x}")
# Output: Local x: 20
# Global x: 10
Global variables are accessible throughout the program, while local variables are only accessible within the function where they are defined. This is fundamental to understanding Python's scoping rules.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own code.
PYTHON INTRODUCTION PRACTICE
========================================
Name: Alice, Type: <class 'str'>
Age: 25, Type: <class 'int'>
Height: 5.6, Type: <class 'float'>
Is Student: True, Type: <class 'bool'>
What's your name? (user types: John)
Hello, John! Welcome to Python!
ā Write your solutions here!
š Related Tutorials
š What is Python
Learn about Python programming language
š Data Types
Review Python data types
š„ Input/Output
Learn about input and output in Python