العمليات الحسابية في بايثون: لغتك الأولى للتعامل مع الأرقام!
مرحباً بك في عالم البرمجة! تخيل أنك تستخدم آلة حاسبة قوية جداً، يمكنها أن تحسب أي شيء تريده. في بايثون، Operators العمليات الحسابية (Arithmetic Operators) هي الأدوات الأساسية التي تمنحك هذه القوة! فهي تتيح لك إجراء العمليات الرياضية الأساسية على الأرقام داخل برامجك، تماماً كما تفعل في الرياضيات، لكن بطريقة تفهمها الكمبيوتر. هيا نبدأ رحلتنا!
ما هي Operators العمليات الحسابية؟
Operators العمليات الحسابية هي رموز خاصة تستخدمها بايثون (ومعظم لغات البرمجة) لإجراء العمليات الرياضية على القيم. فبدلاً من استخدام كلمات مثل "جمع" أو "ضرب"، نستخدم رموزاً مختصرة مثل + و *. هذه الـ Operators تعمل على الـ Operators (Operands)، وهي القيم التي نجري عليها العملية.
مثال بسيط: في العملية 5 + 3
+هو الـ Operator.5و3هما the two Parameters (Operands).
أنواع Operators العمليات الحسابية في بايثون
دعنا نستعرض الـ Operators الأساسية واحداً تلو الآخر مع أمثلة عملية. تذكر: الكود يجب أن يكون بالإنجليزية، والتعليقات بالعربية للشرح.
1. Addition operator (+)
يستخدم لجمع رقمين أو أكثر معاً.
# Example of the addition operator (+)
result = 10 + 5
print(result) # Result: 15
# It can also be used with variables
price = 50
tax = 7
total = price + tax
print("The total amount is:", total) # Result: The total amount is: 57
2. Subtraction operator (-)
يستخدم لطرح رقم من رقم آخر.
# Example of the subtraction operator (-)
balance = 100
withdrawal = 45
new_balance = balance - withdrawal
print("Remaining balance:", new_balance) # Output: Remaining balance: 55
# It can be used to make a number negative
negative_number = -10
print(negative_number) # Output: -10
3. Multiplication operator (*)
يستخدم لضرب رقمين أو أكثر.
# Example of the multiplication operator (*)
area = 5 * 3 # Calculate rectangle area
print("The area is:", area) # Output: The area is: 15
# Multiplying variables
items = 4
cost_per_item = 25
total_cost = items * cost_per_item
print("Total cost:", total_cost) # Output: Total cost: 100
4. Division operator العادية (/)
يقسم الرقم الأول على الثاني، ويُرجع النتيجة كرقم عشري (float) دائماً، حتى لو كانت النتيجة عدداً صحيحاً.
# Example of the division operator (/)
result1 = 10 / 2
print(result1) # Result: 5.0 (Note .0 because the result is float)
result2 = 9 / 2
print(result2) # Result: 4.5
# Dividing quantities
total_students = 30
classrooms = 3
students_per_class = total_students / classrooms
print("Students per class:", students_per_class) # Result: Students per class: 10.0
5. Division operator الصحيحة (//)
يقسم الرقم الأول على الثاني، ويُرجع الجزء الصحيح فقط من النتيجة (يُقرب للأسفل تجاه سالب ما لا نهاية).
# Example of the floor division operator (//)
result1 = 10 // 3
print(result1) # Output: 3 (ignores the fraction 0.333...)
result2 = 9 // 2
print(result2) # Output: 4
# Example with negative numbers
result3 = -7 // 2
print(result3) # Output: -4 (note that it rounds down: -3.5 becomes -4)
6. Remainder operator أو الموديولوس (%)
يقسم الرقم الأول على الثاني، ويُرجع باقي عملية القسمة فقط.
# Example of the modulo operator (%)
remainder1 = 10 % 3
print(remainder1) # Result: 1 (because 10 ÷ 3 = 3 with remainder 1)
remainder2 = 15 % 4
print(remainder2) # Result: 3 (because 15 ÷ 4 = 3 with remainder 3)
# Common use: Checking if a number is even
number = 7
if number % 2 == 0: # If the remainder of dividing the number by 2 is zero
print("The number is even")
else:
print("The number is odd") # This statement will be executed
7. Operator الأسس (**)
يرفع الرقم الأول (الأساس) إلى قوة الرقم الثاني (الأس).
# Example of the exponentiation operator (**)
square = 5 ** 2 # 5 to the power of 2
print(square) # Output: 25
cube = 2 ** 3 # 2 to the power of 3
print(cube) # Output: 8
# Calculate the area of a square
side = 4
area_square = side ** 2
print("Square area:", area_square) # Output: Square area: 16
أولوية العمليات (Order of Operations)
ماذا لو كان لديك أكثر من عملية في سطر واحد؟ مثل 2 + 3 * 4، هل الناتج 20 أم 14؟
تتبع بايثون قاعدة رياضية عالمية تُعرف بـ PEMDAS لتحديد أولوية التنفيذ:
- Parentheses
(): الأقواس (الأولوية القصوى). - Exponents
**: الأسس. - Multiplication
*و Division/و//و%: الضرب والقسمة (بنفس المستوى، تُنفذ من اليسار لليمين). - Addition
+و Subtraction-: الجمع والطرح (بنفس المستوى، تُنفذ من اليسار لليمين).
# Examples of operator precedence
result1 = 2 + 3 * 4 # Multiplication first: 3*4=12, then addition: 2+12
print(result1) # Result: 14
result2 = (2 + 3) * 4 # Parentheses first: (2+3)=5, then multiplication: 5*4
print(result2) # Result: 20
result3 = 10 - 2 ** 3 / 4 # Exponents first: 2**3=8, then division: 8/4=2, then subtraction: 10-2
print(result3) # Result: 8.0
خلاصة الدرس
أصبحت الآن تمتلك أدواتك الحسابية الأولى في بايثون! تعلمت الـ Operators السبعة الأساسية: الجمع (+)، الطرح (-)، الضرب (*)، القسمة العادية (/)، القسمة الصحيحة (//)، Remainder operator (%)، و Operator الأسس (**). كما فهمت قاعدة PEMDAS المهمة التي تحكم أولوية تنفيذ هذه العمليات عندما تجتمع معاً. هذه هي اللبنة الأساسية لأي برنامج يقوم بأي نوع من الحسابات، بسيطة كانت أو معقدة.
اختبر معلوماتك
أسئلة قصيرة تتأكد بها من فهمك للدرس قبل أن تنتقل.
التعليقات
شاركنا رأيك أو أسئلتك حول هذا المحتوى