-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Numbers 3.py
More file actions
100 lines (75 loc) · 4.32 KB
/
Copy pathPython Numbers 3.py
File metadata and controls
100 lines (75 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# في الأرقام، لما نريد الطباعة لا نضع ""، ويمكننا إضافة العمليات الحسابية مثل +، *، /، - وكذلك النسبة المئوية (%)
# In numbers, when we want to print, we do not put "", and we can add +, *, /, - and also modulus (%)
print(1) # Output: 1
print(1 + 1) # Output: 2
print(10 / 2) # Output: 5.0
print(100 * 5) # Output: 500
print(10 % 3) # Output: 1 (the remainder after division)
# يمكننا تخزين الأرقام باستخدام المتغيرات دون الحاجة إلى ""
# We can store numbers using variables without ""
my_num = 5
print(my_num) # Output: 5
# يمكننا طباعة جملة تحتوي على نص ورقم (int) باستخدام دالة str()
# We can print a sentence with both string and number (int) by using str() function,
# without using str() it will show an error and we cannot print both int and str without converting the numbers to string.
print(str(my_num), "is my favorite number") # Output: 5 is my favorite number
# دالة abs() في بايثون: تعني القيمة المطلقة
# In Python, abs() stands for absolute value.
# It means: convert any negative number to positive, and leave positive numbers unchanged.
num1 = -10
num2 = 20
print(abs(num1)) # Output: 10
print(abs(num2)) # Output: 20
# دالة pow() في بايثون: تُستخدم لرفع عدد إلى قوة عدد آخر
# In Python, pow(num1, num2) is used to raise a number to the power of another number.
# base ** exponent = pow
print(pow(3, 3)) # Output: 27
print(pow(num1, num2)) # Output: 1000000000000000000000000000
# **مثال 3**: رفع عدد سالب إلى **قوة فردية** (Negative number raised to an odd power)
# Example 3: Raising a negative number to an odd power
print(pow(-2, 3)) # Output: -8 (عدد سالب رفعنا للأس 3 ويظل سالب)
# **مثال 4**: رفع عدد سالب إلى **قوة زوجية** (Negative number raised to an even power)
# Example 4: Raising a negative number to an even power
print(pow(-2, 4)) # Output: 16 (عدد سالب رفعنا للأس 4 ويصبح موجب)
# ------------------------------------------
# دوال مدمجة في بايثون (Built-in Functions)
# These are built-in functions, you don't need to import anything to use them.
print(abs(-5)) # القيمة المطلقة → Output: 5
print(pow(2, 3)) # 2 أس 3 → Output: 8
print(len("Hello")) # طول النص → Output: 5
# ------------------------------------------
# دوال تحتاج مكتبة الرياضيات (math) (Functions from math module)
# You need to import the math module to use these functions. You can write "from math import *"
# or "from math import" the name of the function like floor, ceil, etc.
# لاستخدام دالة floor
# To use the floor function:
from math import floor
print(floor(4.9)) # Output: 4 → أقرب عدد صحيح أصغر أو يساوي الرقم
print(floor(7.1)) # Output: 7
print(9//2) # output = 4 \ instead of writting floor we can use //
# لاستخدام دالة ceil
# To use the ceil function:
# دالة ceil: تقرب الرقم إلى أقرب عدد صحيح أكبر أو يساوي الرقم
# ceil function: Rounds the number up to the nearest integer greater than or equal to the number.
from math import ceil
print(ceil(4.1)) # Output: 5
print(ceil(7.1)) # Output: 8
# يمكنك أيضًا كتابة
# You can also write it as:
from math import *
print(ceil(9.2)) # Output: 10
# لاستخدام دالة sqrt (الجذر التربيعي)
# To use the sqrt function:
print(sqrt(16)) # الجذر التربيعي للعدد 16 → Output: 4.0
# لاستخدام دالة factorial (المضروب)
# To use the factorial function:
from math import factorial
print(factorial(5)) # 5! = 5 × 4 × 3 × 2 × 1 → Output: 120
# ------------------------------------------
# ملاحظة: لا تنسى عند استخدام دوال من math مثل floor و sqrt و factorial
# You need to import math functions like floor, sqrt, and factorial
# باستخدام هذه الطريقة:
# Using this way:
# from math import * # لاستيراد كل شيء من مكتبة الرياضيات
# Or:
# import math # مع استخدام math. قبل الدوال