Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
63 changes: 63 additions & 0 deletions basics/03_control_structures/condition_and_loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Title: Python Control Structures Tutorial
Author: Python-Basics-to-Advanced Contributors
Difficulty: Beginner
Description: Detailed guide to if-else, loops, break, continue, and control flow in Python
Date: October 2025
"""

print("=== Python Control Structures Tutorial ===\n")

# =============================================================================
# 1. CONDITIONAL STATEMENTS
# =============================================================================

number = int(input("Enter a number: "))

if number > 0:
print("The number is positive.")
elif number == 0:
print("The number is zero.")
else:
print("The number is negative.")

# =============================================================================
# 2. FOR LOOP
# =============================================================================

print("\nCounting from 1 to 5:")
for i in range(1, 6):
print(i)

# =============================================================================
# 3. WHILE LOOP
# =============================================================================

print("\nCounting down from 5 to 1:")
count = 5
while count > 0:
print(count)
count -= 1

# =============================================================================
# 4. BREAK STATEMENT
# =============================================================================

print("\nFinding the first number divisible by 3 between 10 and 20:")
for num in range(10, 21):
if num % 3 == 0:
print(f"Found: {num}")
break

# =============================================================================
# 5. CONTINUE STATEMENT
# =============================================================================

print("\nPrinting numbers between 1 and 10 skipping multiples of 3:")
for num in range(1, 11):
if num % 3 == 0:
continue
print(num)

print("\n=== End of Control Structures Tutorial ===")
print("Practice tip: Modify the range and conditions to see different outputs and behaviors.")
95 changes: 95 additions & 0 deletions basics/04_functions/simple_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Title: Python Functions Tutorial
Author: Python-Basics-to-Advanced Contributors
Difficulty: Beginner
Description: Comprehensive guide to defining and using functions in Python with examples
Date: October 2025
"""

# =============================================================================
# 1. DEFINING FUNCTIONS
# =============================================================================

print("=== Python Functions Tutorial ===\n")

def greet(name):
"""
Greets a person by name.

Args:
name (str): The person's name.

Returns:
str: Greeting message.
"""
return f"Hello, {name}!"

print(greet("Alice"))

# =============================================================================
# 2. FUNCTION PARAMETERS AND DEFAULTS
# =============================================================================

def power(base, exponent=2):
"""
Calculates the power of a base number.

Args:
base (int or float): Base number.
exponent (int or float): Exponent, default is 2.

Returns:
int or float: Result of base raised to exponent.
"""
return base ** exponent

print(f"5^2 = {power(5)}")
print(f"3^3 = {power(3, 3)}")

# =============================================================================
# 3. RETURNING MULTIPLE VALUES
# =============================================================================

def arithmetic_ops(a, b):
"""
Performs basic arithmetic operations on two numbers.

Args:
a (int or float): First number.
b (int or float): Second number.

Returns:
tuple: Sum, difference, product, and quotient.
"""
sum_ = a + b
diff = a - b
prod = a * b
quo = a / b if b != 0 else None
return sum_, diff, prod, quo

s, d, p, q = arithmetic_ops(10, 5)
print(f"Sum: {s}, Difference: {d}, Product: {p}, Quotient: {q}")

# =============================================================================
# 4. LAMBDA (ANONYMOUS) FUNCTIONS
# =============================================================================

add = lambda x, y: x + y
square = lambda x: x * x

print(f"Add 3 + 4 = {add(3, 4)}")
print(f"Square of 6 = {square(6)}")

# =============================================================================
# 5. FUNCTION SCOPE AND LOCAL VARIABLES
# =============================================================================

def demonstrate_scope():
x = 10 # local variable
print(f"Inside function, x = {x}")

demonstrate_scope()
# print(x) # This would raise an error because x is local

print("\n=== End of Functions Tutorial ===")
print("Practice tip: Try creating your own functions and experimenting with parameters and returns!")
34 changes: 34 additions & 0 deletions basics/05_basic_projects/simple_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Title: Simple Calculator
Author: Python-Basics-to-Advanced
Difficulty: Beginner
Description: Basic calculator supporting +, -, *, / operations with input validation
"""

def calculator():
print("Simple Calculator")
try:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter numeric values.")
return

op = input("Choose operation (+, -, *, /): ")

if op == '+':
print(f"Result: {a + b}")
elif op == '-':
print(f"Result: {a - b}")
elif op == '*':
print(f"Result: {a * b}")
elif op == '/':
if b == 0:
print("Error: Cannot divide by zero")
else:
print(f"Result: {a / b}")
else:
print("Invalid operation")

if __name__ == "__main__":
calculator()
Loading