🎯 تمارين عملية: الدرس 4

الدوال ودوال اللامبدا - Functions & Lambda

📝 المهام (7 تمارين)

1Function Definition
2Return Value
3Lambda Function
4Default Parameter
5Multiple Returns
6String Format
7Lambda with Map
Create a function greet(name) that prints "Hello, [name]!"
أنشئ دالة greet(name) تطبع "Hello, [name]!"
💡 استخدم def greet(name): ثم print()
Create a function add(a, b) that returns the sum of a and b
أنشئ دالة add(a, b) ترجع مجموع a و b
💡 استخدم return a + b
Create a lambda function that squares a number (multiply by itself)
أنشئ دالة lambda تربع الرقم (تضربه في نفسه)
💡 استخدم square = lambda x: x * x
Create a function greet_with_title(name, title="Mr.") with a default parameter
أنشئ دالة greet_with_title(name, title="Mr.") مع معامل افتراضي
💡 استخدم def greet_with_title(name, title="Mr."):
Create a function get_min_max(numbers) that returns both minimum and maximum
أنشئ دالة get_min_max(numbers) ترجع الأصغر والأكبر معاً
💡 استخدم return min(numbers), max(numbers)
Create name="Ali" and age=20, then print using .format() method
أنشئ name="Ali" و age=20، ثم اطبع باستخدام .format()
💡 استخدم "My name is {} and I'm {} years old".format(name, age)
Use lambda with map() to double all numbers in [1, 2, 3, 4, 5]
استخدم lambda مع map() لمضاعفة جميع الأرقام في [1, 2, 3, 4, 5]
💡 استخدم list(map(lambda x: x * 2, [1, 2, 3, 4, 5]))
# Output will appear here... # النتيجة ستظهر هنا...