diff --git a/basics/hello_world.py b/basics/01_hello_world/hello_world.py similarity index 100% rename from basics/hello_world.py rename to basics/01_hello_world/hello_world.py diff --git a/basics/variables_and_data_types.py b/basics/02_variables_and_data_types/variables_and_data_types.py similarity index 100% rename from basics/variables_and_data_types.py rename to basics/02_variables_and_data_types/variables_and_data_types.py diff --git a/basics/03_control_structures/condition_and_loops.py b/basics/03_control_structures/condition_and_loops.py new file mode 100644 index 0000000..2fcb943 --- /dev/null +++ b/basics/03_control_structures/condition_and_loops.py @@ -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.") diff --git a/basics/04_functions/simple_functions.py b/basics/04_functions/simple_functions.py new file mode 100644 index 0000000..3d44fef --- /dev/null +++ b/basics/04_functions/simple_functions.py @@ -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!") diff --git a/basics/05_basic_projects/simple_calculator.py b/basics/05_basic_projects/simple_calculator.py new file mode 100644 index 0000000..8076677 --- /dev/null +++ b/basics/05_basic_projects/simple_calculator.py @@ -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()