SlideShare a Scribd company logo
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
1
Chapter 2 Elementary Programming
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫وهياكل‬ ‫الخوارزميات‬ ‫هي‬ ‫ما‬
‫البيانات؟‬

‫الخوارزميات‬
‫في‬ ‫ي‬ ‫والت‬ ‫ة‬ ‫والمنطقي‬ ‫ة‬ ‫الرياضي‬ ‫ب‬ ‫التراكي‬ ‫ن‬ ‫م‬ ‫ة‬ ‫مجموع‬
‫أو‬ ‫ج‬ ‫لمنت‬ ‫األداء‬ ‫ين‬ ‫تحس‬ ‫و‬ ‫أ‬ ‫ل‬ ‫مشك‬ ‫ل‬ ‫لح‬ ‫تهدف‬ ‫ة‬ ‫النهاي‬
‫هي‬ ‫وب‬‫الحاس‬ ‫م‬‫عل‬ ‫ي‬‫ف‬ ‫الخوارزميات‬ ،‫ة‬‫المعالج‬ ‫ة‬‫طريق‬
‫واحدة‬ ‫تنفيذها‬ ‫يتم‬ ‫التي‬ ‫الواضحة‬ ‫التعليمات‬ ‫من‬ ‫مجموعة‬
‫لعالم‬ ‫بة‬ ‫نس‬ ‫ظ‬ ‫واللف‬ ،‫ا‬ ‫م‬ ‫ة‬ ‫مشكل‬ ‫ل‬ ‫لح‬ ‫اآلخرى‬ ‫و‬ ‫تل‬
.‫الخوارزمي‬ ‫موسى‬ ‫بن‬ ‫محمد‬ ‫الرياضيات‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫وهياكل‬ ‫الخوارزميات‬ ‫هي‬ ‫ما‬
‫البيانات؟‬

‫البيانات‬ ‫هياكل‬
‫فى‬ ‫التخزين‬ ‫عملية‬ ‫تصميم‬ ‫على‬ ‫تعتمد‬ ‫البيانات‬ ‫هياكل‬
‫نوع‬ ‫ليدعم‬ ،‫معقدة‬ ‫ر‬‫وغي‬ ‫فعالة‬ ‫بطريقة‬ ‫بيوتر‬‫الكم‬
‫او‬ ‫البرمجة‬ ‫شيفرة‬ ‫ل‬‫لتعم‬ ‫ا‬‫تخزينه‬ ‫المراد‬ ‫بيانات‬‫ال‬
.‫كفاءة‬ ‫وأكثر‬ ‫اسرع‬ ‫بطريقة‬ ‫الخوارزميات‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫وهياكل‬ ‫الخوارزميات‬ ‫نتعلم‬ ‫لماذا‬
‫البيانات؟‬
)1
‫تفكيرك‬ ‫طريقة‬ ‫تحسن‬
o
‫تكتبه‬ ‫الذي‬ ‫البرمجي‬ ‫للكود‬ ‫ادراكا‬ ‫أكثر‬ ‫تصبح‬ ‫حيث‬
)2
‫وتنظيمها‬ ‫األفكار‬ ‫وضوح‬
)3
‫مصادر‬ ‫وأقل‬ ‫بفاعلية‬ ‫المشاكل‬ ‫حل‬
)4
‫العمل‬ ‫مقابالت‬ ‫في‬ ‫النجاح‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهم‬

– ‫القوائم‬
Lists

– ‫المتصلة‬ ‫القوائم‬
Linked Lists

– ‫المكدسات‬
Stacks

– ‫الطوابير‬
Queues

– ‫الشجرات‬
Trees

– ‫البحث‬
Searching

– ‫الفرز‬
Sorting
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
‫البرنامج‬
Program Code
‫بيانات‬
Data
‫األوامر‬
Instructions
‫من‬ ‫يتكون‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬

