اليوم 4 — التحكم والحلقات
Day 4: Python Basics - Part 2
Tasks
-
Continue Learning Python Basics
- Control Flow Statements
- Understand how to use
if,else, andelifstatements to control the flow of your program. - Example:
CODE EXAMPLE
x = 10 if x > 0: print("x is positive") elif x == 0: print("x is zero") else: print("x is negative")
- Understand how to use
- Loops
- Learn the difference between
forandwhileloops and when to use each. - For Loop Example:
CODE EXAMPLE
for i in range(5): print(i) - While Loop Example:
CODE EXAMPLE
count = 0 while count < 5: print(count) count += 1
- Learn the difference between
- Functions
- Learn how to define and call functions.
- Understand function parameters and return values.
- Example:
CODE EXAMPLE
def greet(name): return "Hello, " + name print(greet("Alice")) - Explore recursion and scope in functions.
- Recursive Function Example:
CODE EXAMPLE
def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) print(factorial(5))
- Control Flow Statements
-
Write Small Programs to Solidify Your Understanding
- Implement small programs that make use of loops, functions, and data structures.
- Examples:
-
Factorial Program (Iterative and Recursive):
CODE EXAMPLEdef factorial_iterative(n): result = 1 for i in range(1, n + 1): result *= i return result def factorial_recursive(n): if n == 0 or n == 1: return 1 else: return n * factorial_recursive(n - 1) number = int(input("Enter a number: ")) print("Factorial (Iterative):", factorial_iterative(number)) print("Factorial (Recursive):", factorial_recursive(number)) -
Bubble Sort Program:
CODE EXAMPLEdef bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr numbers = [64, 34, 25, 12, 22, 11, 90] print("Sorted array is:", bubble_sort(numbers)) -
Word Count Program:
CODE EXAMPLEdef word_count(text): words = text.split() word_dict = {} for word in words: if word in word_dict: word_dict[word] += 1 else: word_dict[word] = 1 return word_dict sample_text = "hello world hello" print("Word count:", word_count(sample_text))
-
-
Explore Python Data Structures
- Learn about lists, tuples, sets, and dictionaries.
- Lists:
- Example:
CODE EXAMPLE
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
- Example:
- Tuples:
- Example:
CODE EXAMPLE
coordinates = (10, 20) print(coordinates[0])
- Example:
- Sets:
- Example:
CODE EXAMPLE
unique_numbers = {1, 2, 3, 3, 4} print(unique_numbers)
- Example:
- Dictionaries:
- Example:
CODE EXAMPLE
student_grades = {"Alice": 90, "Bob": 85, "Charlie": 92} for student, grade in student_grades.items(): print(student, grade)
- Example:
Resources
-
Python Loops and Control Flow
-
Python Functions
-
Data Structures
Summary
By the end of Day 4, you should have a solid understanding of Python loops, functions, and basic data structures. Practice these concepts through small programs to reinforce your learning. Use the resources provided to deepen your understanding and continue building a strong foundation in Python.