‫مثال‬
:
Method
Add(a, b)
{
return a + b;
}
Data – ‫البيانات‬:
•a
•b
Instructions – ‫األوامر‬:
•Return
•+
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
RAM
Storage Memory
Code
(Instructions)
Data
‫المستخدمة‬ ‫بيانات‬55‫وال‬ ‫برنامج‬55‫ال‬
‫التخزين‬ ‫وحدة‬ ‫ل‬5‫خ‬‫دا‬ ‫ة‬5‫ن‬‫مخز‬ ‫تكون‬
) (
‫ديسك‬ ‫هارد‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
( ‫ل‬5‫ي‬‫لتشغ‬
run
)
‫ال‬ ‫يقوم‬ ‫البرنامج‬
CPU
‫الى‬ ‫الكود‬ ‫خ‬5‫س‬‫ن‬ ‫و‬5‫أ‬ ‫ل‬5‫ي‬‫بتحم‬
( ‫العشوائية‬ ‫الذاكرة‬
RAM
)
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
Code
(Instructions)
‫ال‬ ‫يقوم‬ ‫الكود‬ ‫تشغيل‬ ‫أثناء‬
CPU
( ‫الالزمة‬ ‫بيانات‬5‫ل‬‫ا‬ ‫ل‬5‫ي‬‫بتحم‬
Data
)
‫أيضا‬ ‫العشوائية‬ ‫الذاكرة‬ ‫الى‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
Code
(Instructions)
Data
5‫ل‬‫العم‬ ‫برنامج‬55‫ال‬ ‫تطيع‬55‫يس‬ ‫االن‬
‫أفضل‬ ‫وفاعلية‬ ‫بسرعة‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
Code
(Instructions)
Data
‫البيانات‬ ‫تدعاء‬55‫باس‬ ‫الكود‬ ‫يقوم‬
‫العشوائية‬ ‫الذاكرة‬ ‫ي‬55‫ف‬ ‫ة‬55‫المخزن‬
‫البرنامج‬ ‫عمل‬ ‫اثناء‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
Code
(Instructions)
Data
‫البيانات‬ ‫ل‬5‫هياك‬ ‫ة‬5‫اهمي‬ ‫ن‬5‫تكم‬ ‫ا‬5‫هن‬
(
Data Structure
)
‫خالل‬ ‫من‬
‫في‬ ‫بيانات‬5‫ل‬5‫ا‬ ‫ن‬5‫ي‬‫تخز‬ ‫ة‬5‫ي‬‫وال‬ ‫ة‬5‫ق‬‫طري‬
‫العشوائية‬ ‫الذاكرة‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬
CPU
Storage Memory
Code
(Instructions)
Data
Code
(Instructions)
Data
( ‫البيانات‬ ‫ل‬5‫ك‬‫هيا‬ ‫ف‬5‫ي‬‫تعر‬ ‫ن‬5‫ك‬‫يم‬
Data
Structure
)
‫طريقة‬ ‫ا‬55‫أنه‬ ‫ى‬55‫عل‬
‫المستخدمة‬ ‫بيانات‬5‫ال‬ ‫ن‬5‫وتخزي‬ ‫ب‬5‫لترتي‬
‫أكثر‬ ‫ل‬5‫بشك‬ ‫تخدامها‬5‫باس‬ ‫مح‬5‫تس‬ ‫ة‬5‫بالي‬
‫من‬ ‫برنامج‬55‫ال‬ ‫ن‬55‫تمك‬ ‫ا‬55‫كم‬ ،‫ة‬55‫فعالي‬
‫أقصر‬ ‫ووقت‬ ‫بسهولة‬ ‫اليها‬ ‫الوصول‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
15
Introducing Programming with an
Example
Listing 2.1 Computing the Area of a
Circle
This program computes the area of the
circle.
ComputeArea
Algorithms and Data Structures
Algorithms and Data Structures
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬
#
#
02
02
‫الذاكرة‬ ‫تخصيص‬ –
‫الذاكرة‬ ‫تخصيص‬ –
‫الذاكرة‬ ‫تخصيص‬
‫الذاكرة‬ ‫تخصيص‬
(Memory Allocation)
(Memory Allocation)
•
)‫وأوامر‬ ‫(بيانات‬ ‫ما‬ ‫لعملية‬ ‫الذاكرة‬ ‫عنوان‬ ‫مساحة‬ ‫لتخصيص‬ ‫إجراء‬
‫لتنفيذها‬
•
‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬
.1
‫الثابت‬ ‫الذاكرة‬ ‫تخصيص‬
Static Memory Allocation
.2
‫الديناميكي‬ ‫الذاكرة‬ ‫تخصيص‬
Dynamic Memory Allocation
‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬
‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬
•
‫الثابت‬ ‫الذاكرة‬ ‫تخصيص‬
Static Memory Allocation
o
( ‫م‬5‫المترج‬ ‫يقوم‬ ‫ا‬5‫عندم‬ ‫ت‬5‫الثاب‬ ‫الذاكرة‬ ‫يص‬5‫تخص‬ ‫ذ‬5‫ي‬‫تنف‬ ‫م‬5‫يت‬
Compiler
‫ع‬5‫بتجمي‬ )
‫ر‬5‫العناص‬ ‫ملفات‬ ‫ع‬5‫جمي‬ ‫ج‬5‫بدم‬ ‫ط‬5‫الراب‬ ‫ويقوم‬ ،‫ر‬5‫العناص‬ ‫ملفات‬ ‫وإنشاء‬ ‫برنامج‬5‫ال‬
‫للتنفيذ‬ ‫قابل‬ ‫واحد‬ ‫ملف‬ ‫وإنشاء‬ ‫هذه‬
‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬
‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬
•
‫الديناميكي‬ ‫الذاكرة‬ ‫تخصيص‬
Dynamic Memory Allocation
o
‫يص‬5‫ص‬‫تخ‬ ‫م‬5‫ت‬‫ي‬ ،‫ا‬5‫ن‬‫ه‬ ،‫برنامج‬5‫ل‬‫ا‬ ‫ذ‬5‫ي‬‫تنف‬ ‫أثناء‬ ‫ي‬5‫ك‬‫الدينامي‬ ‫الذاكرة‬ ‫يص‬5‫ص‬‫تخ‬ ‫ذ‬5‫ي‬‫تنف‬ ‫م‬5‫ت‬‫ي‬
،‫البرنامج‬ ‫تشغيل‬ ‫أثناء‬ ‫مرة‬ ‫ألول‬ ‫استخدامها‬ ‫يتم‬ ‫عندما‬ ‫البرنامج‬ ‫لعناصر‬ ‫الذاكرة‬
‫ص‬5‫يخص‬ ‫و‬5‫فه‬ ‫لذا‬ ،‫ل‬5‫التشغي‬ ‫ت‬5‫وق‬ ‫ي‬5‫ف‬ ‫ة‬5‫المطلوب‬ ‫بيانات‬5‫لل‬ ‫ي‬5‫الفعل‬ ‫م‬5‫الحج‬ ‫عرف‬ُ
‫ي‬
‫الذاكرة‬ ‫إهدار‬ ‫من‬ ‫يقلل‬ ‫وبالتالي‬ ‫للبرنامج‬ ‫الدقيقة‬ ‫الذاكرة‬ ‫مساحة‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
CPU
Storage
Memory
Code
(Instructions)
Data
( ‫ة‬5‫ي‬‫لعشوائ‬5‫ا‬ ‫الذاكرة‬ ‫م‬5‫س‬‫تق‬
RAM
)
‫أقسام‬ ‫ثالثة‬ ‫الى‬
Code
Stack
Heap
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
CPU
Storage
Memory
Code
(Instructions)
Data
‫جزء‬ ‫في‬ ‫البرنامج‬ ‫كود‬ ‫تحميل‬ ‫يتم‬
‫الذاكرة‬ ‫في‬ ‫الكود‬
Code
Code
Stack
Heap
Code
(Instructions)
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
CPU
Storage
Memory
Code
(Instructions)
Data
‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬:
main()
{
int n1, n2;
float Avg;
Avg = (n1+n2) / 2;
}
Code
Stack
Heap
Code
(Instructions)
( ‫البيانات‬ ‫نوع‬ ‫معروف‬ ‫يكون‬ ‫عندما‬
Primitive Data Type
)
‫للمتغيرات‬ ‫المحجوزة‬ ‫لمساحة‬5‫ا‬ ‫معروف‬ ‫يكون‬
n1 n2
Avg
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
CPU
Storage
Memory
Code
(Instructions)
Data
‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬:
main()
{
int n1, n2;
n1 = 5; n2 = 10;
float Avg;
Avg = (n1+n2) / 2;
}
Code
Stack
Heap
Code
(Instructions)
‫يتم‬ ‫معروفة‬ ‫المحجوزة‬ ‫والمساحة‬ ‫معروف‬ ‫البيانات‬ ‫نوع‬ ‫أن‬ ‫بما‬
( ‫قسم‬ ‫في‬ ‫المساحة‬ ‫حجز‬
Stack
‫الذاكرة‬ ‫من‬ )
n1 n2
Avg
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬
CPU
Storage
Memory
Code
(Instructions)
Data
‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬:
main()
{
int n1, n2;
n1 = 5; n2 = 10;
float Avg;
Avg = (n1+n2) / 2;
}
Code
Stack
Heap
Code
(Instructions)
( ‫قسم‬ ‫في‬ ‫للمتغيرات‬ ‫المساحة‬ ‫حجز‬ ‫يتم‬
Stack
‫في‬ ‫الذاكرة‬ ‫من‬ )
( ‫ال‬ ‫مرحلة‬
Compilation
‫البرنامج‬ ‫تنفيذ‬ ‫مرحلة‬ ‫قبل‬ )
5 10
n1 n2
7.5
Avg
‫الذاكرة‬ ‫تخصيص‬ ‫مثال‬
‫الذاكرة‬ ‫تخصيص‬ ‫مثال‬
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory
‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬:
main()
{
int i;
float * ptr;
cout << “Enter array size 
n”;
cin >> i;  ex: 5
ptr = new float [i];
delete [] ptr;
return 0;
} Code
Stack
Heap
Code
*
i ptr
Main
( ‫في‬ ‫الديناميكي‬ ‫كرة‬5‫ا‬‫الذ‬ ‫حجز‬ ‫يوضح‬ ‫المثال‬ ‫هذا‬
Heap
‫من‬ )
( ‫البرنامج‬ ‫تنفيذ‬ ‫مرحلة‬ ‫في‬ ‫الذاكرة‬
Run
)
Algorithms and Data Structures
Algorithms and Data Structures
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬
‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬
#
#
03
03
‫تصنيفات‬ –
‫تصنيفات‬ –
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
‫البيانات‬ ‫هياكل‬
o
.‫فعال‬ ‫بشكل‬ ‫استخدامها‬ ‫ليتم‬ ‫الحاسوب‬ ‫في‬ ‫البيانات‬ ‫وتنظيم‬ ‫لترتيب‬ ‫خاصة‬ ‫طريقة‬
o
.‫واحد‬ ‫اسم‬ ‫تحت‬ ‫البعض‬ ‫بعضها‬ ‫مع‬ ‫تخزينها‬ ‫يتم‬ ‫البيانات‬ ‫عناصر‬ ‫من‬ ‫مجموعة‬
•
( ‫المصفوفة‬ :‫مثال‬
Array
)
.‫النوع‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫عدة‬ ‫يحتوي‬ –
•
– ‫أيضا‬ ‫عليها‬ ‫يطلق‬
Abstract Data Type (ADT)
o
‫البيانات‬ ‫تخزين‬ ‫طريقة‬
o
‫البيانات‬ ‫هذه‬ ‫على‬ ‫تتم‬ ‫التي‬ ‫والعمليات‬ ‫األوامر‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
( ‫مثال‬
ADT
– ‫الطابور‬ :)
Queue
Counter
Front
or
Head
Rear
or
Tail
enqueue dequeue
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
‫البيانات‬ ‫هياكل‬ ‫استخدام‬ ‫من‬ ‫الهدف‬
o
:‫على‬ ‫يساعد‬
•
‫فعال‬ ‫بشكل‬ ‫البيانات‬ ‫تخزين‬
•
‫ممكن‬ ‫وقت‬ ‫أقل‬ ‫في‬ ‫البيانات‬ ‫استرجاع‬
•
‫فعال‬ ‫بشكل‬ ‫البيانات‬ ‫تعديل‬
•
‫البرنامج‬ ‫تصميم‬ ‫أثناء‬
o
( ‫البيانات‬ ‫هياكل‬ ‫من‬ ‫نوع‬ ‫أنسب‬ ‫ختيار‬5‫ا‬ ‫هو‬ ‫التحدي‬
Data Structure
‫ما‬ ‫مشكلة‬ ‫لحل‬ )
•
( ‫ال‬ ‫استخدام‬ ‫يفضل‬ ‫ال‬ ‫الطالب‬ ‫عالمات‬ ‫الدخال‬ :‫مثال‬
Array
.‫ثابت‬ ‫حجمها‬ ‫الن‬ )
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
‫البيانات‬ ‫هياكل‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
‫الوجود‬ ‫حسب‬ ‫التصنيف‬
o
‫المادية‬ ‫البيانات‬ ‫هياكل‬
(
Physical
)
•
‫مستقل‬ ‫بشكل‬ ‫انشائها‬ ‫يمكن‬
o
( ‫المصفوفات‬
Array
)
o
( ‫المتصلة‬ ‫القوائم‬
Linked List
)
o
‫المنطقية‬ ‫البيانات‬ ‫هياكل‬
(
Logical
)
•
‫مستقل‬ ‫بشكل‬ ‫انشائها‬ ‫يمكن‬ ‫ال‬
o
( ‫المكدسات‬
Stacks
)
o
( ‫الطوابير‬
Queues
)
o
( ‫الشجرات‬
Trees
)
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
‫الذاكرة‬ ‫حجز‬ ‫حسب‬ ‫التصنيف‬
o
( ‫المساحة‬ ‫ثابتة‬ ‫البيانات‬ ‫هياكل‬
Static
)
•
‫ثابتة‬ ‫مساحة‬ ‫تحجز‬
•
( ‫المصفوفات‬ ‫مثل‬
Array
)
o
( ‫المساحة‬ ‫ديناميكية‬ ‫البيانات‬ ‫هياكل‬
Dynamic
)
•
‫الحاجة‬ ‫حسب‬ ‫المساحة‬ ‫تتغير‬
•
( ‫المتصلة‬ ‫القوائم‬ ‫مثل‬
Linked List
)
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬
•
5
‫ل‬‫التمثي‬ ‫حسب‬ ‫التصنيف‬
o
( ‫الخطية‬ ‫البيانات‬ ‫هياكل‬
Linear
)
•
( ‫المصفوفات‬
Array
)
•
( ‫المتصلة‬ ‫القوائم‬
Linked List
)
•
( ‫المكدسات‬
Stack
)
•
( ‫الطابور‬
Queue
)
o
( ‫الخطية‬ ‫غير‬ ‫البيانات‬ ‫هياكل‬
Non-Linear
)
•
( ‫الشجرات‬
Trees
)
•
( ‫البياني‬ ‫الرسم‬
Graph
)
‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬
‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬
•
– 5
‫ر‬‫العناص‬ ‫جميع‬ ‫على‬ ‫المرور‬
Traversing
o
.‫العنصر‬ ‫هذا‬ ‫على‬ ‫ما‬ ‫عملية‬ ‫وتنفيذ‬ ‫عنصر‬ ‫كل‬ ‫زيارة‬ ‫أو‬ ‫الوصول‬
•
- ‫البحث‬
Searching
o
.‫البيانات‬ ‫تركيب‬ ‫في‬ ‫مخزن‬ ‫معين‬ ‫عنصر‬ ‫موقع‬ ‫تحديد‬ ‫أو‬ ‫البحث‬
•
– ‫االضافة‬
Insertion
o
.‫موجود‬ ‫بيانات‬ ‫تركيب‬ ‫الى‬ ‫جديد‬ ‫عنصر‬ ‫اضافة‬
‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬
‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬
•
– ‫الحذف‬
Deletion
o
.‫موجود‬ ‫بيانات‬ ‫تركيب‬ ‫من‬ ‫عنصر‬ ‫ازالة‬ ‫أو‬ ‫حذف‬
•
– ‫الترتيب‬
Sorting
o
.‫منطقية‬ ‫بطريقة‬ ‫البيانات‬ ‫تركيب‬ ‫في‬ ‫العناصر‬ ‫ترتيب‬
•
.‫تنازلي‬ ‫أو‬ ‫تصاعدي‬ ‫ترتيبها‬ ‫يمكن‬
•
– ‫الدمج‬
Merging
o
.‫واحد‬ ‫في‬ ‫وجمعها‬ ‫بيانات‬ ‫تركيب‬ ‫من‬ ‫أكثر‬ ‫من‬ ‫العناصر‬ ‫ودمج‬ ‫ضم‬
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
36
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius",
radius, "is", area)
20
radius
Assign 20 to radius
animation
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
37
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius",
radius, "is", area)
20
radius
Assign result to
area
animation
1256.636
area
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
38
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius",
radius, "is", area)
20
radius
print a message to
the console
animation
1256.636
area
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
39
Reading Input from the Console
1. Use the input function
variable = input("Enter a string: ")
2. Use the float and int function to convert a string to a float
or int.
var = float(stringVariable)
var = int(stringVariable)
ComputeAreaWithConsoleInput
ComputeAverage
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
40
Identifiers
 An identifier is a sequence of characters that
consists of letters, digits, underscores (_), and
asterisk (*).
 An identifier must start with a letter or an
underscore. It cannot start with a digit.
 An identifier cannot be a reserved word. (See
Appendix A, "Python Keywords," for a list of
reserved words.) Reserved words have special
meanings in Python, which we will later discuss.
 An identifier can be of any length.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
41
Variables
# Compute the first area
radius = 1.0
area = radius * radius * 3.14159
print("The area is ", area,
" for radius ", radius)
# Compute the second area
radius = 2.0
area = radius * radius * 3.14159
print("The area is ", area,
" for radius ", radius)
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
42
Expression
x = 1 # Assign 1 to variable x
radius = 1.0 # Assign 1.0 to variable radius
# Assign the value of the expression to x
x = 5 * (3 / 2) + 3 * 2
x = y + 1 # Assign the addition of y and 1 to x
area = radius * radius * 3.14159 # Compute area
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
43
Assignment Statements
x = 1 # Assign 1 to x
x = x + 1
i = j = k = 1
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
44
Simultaneous Assignment
var1, var2, ..., varn = exp1, exp2, ..., expn
x, y = y, x # Swap x with y
ComputeAverageWithSimultaneousAssignment
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
45
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
46
Named Constants
The value of a variable may change during the
execution of a program, but a named constant or
simply constant represents permanent data that
never changes. Python does not have a special
syntax for naming constants. You can simply
create a variable to denote a constant. To
distinguish a constant from a variable, use all
uppercase letters to name a constant.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
47
Numerical Data Types
 integer: e.g., 3, 4
 float: e.g., 3.0, 4.0
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
48
Numeric Operators
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0 – 0.1 33.9
* Multiplication 300 * 30 9000
/ Float Division 1 / 2 0.5
// Integer Division 1 // 2 0
** Exponentiation 4 ** 0.5 2.0
% Remainder 20 % 3 2
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
49
The % Operator
12
4
3
12
0
7
3
2
6
1
26
8
3
24
2 Remainder
Quotient
20
13
1
13
7
Dividend
Divisor
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
50
Remainder Operator
Remainder is very useful in programming. For example, an
even number % 2 is always 0 and an odd number % 2 is always
1. So you can use this property to determine whether a number
is even or odd. Suppose today is Saturday and you and your
friends are going to meet in 10 days. What day is in 10
days? You can find that day is Tuesday using the following
expression:
Saturday is the 6th
day in a week
A week has 7 days
After 10 days
The 2nd
day in a week is Tuesday
(6 + 10) % 7 is 2
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
51
Problem: Displaying Time
Write a program that obtains minutes and
remaining seconds from seconds.
DisplayTime
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
52
Overflow
When a variable is assigned a value that is too
large (in size) to be stored, it causes overflow.
For example, executing the following
statement causes overflow.
>>> 245.0 ** 1000
OverflowError: 'Result too large'
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
53
Underflow
When a floating-point number is too small (i.e., too
close to zero) to be stored, it causes underflow.
Python approximates it to zero. So normally you
should not be concerned with underflow.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
54
Scientific Notation
Floating-point literals can also be specified in
scientific notation, for example, 1.23456e+2,
same as 1.23456e2, is equivalent to 123.456, and
1.23456e-2 is equivalent to 0.0123456. E (or e)
represents an exponent and it can be either in
lowercase or uppercase.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
55
Arithmetic Expressions
)
9
4
(
9
)
)(
5
(
10
5
4
3
y
x
x
x
c
b
a
y
x 







is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
56
How to Evaluate an Expression
Though Python has its own way to evaluate an
expression behind the scene, the result of a Python
expression and its corresponding arithmetic expression
are the same. Therefore, you can safely apply the
arithmetic rule for evaluating a Python expression.
3 + 4 * 4 + 5 * (4 + 3) - 1
3 + 4 * 4 + 5 * 7 – 1
3 + 16 + 5 * 7 – 1
3 + 16 + 35 – 1
19 + 35 – 1
54 - 1
53
(1) inside parentheses first
(2) multiplication
(3) multiplication
(4) addition
(6) subtraction
(5) addition
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
57
Augmented Assignment Operators
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
//= i //= 8 i = i // 8
%= i %= 8 i = i % 8
**= i **= 8 i = i ** 8
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
58
Type Conversion and Rounding
datatype(value)
i.e., int(4.5) => 4
float(4) => 4.0
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
59
Problem: Keeping Two Digits After
Decimal Points
Write a program that displays the sales tax with two
digits after the decimal point.
SalesTax
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
60
Problem: Displaying Current Time
Write a program that displays current time in GMT in the
format hour:minute:second such as 1:45:19.
The time.time() function returns the current time in seconds
with millisecond precision since the midnight, January 1,
1970 GMT. (1970 was the year when the Unix operating
system was formally introduced.) You can use this function
to obtain the current time, and then compute the current
second, minute, and hour as follows.
Elapsed
time
Unix epoch
01-01-1970
00:00:00 GMT
Current time
Time
time.time()
ShowCurrentTime
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
61
Problem:
Computing Loan Payments
This program lets the user enter the interest
rate, number of years, and loan amount, and
computes monthly payment and total
payment.
12
)
1
(
1
1 




ars
numberOfYe
erestRate
monthlyInt
erestRate
monthlyInt
loanAmount
ment
monthlyPay
ComputeLoan
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
62
Case Study: Computing Distances
This program prompts the user to enter two
points, computes their distance, and displays
the distance.
2
1
2
2
1
2 )
(
)
( y
y
x
x 


ComputeDistance
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
63
Case Study: Computing Distances
This program prompts the user to enter two
points, computes their distance, and displays
the points and their distances in graphics.
ComputeDistanceGraphics

More Related Content

Similar to python Elementary Programming algorithem (20)

مقدمة_في_الحاسوب.pptx
مقدمة_في_الحاسوب.pptx
anasalsaaydeh
 
مشروع مكونات الحاسب
مشروع مكونات الحاسب
manar gamel
 
مقدمة_في_الحاسب شرح مختصر ومفيد للمبتدئين.pptx
مقدمة_في_الحاسب شرح مختصر ومفيد للمبتدئين.pptx
KHALIDahmed579182
 
، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها، وتصبح مفهومة ...
، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها، وتصبح مفهومة ...
OsamahAli7
 
المحاضرة الاولى، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها...
المحاضرة الاولى، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها...
OsamahAli7
 
المحاضرة الاولىالحاسوب :هو عبارة عن آلة الكترونية تقوم بمعالجة البيانات وتخزي...
المحاضرة الاولىالحاسوب :هو عبارة عن آلة الكترونية تقوم بمعالجة البيانات وتخزي...
OsamahAli7
 
القرص الصلب
القرص الصلب
Baba Habeby
 
هو جهاز إلكتروني يقوم باستقبال البيانات وتخزينها ، ومن ثم إجراء مجموعة من الع...
هو جهاز إلكتروني يقوم باستقبال البيانات وتخزينها ، ومن ثم إجراء مجموعة من الع...
TheGame9874
 
java programming language - Lecture7.pdf
java programming language - Lecture7.pdf
fatmaolowa
 
Hardware
Hardware
aishah9200
 
introduction to database system_lecture1.pptx
introduction to database system_lecture1.pptx
AsiaElamin1
 
مبادئ الحاسب والمعلومات - 1ث
مبادئ الحاسب والمعلومات - 1ث
kr3sh
 
7459مكونات الحاسوب
7459مكونات الحاسوب
maryamassaf
 
First chapter
First chapter
Amin Alam
 
Ta3lime.com f919257881 (1)
Ta3lime.com f919257881 (1)
shari3a
 
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
Hanaa Ahmed
 
أوامر دوس Dos
أوامر دوس Dos
Safa Tayeb
 
Computer school-books-1st-preparatory-1st-term-khawagah-2019-9
Computer school-books-1st-preparatory-1st-term-khawagah-2019-9
khawagah
 
مقدمة عن الفيجوال بيسك 9-2019
مقدمة عن الفيجوال بيسك 9-2019
Amr Rashed
 
Computer 3rd-preparatory-second-term-khawagah-6
Computer 3rd-preparatory-second-term-khawagah-6
khawagah
 
مقدمة_في_الحاسوب.pptx
مقدمة_في_الحاسوب.pptx
anasalsaaydeh
 
مشروع مكونات الحاسب
مشروع مكونات الحاسب
manar gamel
 
مقدمة_في_الحاسب شرح مختصر ومفيد للمبتدئين.pptx
مقدمة_في_الحاسب شرح مختصر ومفيد للمبتدئين.pptx
KHALIDahmed579182
 
، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها، وتصبح مفهومة ...
، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها، وتصبح مفهومة ...
OsamahAli7
 
المحاضرة الاولى، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها...
المحاضرة الاولى، خام. المعلومات (INFORMATION):هي البيانات بعد ما تتم معالجتها...
OsamahAli7
 
المحاضرة الاولىالحاسوب :هو عبارة عن آلة الكترونية تقوم بمعالجة البيانات وتخزي...
المحاضرة الاولىالحاسوب :هو عبارة عن آلة الكترونية تقوم بمعالجة البيانات وتخزي...
OsamahAli7
 
القرص الصلب
القرص الصلب
Baba Habeby
 
هو جهاز إلكتروني يقوم باستقبال البيانات وتخزينها ، ومن ثم إجراء مجموعة من الع...
هو جهاز إلكتروني يقوم باستقبال البيانات وتخزينها ، ومن ثم إجراء مجموعة من الع...
TheGame9874
 
java programming language - Lecture7.pdf
java programming language - Lecture7.pdf
fatmaolowa
 
introduction to database system_lecture1.pptx
introduction to database system_lecture1.pptx
AsiaElamin1
 
مبادئ الحاسب والمعلومات - 1ث
مبادئ الحاسب والمعلومات - 1ث
kr3sh
 
7459مكونات الحاسوب
7459مكونات الحاسوب
maryamassaf
 
First chapter
First chapter
Amin Alam
 
Ta3lime.com f919257881 (1)
Ta3lime.com f919257881 (1)
shari3a
 
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
Hanaa Ahmed
 
أوامر دوس Dos
أوامر دوس Dos
Safa Tayeb
 
Computer school-books-1st-preparatory-1st-term-khawagah-2019-9
Computer school-books-1st-preparatory-1st-term-khawagah-2019-9
khawagah
 
مقدمة عن الفيجوال بيسك 9-2019
مقدمة عن الفيجوال بيسك 9-2019
Amr Rashed
 
Computer 3rd-preparatory-second-term-khawagah-6
Computer 3rd-preparatory-second-term-khawagah-6
khawagah
 

Recently uploaded (10)

نظام سداد الرسوم والتحصيلات العامه الحكومية Zakat_App_Presentation.pptx
نظام سداد الرسوم والتحصيلات العامه الحكومية Zakat_App_Presentation.pptx
mohmedalgathy
 
Saint Anthony of Padua and Lisbon (1195 - 1231).Arabic.pptx
Saint Anthony of Padua and Lisbon (1195 - 1231).Arabic.pptx
Martin M Flynn
 
الميسّر في تبسيط عهد الامام علي لمالك الأشتر.pdf
الميسّر في تبسيط عهد الامام علي لمالك الأشتر.pdf
huseeinbader
 
مؤتمر القمة السنوية 2025.pdf في دبي دكتورة رانيا مدحت
مؤتمر القمة السنوية 2025.pdf في دبي دكتورة رانيا مدحت
DrMohamedHusseinMoha
 
كتاب الوهفة في شرح دعاء عرفة للإمام علي بن الحسين عليهما السلام
كتاب الوهفة في شرح دعاء عرفة للإمام علي بن الحسين عليهما السلام
huseeinbader
 
learning arabic for non native تعليم_العربية_لغير_الناطقين_بها.pptx
learning arabic for non native تعليم_العربية_لغير_الناطقين_بها.pptx
Ryan Shaputra
 
كيف تدعم التكنولوجيا رحلة تعلم وتعليم التربية الإسلامية؟ _ نحو تعليم إسلامي ع...
كيف تدعم التكنولوجيا رحلة تعلم وتعليم التربية الإسلامية؟ _ نحو تعليم إسلامي ع...
DrMohamedHusseinMoha
 
الاربعون في المرجع الديني الاعلى من جهة التعين والحكم .pdf
الاربعون في المرجع الديني الاعلى من جهة التعين والحكم .pdf
أنور غني الموسوي
 
الإختبارات الاتلافية.12234456677899009988
الإختبارات الاتلافية.12234456677899009988
ssuserefc16b
 
الميّسر في تبسيط عهد الامام علي لمالك الاشتر
الميّسر في تبسيط عهد الامام علي لمالك الاشتر
huseeinbader
 
نظام سداد الرسوم والتحصيلات العامه الحكومية Zakat_App_Presentation.pptx
نظام سداد الرسوم والتحصيلات العامه الحكومية Zakat_App_Presentation.pptx
mohmedalgathy
 
Saint Anthony of Padua and Lisbon (1195 - 1231).Arabic.pptx
Saint Anthony of Padua and Lisbon (1195 - 1231).Arabic.pptx
Martin M Flynn
 
الميسّر في تبسيط عهد الامام علي لمالك الأشتر.pdf
الميسّر في تبسيط عهد الامام علي لمالك الأشتر.pdf
huseeinbader
 
مؤتمر القمة السنوية 2025.pdf في دبي دكتورة رانيا مدحت
مؤتمر القمة السنوية 2025.pdf في دبي دكتورة رانيا مدحت
DrMohamedHusseinMoha
 
كتاب الوهفة في شرح دعاء عرفة للإمام علي بن الحسين عليهما السلام
كتاب الوهفة في شرح دعاء عرفة للإمام علي بن الحسين عليهما السلام
huseeinbader
 
learning arabic for non native تعليم_العربية_لغير_الناطقين_بها.pptx
learning arabic for non native تعليم_العربية_لغير_الناطقين_بها.pptx
Ryan Shaputra
 
كيف تدعم التكنولوجيا رحلة تعلم وتعليم التربية الإسلامية؟ _ نحو تعليم إسلامي ع...
كيف تدعم التكنولوجيا رحلة تعلم وتعليم التربية الإسلامية؟ _ نحو تعليم إسلامي ع...
DrMohamedHusseinMoha
 
الاربعون في المرجع الديني الاعلى من جهة التعين والحكم .pdf
الاربعون في المرجع الديني الاعلى من جهة التعين والحكم .pdf
أنور غني الموسوي
 
الإختبارات الاتلافية.12234456677899009988
الإختبارات الاتلافية.12234456677899009988
ssuserefc16b
 
الميّسر في تبسيط عهد الامام علي لمالك الاشتر
الميّسر في تبسيط عهد الامام علي لمالك الاشتر
huseeinbader
 
Ad

python Elementary Programming algorithem

  • 1. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 2 Elementary Programming
  • 2. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫وهياكل‬ ‫الخوارزميات‬ ‫هي‬ ‫ما‬ ‫البيانات؟‬  ‫الخوارزميات‬ ‫في‬ ‫ي‬ ‫والت‬ ‫ة‬ ‫والمنطقي‬ ‫ة‬ ‫الرياضي‬ ‫ب‬ ‫التراكي‬ ‫ن‬ ‫م‬ ‫ة‬ ‫مجموع‬ ‫أو‬ ‫ج‬ ‫لمنت‬ ‫األداء‬ ‫ين‬ ‫تحس‬ ‫و‬ ‫أ‬ ‫ل‬ ‫مشك‬ ‫ل‬ ‫لح‬ ‫تهدف‬ ‫ة‬ ‫النهاي‬ ‫هي‬ ‫وب‬‫الحاس‬ ‫م‬‫عل‬ ‫ي‬‫ف‬ ‫الخوارزميات‬ ،‫ة‬‫المعالج‬ ‫ة‬‫طريق‬ ‫واحدة‬ ‫تنفيذها‬ ‫يتم‬ ‫التي‬ ‫الواضحة‬ ‫التعليمات‬ ‫من‬ ‫مجموعة‬ ‫لعالم‬ ‫بة‬ ‫نس‬ ‫ظ‬ ‫واللف‬ ،‫ا‬ ‫م‬ ‫ة‬ ‫مشكل‬ ‫ل‬ ‫لح‬ ‫اآلخرى‬ ‫و‬ ‫تل‬ .‫الخوارزمي‬ ‫موسى‬ ‫بن‬ ‫محمد‬ ‫الرياضيات‬
  • 3. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫وهياكل‬ ‫الخوارزميات‬ ‫هي‬ ‫ما‬ ‫البيانات؟‬  ‫البيانات‬ ‫هياكل‬ ‫فى‬ ‫التخزين‬ ‫عملية‬ ‫تصميم‬ ‫على‬ ‫تعتمد‬ ‫البيانات‬ ‫هياكل‬ ‫نوع‬ ‫ليدعم‬ ،‫معقدة‬ ‫ر‬‫وغي‬ ‫فعالة‬ ‫بطريقة‬ ‫بيوتر‬‫الكم‬ ‫او‬ ‫البرمجة‬ ‫شيفرة‬ ‫ل‬‫لتعم‬ ‫ا‬‫تخزينه‬ ‫المراد‬ ‫بيانات‬‫ال‬ .‫كفاءة‬ ‫وأكثر‬ ‫اسرع‬ ‫بطريقة‬ ‫الخوارزميات‬
  • 4. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫وهياكل‬ ‫الخوارزميات‬ ‫نتعلم‬ ‫لماذا‬ ‫البيانات؟‬ )1 ‫تفكيرك‬ ‫طريقة‬ ‫تحسن‬ o ‫تكتبه‬ ‫الذي‬ ‫البرمجي‬ ‫للكود‬ ‫ادراكا‬ ‫أكثر‬ ‫تصبح‬ ‫حيث‬ )2 ‫وتنظيمها‬ ‫األفكار‬ ‫وضوح‬ )3 ‫مصادر‬ ‫وأقل‬ ‫بفاعلية‬ ‫المشاكل‬ ‫حل‬ )4 ‫العمل‬ ‫مقابالت‬ ‫في‬ ‫النجاح‬
  • 5. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهم‬  – ‫القوائم‬ Lists  – ‫المتصلة‬ ‫القوائم‬ Linked Lists  – ‫المكدسات‬ Stacks  – ‫الطوابير‬ Queues  – ‫الشجرات‬ Trees  – ‫البحث‬ Searching  – ‫الفرز‬ Sorting
  • 6. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ ‫البرنامج‬ Program Code ‫بيانات‬ Data ‫األوامر‬ Instructions ‫من‬ ‫يتكون‬
  • 7. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬  ‫مثال‬ : Method Add(a, b) { return a + b; } Data – ‫البيانات‬: •a •b Instructions – ‫األوامر‬: •Return •+
  • 8. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU RAM Storage Memory Code (Instructions) Data ‫المستخدمة‬ ‫بيانات‬55‫وال‬ ‫برنامج‬55‫ال‬ ‫التخزين‬ ‫وحدة‬ ‫ل‬5‫خ‬‫دا‬ ‫ة‬5‫ن‬‫مخز‬ ‫تكون‬ ) ( ‫ديسك‬ ‫هارد‬
  • 9. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data ( ‫ل‬5‫ي‬‫لتشغ‬ run ) ‫ال‬ ‫يقوم‬ ‫البرنامج‬ CPU ‫الى‬ ‫الكود‬ ‫خ‬5‫س‬‫ن‬ ‫و‬5‫أ‬ ‫ل‬5‫ي‬‫بتحم‬ ( ‫العشوائية‬ ‫الذاكرة‬ RAM )
  • 10. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data Code (Instructions) ‫ال‬ ‫يقوم‬ ‫الكود‬ ‫تشغيل‬ ‫أثناء‬ CPU ( ‫الالزمة‬ ‫بيانات‬5‫ل‬‫ا‬ ‫ل‬5‫ي‬‫بتحم‬ Data ) ‫أيضا‬ ‫العشوائية‬ ‫الذاكرة‬ ‫الى‬
  • 11. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data Code (Instructions) Data 5‫ل‬‫العم‬ ‫برنامج‬55‫ال‬ ‫تطيع‬55‫يس‬ ‫االن‬ ‫أفضل‬ ‫وفاعلية‬ ‫بسرعة‬
  • 12. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data Code (Instructions) Data ‫البيانات‬ ‫تدعاء‬55‫باس‬ ‫الكود‬ ‫يقوم‬ ‫العشوائية‬ ‫الذاكرة‬ ‫ي‬55‫ف‬ ‫ة‬55‫المخزن‬ ‫البرنامج‬ ‫عمل‬ ‫اثناء‬
  • 13. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data Code (Instructions) Data ‫البيانات‬ ‫ل‬5‫هياك‬ ‫ة‬5‫اهمي‬ ‫ن‬5‫تكم‬ ‫ا‬5‫هن‬ ( Data Structure ) ‫خالل‬ ‫من‬ ‫في‬ ‫بيانات‬5‫ل‬5‫ا‬ ‫ن‬5‫ي‬‫تخز‬ ‫ة‬5‫ي‬‫وال‬ ‫ة‬5‫ق‬‫طري‬ ‫العشوائية‬ ‫الذاكرة‬
  • 14. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫أهمية‬ CPU Storage Memory Code (Instructions) Data Code (Instructions) Data ( ‫البيانات‬ ‫ل‬5‫ك‬‫هيا‬ ‫ف‬5‫ي‬‫تعر‬ ‫ن‬5‫ك‬‫يم‬ Data Structure ) ‫طريقة‬ ‫ا‬55‫أنه‬ ‫ى‬55‫عل‬ ‫المستخدمة‬ ‫بيانات‬5‫ال‬ ‫ن‬5‫وتخزي‬ ‫ب‬5‫لترتي‬ ‫أكثر‬ ‫ل‬5‫بشك‬ ‫تخدامها‬5‫باس‬ ‫مح‬5‫تس‬ ‫ة‬5‫بالي‬ ‫من‬ ‫برنامج‬55‫ال‬ ‫ن‬55‫تمك‬ ‫ا‬55‫كم‬ ،‫ة‬55‫فعالي‬ ‫أقصر‬ ‫ووقت‬ ‫بسهولة‬ ‫اليها‬ ‫الوصول‬
  • 15. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 15 Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area of the circle. ComputeArea
  • 16. Algorithms and Data Structures Algorithms and Data Structures ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ # # 02 02 ‫الذاكرة‬ ‫تخصيص‬ – ‫الذاكرة‬ ‫تخصيص‬ –
  • 17. ‫الذاكرة‬ ‫تخصيص‬ ‫الذاكرة‬ ‫تخصيص‬ (Memory Allocation) (Memory Allocation) • )‫وأوامر‬ ‫(بيانات‬ ‫ما‬ ‫لعملية‬ ‫الذاكرة‬ ‫عنوان‬ ‫مساحة‬ ‫لتخصيص‬ ‫إجراء‬ ‫لتنفيذها‬ • ‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬ .1 ‫الثابت‬ ‫الذاكرة‬ ‫تخصيص‬ Static Memory Allocation .2 ‫الديناميكي‬ ‫الذاكرة‬ ‫تخصيص‬ Dynamic Memory Allocation
  • 18. ‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬ ‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬ • ‫الثابت‬ ‫الذاكرة‬ ‫تخصيص‬ Static Memory Allocation o ( ‫م‬5‫المترج‬ ‫يقوم‬ ‫ا‬5‫عندم‬ ‫ت‬5‫الثاب‬ ‫الذاكرة‬ ‫يص‬5‫تخص‬ ‫ذ‬5‫ي‬‫تنف‬ ‫م‬5‫يت‬ Compiler ‫ع‬5‫بتجمي‬ ) ‫ر‬5‫العناص‬ ‫ملفات‬ ‫ع‬5‫جمي‬ ‫ج‬5‫بدم‬ ‫ط‬5‫الراب‬ ‫ويقوم‬ ،‫ر‬5‫العناص‬ ‫ملفات‬ ‫وإنشاء‬ ‫برنامج‬5‫ال‬ ‫للتنفيذ‬ ‫قابل‬ ‫واحد‬ ‫ملف‬ ‫وإنشاء‬ ‫هذه‬
  • 19. ‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬ ‫الذاكرة‬ ‫تخصيص‬ ‫أنواع‬ • ‫الديناميكي‬ ‫الذاكرة‬ ‫تخصيص‬ Dynamic Memory Allocation o ‫يص‬5‫ص‬‫تخ‬ ‫م‬5‫ت‬‫ي‬ ،‫ا‬5‫ن‬‫ه‬ ،‫برنامج‬5‫ل‬‫ا‬ ‫ذ‬5‫ي‬‫تنف‬ ‫أثناء‬ ‫ي‬5‫ك‬‫الدينامي‬ ‫الذاكرة‬ ‫يص‬5‫ص‬‫تخ‬ ‫ذ‬5‫ي‬‫تنف‬ ‫م‬5‫ت‬‫ي‬ ،‫البرنامج‬ ‫تشغيل‬ ‫أثناء‬ ‫مرة‬ ‫ألول‬ ‫استخدامها‬ ‫يتم‬ ‫عندما‬ ‫البرنامج‬ ‫لعناصر‬ ‫الذاكرة‬ ‫ص‬5‫يخص‬ ‫و‬5‫فه‬ ‫لذا‬ ،‫ل‬5‫التشغي‬ ‫ت‬5‫وق‬ ‫ي‬5‫ف‬ ‫ة‬5‫المطلوب‬ ‫بيانات‬5‫لل‬ ‫ي‬5‫الفعل‬ ‫م‬5‫الحج‬ ‫عرف‬ُ ‫ي‬ ‫الذاكرة‬ ‫إهدار‬ ‫من‬ ‫يقلل‬ ‫وبالتالي‬ ‫للبرنامج‬ ‫الدقيقة‬ ‫الذاكرة‬ ‫مساحة‬
  • 20. ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ CPU Storage Memory Code (Instructions) Data ( ‫ة‬5‫ي‬‫لعشوائ‬5‫ا‬ ‫الذاكرة‬ ‫م‬5‫س‬‫تق‬ RAM ) ‫أقسام‬ ‫ثالثة‬ ‫الى‬ Code Stack Heap
  • 21. ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ CPU Storage Memory Code (Instructions) Data ‫جزء‬ ‫في‬ ‫البرنامج‬ ‫كود‬ ‫تحميل‬ ‫يتم‬ ‫الذاكرة‬ ‫في‬ ‫الكود‬ Code Code Stack Heap Code (Instructions)
  • 22. ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ CPU Storage Memory Code (Instructions) Data ‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬: main() { int n1, n2; float Avg; Avg = (n1+n2) / 2; } Code Stack Heap Code (Instructions) ( ‫البيانات‬ ‫نوع‬ ‫معروف‬ ‫يكون‬ ‫عندما‬ Primitive Data Type ) ‫للمتغيرات‬ ‫المحجوزة‬ ‫لمساحة‬5‫ا‬ ‫معروف‬ ‫يكون‬ n1 n2 Avg
  • 23. ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ CPU Storage Memory Code (Instructions) Data ‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬: main() { int n1, n2; n1 = 5; n2 = 10; float Avg; Avg = (n1+n2) / 2; } Code Stack Heap Code (Instructions) ‫يتم‬ ‫معروفة‬ ‫المحجوزة‬ ‫والمساحة‬ ‫معروف‬ ‫البيانات‬ ‫نوع‬ ‫أن‬ ‫بما‬ ( ‫قسم‬ ‫في‬ ‫المساحة‬ ‫حجز‬ Stack ‫الذاكرة‬ ‫من‬ ) n1 n2 Avg
  • 24. ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ ‫الذاكرة‬ ‫تخصيص‬ ‫فهم‬ CPU Storage Memory Code (Instructions) Data ‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬: main() { int n1, n2; n1 = 5; n2 = 10; float Avg; Avg = (n1+n2) / 2; } Code Stack Heap Code (Instructions) ( ‫قسم‬ ‫في‬ ‫للمتغيرات‬ ‫المساحة‬ ‫حجز‬ ‫يتم‬ Stack ‫في‬ ‫الذاكرة‬ ‫من‬ ) ( ‫ال‬ ‫مرحلة‬ Compilation ‫البرنامج‬ ‫تنفيذ‬ ‫مرحلة‬ ‫قبل‬ ) 5 10 n1 n2 7.5 Avg
  • 25. ‫الذاكرة‬ ‫تخصيص‬ ‫مثال‬ ‫الذاكرة‬ ‫تخصيص‬ ‫مثال‬ Dynamic Memory Allocation Dynamic Memory Allocation Memory ‫يحتوي‬ ‫البرنامج‬ ‫ان‬ ‫فرض‬ ‫على‬: main() { int i; float * ptr; cout << “Enter array size n”; cin >> i;  ex: 5 ptr = new float [i]; delete [] ptr; return 0; } Code Stack Heap Code * i ptr Main ( ‫في‬ ‫الديناميكي‬ ‫كرة‬5‫ا‬‫الذ‬ ‫حجز‬ ‫يوضح‬ ‫المثال‬ ‫هذا‬ Heap ‫من‬ ) ( ‫البرنامج‬ ‫تنفيذ‬ ‫مرحلة‬ ‫في‬ ‫الذاكرة‬ Run )
  • 26. Algorithms and Data Structures Algorithms and Data Structures ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ ‫البيانات‬ ‫وهياكل‬ ‫الخوارزميات‬ # # 03 03 ‫تصنيفات‬ – ‫تصنيفات‬ –
  • 27. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ‫البيانات‬ ‫هياكل‬ o .‫فعال‬ ‫بشكل‬ ‫استخدامها‬ ‫ليتم‬ ‫الحاسوب‬ ‫في‬ ‫البيانات‬ ‫وتنظيم‬ ‫لترتيب‬ ‫خاصة‬ ‫طريقة‬ o .‫واحد‬ ‫اسم‬ ‫تحت‬ ‫البعض‬ ‫بعضها‬ ‫مع‬ ‫تخزينها‬ ‫يتم‬ ‫البيانات‬ ‫عناصر‬ ‫من‬ ‫مجموعة‬ • ( ‫المصفوفة‬ :‫مثال‬ Array ) .‫النوع‬ ‫نفس‬ ‫من‬ ‫عناصر‬ ‫عدة‬ ‫يحتوي‬ – • – ‫أيضا‬ ‫عليها‬ ‫يطلق‬ Abstract Data Type (ADT) o ‫البيانات‬ ‫تخزين‬ ‫طريقة‬ o ‫البيانات‬ ‫هذه‬ ‫على‬ ‫تتم‬ ‫التي‬ ‫والعمليات‬ ‫األوامر‬
  • 28. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ( ‫مثال‬ ADT – ‫الطابور‬ :) Queue Counter Front or Head Rear or Tail enqueue dequeue
  • 29. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ‫البيانات‬ ‫هياكل‬ ‫استخدام‬ ‫من‬ ‫الهدف‬ o :‫على‬ ‫يساعد‬ • ‫فعال‬ ‫بشكل‬ ‫البيانات‬ ‫تخزين‬ • ‫ممكن‬ ‫وقت‬ ‫أقل‬ ‫في‬ ‫البيانات‬ ‫استرجاع‬ • ‫فعال‬ ‫بشكل‬ ‫البيانات‬ ‫تعديل‬ • ‫البرنامج‬ ‫تصميم‬ ‫أثناء‬ o ( ‫البيانات‬ ‫هياكل‬ ‫من‬ ‫نوع‬ ‫أنسب‬ ‫ختيار‬5‫ا‬ ‫هو‬ ‫التحدي‬ Data Structure ‫ما‬ ‫مشكلة‬ ‫لحل‬ ) • ( ‫ال‬ ‫استخدام‬ ‫يفضل‬ ‫ال‬ ‫الطالب‬ ‫عالمات‬ ‫الدخال‬ :‫مثال‬ Array .‫ثابت‬ ‫حجمها‬ ‫الن‬ )
  • 30. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ‫البيانات‬ ‫هياكل‬
  • 31. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ‫الوجود‬ ‫حسب‬ ‫التصنيف‬ o ‫المادية‬ ‫البيانات‬ ‫هياكل‬ ( Physical ) • ‫مستقل‬ ‫بشكل‬ ‫انشائها‬ ‫يمكن‬ o ( ‫المصفوفات‬ Array ) o ( ‫المتصلة‬ ‫القوائم‬ Linked List ) o ‫المنطقية‬ ‫البيانات‬ ‫هياكل‬ ( Logical ) • ‫مستقل‬ ‫بشكل‬ ‫انشائها‬ ‫يمكن‬ ‫ال‬ o ( ‫المكدسات‬ Stacks ) o ( ‫الطوابير‬ Queues ) o ( ‫الشجرات‬ Trees )
  • 32. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • ‫الذاكرة‬ ‫حجز‬ ‫حسب‬ ‫التصنيف‬ o ( ‫المساحة‬ ‫ثابتة‬ ‫البيانات‬ ‫هياكل‬ Static ) • ‫ثابتة‬ ‫مساحة‬ ‫تحجز‬ • ( ‫المصفوفات‬ ‫مثل‬ Array ) o ( ‫المساحة‬ ‫ديناميكية‬ ‫البيانات‬ ‫هياكل‬ Dynamic ) • ‫الحاجة‬ ‫حسب‬ ‫المساحة‬ ‫تتغير‬ • ( ‫المتصلة‬ ‫القوائم‬ ‫مثل‬ Linked List )
  • 33. ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ ‫البيانات‬ ‫هياكل‬ ‫تصنيفات‬ • 5 ‫ل‬‫التمثي‬ ‫حسب‬ ‫التصنيف‬ o ( ‫الخطية‬ ‫البيانات‬ ‫هياكل‬ Linear ) • ( ‫المصفوفات‬ Array ) • ( ‫المتصلة‬ ‫القوائم‬ Linked List ) • ( ‫المكدسات‬ Stack ) • ( ‫الطابور‬ Queue ) o ( ‫الخطية‬ ‫غير‬ ‫البيانات‬ ‫هياكل‬ Non-Linear ) • ( ‫الشجرات‬ Trees ) • ( ‫البياني‬ ‫الرسم‬ Graph )
  • 34. ‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬ ‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬ • – 5 ‫ر‬‫العناص‬ ‫جميع‬ ‫على‬ ‫المرور‬ Traversing o .‫العنصر‬ ‫هذا‬ ‫على‬ ‫ما‬ ‫عملية‬ ‫وتنفيذ‬ ‫عنصر‬ ‫كل‬ ‫زيارة‬ ‫أو‬ ‫الوصول‬ • - ‫البحث‬ Searching o .‫البيانات‬ ‫تركيب‬ ‫في‬ ‫مخزن‬ ‫معين‬ ‫عنصر‬ ‫موقع‬ ‫تحديد‬ ‫أو‬ ‫البحث‬ • – ‫االضافة‬ Insertion o .‫موجود‬ ‫بيانات‬ ‫تركيب‬ ‫الى‬ ‫جديد‬ ‫عنصر‬ ‫اضافة‬
  • 35. ‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬ ‫البيانات‬ ‫هياكل‬ ‫على‬ ‫العمليات‬ • – ‫الحذف‬ Deletion o .‫موجود‬ ‫بيانات‬ ‫تركيب‬ ‫من‬ ‫عنصر‬ ‫ازالة‬ ‫أو‬ ‫حذف‬ • – ‫الترتيب‬ Sorting o .‫منطقية‬ ‫بطريقة‬ ‫البيانات‬ ‫تركيب‬ ‫في‬ ‫العناصر‬ ‫ترتيب‬ • .‫تنازلي‬ ‫أو‬ ‫تصاعدي‬ ‫ترتيبها‬ ‫يمكن‬ • – ‫الدمج‬ Merging o .‫واحد‬ ‫في‬ ‫وجمعها‬ ‫بيانات‬ ‫تركيب‬ ‫من‬ ‫أكثر‬ ‫من‬ ‫العناصر‬ ‫ودمج‬ ‫ضم‬
  • 36. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 36 Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius", radius, "is", area) 20 radius Assign 20 to radius animation
  • 37. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 37 Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius", radius, "is", area) 20 radius Assign result to area animation 1256.636 area
  • 38. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 38 Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius", radius, "is", area) 20 radius print a message to the console animation 1256.636 area
  • 39. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 39 Reading Input from the Console 1. Use the input function variable = input("Enter a string: ") 2. Use the float and int function to convert a string to a float or int. var = float(stringVariable) var = int(stringVariable) ComputeAreaWithConsoleInput ComputeAverage
  • 40. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 40 Identifiers  An identifier is a sequence of characters that consists of letters, digits, underscores (_), and asterisk (*).  An identifier must start with a letter or an underscore. It cannot start with a digit.  An identifier cannot be a reserved word. (See Appendix A, "Python Keywords," for a list of reserved words.) Reserved words have special meanings in Python, which we will later discuss.  An identifier can be of any length.
  • 41. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 41 Variables # Compute the first area radius = 1.0 area = radius * radius * 3.14159 print("The area is ", area, " for radius ", radius) # Compute the second area radius = 2.0 area = radius * radius * 3.14159 print("The area is ", area, " for radius ", radius)
  • 42. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 42 Expression x = 1 # Assign 1 to variable x radius = 1.0 # Assign 1.0 to variable radius # Assign the value of the expression to x x = 5 * (3 / 2) + 3 * 2 x = y + 1 # Assign the addition of y and 1 to x area = radius * radius * 3.14159 # Compute area
  • 43. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 43 Assignment Statements x = 1 # Assign 1 to x x = x + 1 i = j = k = 1
  • 44. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 44 Simultaneous Assignment var1, var2, ..., varn = exp1, exp2, ..., expn x, y = y, x # Swap x with y ComputeAverageWithSimultaneousAssignment
  • 45. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 45
  • 46. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 46 Named Constants The value of a variable may change during the execution of a program, but a named constant or simply constant represents permanent data that never changes. Python does not have a special syntax for naming constants. You can simply create a variable to denote a constant. To distinguish a constant from a variable, use all uppercase letters to name a constant.
  • 47. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 47 Numerical Data Types  integer: e.g., 3, 4  float: e.g., 3.0, 4.0
  • 48. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 48 Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Float Division 1 / 2 0.5 // Integer Division 1 // 2 0 ** Exponentiation 4 ** 0.5 2.0 % Remainder 20 % 3 2
  • 49. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 49 The % Operator 12 4 3 12 0 7 3 2 6 1 26 8 3 24 2 Remainder Quotient 20 13 1 13 7 Dividend Divisor
  • 50. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 50 Remainder Operator Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression: Saturday is the 6th day in a week A week has 7 days After 10 days The 2nd day in a week is Tuesday (6 + 10) % 7 is 2
  • 51. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 51 Problem: Displaying Time Write a program that obtains minutes and remaining seconds from seconds. DisplayTime
  • 52. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 52 Overflow When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. For example, executing the following statement causes overflow. >>> 245.0 ** 1000 OverflowError: 'Result too large'
  • 53. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 53 Underflow When a floating-point number is too small (i.e., too close to zero) to be stored, it causes underflow. Python approximates it to zero. So normally you should not be concerned with underflow.
  • 54. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 54 Scientific Notation Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase.
  • 55. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 55 Arithmetic Expressions ) 9 4 ( 9 ) )( 5 ( 10 5 4 3 y x x x c b a y x         is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
  • 56. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 56 How to Evaluate an Expression Though Python has its own way to evaluate an expression behind the scene, the result of a Python expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Python expression. 3 + 4 * 4 + 5 * (4 + 3) - 1 3 + 4 * 4 + 5 * 7 – 1 3 + 16 + 5 * 7 – 1 3 + 16 + 35 – 1 19 + 35 – 1 54 - 1 53 (1) inside parentheses first (2) multiplication (3) multiplication (4) addition (6) subtraction (5) addition
  • 57. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 57 Augmented Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f - 8.0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 //= i //= 8 i = i // 8 %= i %= 8 i = i % 8 **= i **= 8 i = i ** 8
  • 58. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 58 Type Conversion and Rounding datatype(value) i.e., int(4.5) => 4 float(4) => 4.0
  • 59. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 59 Problem: Keeping Two Digits After Decimal Points Write a program that displays the sales tax with two digits after the decimal point. SalesTax
  • 60. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 60 Problem: Displaying Current Time Write a program that displays current time in GMT in the format hour:minute:second such as 1:45:19. The time.time() function returns the current time in seconds with millisecond precision since the midnight, January 1, 1970 GMT. (1970 was the year when the Unix operating system was formally introduced.) You can use this function to obtain the current time, and then compute the current second, minute, and hour as follows. Elapsed time Unix epoch 01-01-1970 00:00:00 GMT Current time Time time.time() ShowCurrentTime
  • 61. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 61 Problem: Computing Loan Payments This program lets the user enter the interest rate, number of years, and loan amount, and computes monthly payment and total payment. 12 ) 1 ( 1 1      ars numberOfYe erestRate monthlyInt erestRate monthlyInt loanAmount ment monthlyPay ComputeLoan
  • 62. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 62 Case Study: Computing Distances This program prompts the user to enter two points, computes their distance, and displays the distance. 2 1 2 2 1 2 ) ( ) ( y y x x    ComputeDistance
  • 63. © Copyright 2022 by Pearson Education, Inc. All Rights Reserved. 63 Case Study: Computing Distances This program prompts the user to enter two points, computes their distance, and displays the points and their distances in graphics. ComputeDistanceGraphics