0% found this document useful (0 votes)
19 views12 pages

Python Comprehensive Guide

תדתת

Uploaded by

omar.shalbe.111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Python Comprehensive Guide

תדתת

Uploaded by

omar.shalbe.111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Comprehensive Guide

1. Python Basics
Variables and Data Types

 Variables: Containers for storing data values.


 x = 5
 y = "Hello"
 Data Types: int, float, str, bool, list, tuple, dict, set, etc.
 age = 25 # int
 pi = 3.14 # float
 name = "Alice" # str
 is_happy = True # bool

Operators

 Arithmetic: +, -, *, /, %, **, //
 Comparison: ==, !=, >, <, >=, <=
 Logical: and, or, not
 Membership: in, not in
 print(3 in [1, 2, 3]) # True

2. Control Structures
Conditional Statements
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")

3. Loops
for Loops
for i in range(5):
print(i) # Prints 0 to 4

while Loops
count = 0
while count < 5:
print(count)
count += 1

Loop Control Statements

 break: Exits the loop.


 continue: Skips to the next iteration.
 pass: Does nothing.
 for i in range(5):
 if i == 3:
 break
 print(i) # Stops when i == 3

4. Lists
List Basics
my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # Access first element
my_list.append(6) # Add to list
my_list.remove(3) # Remove element

List Comprehensions
squares = [x**2 for x in range(5)]

5. Strings
String Operations
name = "Alice"
print(name.upper()) # ALICE
print(name.lower()) # alice
print(name[0]) # A

String Formatting
age = 25
print(f"My age is {age}")

6. Memory Management
Dynamic Typing

Variables in Python are dynamically typed.

Garbage Collection

Python uses automatic garbage collection to manage memory.

7. Functions
Defining Functions
def greet(name):
return f"Hello, {name}!"

Lambda Functions
add = lambda x, y: x + y
print(add(2, 3))

8. Search
Linear Search
def linear_search(lst, target):
for i in range(len(lst)):
if lst[i] == target:
return i
return -1

Binary Search
def binary_search(lst, target):
low, high = 0, len(lst) - 1
while low <= high:
mid = (low + high) // 2
if lst[mid] == target:
return mid
elif lst[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
9. Input/Output (I/O) and Exceptions
File Operations
with open("file.txt", "w") as file:
file.write("Hello, World!")

with open("file.txt", "r") as file:


content = file.read()
print(content)

Exception Handling
try:
x = 1 / 0
except ZeroDivisionError as e:
print("Cannot divide by zero")
finally:
print("Execution finished")

10. Recursion
Recursive Function
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

11. Dictionaries
Dictionary Basics
my_dict = {"name": "Alice", "age": 25}
print(my_dict["name"])
my_dict["job"] = "Engineer"

Dictionary Comprehensions
squares = {x: x**2 for x in range(5)}

12. Object-Oriented Programming (OOP)


Classes and Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
return f"Hello, my name is {self.name}!"

person = Person("Alice", 25)


print(person.greet())

Inheritance
class Employee(Person):
def __init__(self, name, age, job):
super().__init__(name, age)
self.job = job

employee = Employee("Bob", 30, "Developer")


print(employee.greet())

‫دليل شامل للغة بايثون‬

1. ‫أساسيات بايثون‬
‫تعريف المتغيرات وأنواع البيانات‬:

‫ مثل‬،‫ يمكن أن تكون المتغيرات من أنواع متعددة‬.‫المتغيرات هي حاويات لتخزين القيم‬:

 int ‫لألعداد الصحيحة‬.


 float ‫لألعداد العشرية‬.
 str ‫للنصوص‬.
 bool ‫للقيم المنطقية‬.

‫أمثلة‬:

x = 10 # ‫عدد صحيح‬
y = 3.14 # ‫عدد عشري‬
name = "‫ نص‬# "‫علي‬
is_active = True # ‫قيمة منطقية‬

‫العمليات الحسابية والمنطقية‬:


‫‪‬‬ ‫‪.‬العمليات الحسابية تشمل‪( // ،/ ،* ،- ،+ :‬القسمة الصحيحة)‪( % ،‬باقي القسمة)‪( ** ،‬األس)‬
‫‪‬‬ ‫‪: and، or، not.‬العمليات المنطقية تشمل‬

‫‪:‬أمثلة‬

‫العمليات الحسابية ‪#‬‬


‫‪print(5 + 3) # 8‬‬
‫‪print(10 // 3) # 3‬‬
‫‪print(10 % 3) # 1‬‬

‫العمليات المنطقية ‪#‬‬


‫‪print(True and False) # False‬‬
‫‪print(True or False) # True‬‬
‫‪print(not True) # False‬‬

‫التحكم في التدفق ‪2.‬‬


‫‪:‬الجمل الشرطية‬

‫‪ُ.‬تستخدم الجمل الشرطية التخاذ قرارات بناًء على شروط معينة‬

‫‪:‬الصيغة العامة‬

‫‪if condition:‬‬
‫الكود الذي سيتم تنفيذه إذا تحقق الشرط ‪#‬‬
‫‪elif another_condition:‬‬
‫الكود إذا تحقق شرط آخر ‪#‬‬
‫‪else:‬‬
‫الكود إذا لم يتحقق أي شرط ‪#‬‬

‫‪:‬أمثلة‬

‫‪x = 10‬‬
‫‪if x > 5:‬‬
‫)"أكبر من ‪print("x 5‬‬
‫‪elif x == 5:‬‬
‫)"يساوي ‪print("x 5‬‬
‫‪else:‬‬
‫)"أصغر من ‪print("x 5‬‬

‫‪:‬التحكم بالحلقات‬

‫‪‬‬ ‫‪.‬للخروج من الحلقة ‪break:‬‬


‫‪‬‬ ‫‪.‬لتخطي التكرار الحالي ‪continue:‬‬
‫‪‬‬ ‫‪.‬للمرور دون تنفيذ أي كود ‪pass:‬‬

‫‪:‬أمثلة‬
‫‪for i in range(5):‬‬
‫‪if i == 3:‬‬
‫‪break‬‬
‫يطبع ‪print(i) # 2 ،1 ،0‬‬

‫‪for i in range(5):‬‬
‫‪if i == 2:‬‬
‫‪continue‬‬
‫يتخطى الرقم ‪print(i) # 2‬‬

‫الحلقات ‪3.‬‬
‫‪ for:‬حلقات‬

‫‪ُ.‬تستخدم للتكرار على تسلسالت مثل القوائم أو النصوص أو القواميس‬

‫‪:‬أمثلة‬

‫التكرار على قائمة ‪#‬‬


‫‪for item in [1, 2, 3]:‬‬
‫)‪print(item‬‬

‫التكرار على نص ‪#‬‬


‫‪":‬مرحًبا" ‪for char in‬‬
‫)‪print(char‬‬

‫‪ while:‬حلقات‬

‫‪ُ.‬تستخدم للتكرار طالما تحقق شرط معين‬

‫‪:‬أمثلة‬

‫‪count = 0‬‬
‫‪while count < 5:‬‬
‫)‪print(count‬‬
‫‪count += 1‬‬

‫القوائم ‪4.‬‬
‫‪:‬تعريف القوائم‬

‫‪.‬القوائم هي مجموعة مرتبة وقابلة للتغيير من العناصر‬

‫‪:‬أمثلة‬

‫]‪my_list = [1, 2, 3‬‬


‫إضافة عنصر ‪my_list.append(4) #‬‬
‫إزالة عنصر ‪my_list.remove(2) #‬‬
‫تعديل عنصر ‪my_list[1] = 10 #‬‬

‫‪:‬تراكيب القوائم‬

‫‪.‬طريقة مختصرة إلنشاء القوائم‬

‫‪:‬أمثلة‬

‫])‪squares = [x**2 for x in range(5‬‬


‫]‪print(squares) # [0, 1, 4, 9, 16‬‬

‫النصوص ‪5.‬‬
‫‪:‬التعامل مع النصوص‬

‫‪:‬أمثلة‬

‫"مرحًبا بالعالم" = ‪text‬‬


‫تحويل إلى أحرف كبيرة ‪print(text.upper()) #‬‬
‫تحويل إلى أحرف صغيرة ‪print(text.lower()) #‬‬
‫تقسيم النص ‪print(text.split()) #‬‬

‫إدارة الذاكرة ‪6.‬‬


‫‪‬‬ ‫‪.‬لتنظيف الذاكرة تلقائًيا )‪ (Garbage Collector‬بايثون تستخدم جامع القمامة‬
‫‪‬‬ ‫‪ del:‬يمكن حذف المتغيرات يدوًيا باستخدام‬

‫‪:‬أمثلة‬

‫‪x = 10‬‬
‫حذف المتغير ‪del x #‬‬

‫الدوال ‪7.‬‬
‫‪:‬تعريف الدوال‬

‫‪:‬أمثلة‬

‫‪def greet(name):‬‬
‫"!}‪، {name‬مرحًبا"‪return f‬‬

‫))"علي"(‪print(greet‬‬
‫دوال المساعد‬:

‫ُتستخدم لتقسيم الكود المعقد إلى أجزاء صغيرة قابلة لإلدارة‬.

‫أمثلة‬:

def calculate_total(price, tax):


def apply_tax(amount, tax_rate):
return amount * (1 + tax_rate)

return apply_tax(price, tax)

print(calculate_total(100, 0.15))

8. ‫البحث‬
‫البحث الخطي‬:

‫أمثلة‬:

def linear_search(lst, target):


for i in range(len(lst)):
if lst[i] == target:
return i
return -1

‫البحث الثنائي‬:

‫أمثلة‬:

def binary_search(lst, target):


low, high = 0, len(lst) - 1
while low <= high:
mid = (low + high) // 2
if lst[mid] == target:
return mid
elif lst[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

9. ‫اإلدخال واإلخراج ومعالجة االستثناءات‬


‫التعامل مع الملفات‬:

‫أمثلة‬:
with open("file.txt", "w") as file:
file.write("‫)"!مرحًبا بالعالم‬

with open("file.txt", "r") as file:


content = file.read()
print(content)

‫معالجة األخطاء‬:

‫أمثلة‬:

try:
x = 1 / 0
except ZeroDivisionError:
print("‫)"ال يمكن القسمة على الصفر‬
finally:
print("‫)"تمت المعالجة‬

10. ‫التكرار‬
‫أمثلة على التكرار‬:
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(5))

11. ‫القواميس‬
‫أمثلة على القواميس‬:
my_dict = {"name": "‫"علي‬, "age": 25}
print(my_dict["name"])
my_dict["job"] = "‫"مهندس‬

12. ‫البرمجة كائنية التوجه‬


‫أمثلة‬:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"‫ اسمي‬،‫{ مرحًبا‬self.name}!"

person = Person("25 ,"‫)علي‬


print(person.greet())

13. ‫ مكتبة‬NumPy
‫أمثلة‬:
import numpy as np
arr = np.array([1, 2, 3])
print(arr + 1)

14. ‫الرسم البياني‬


‫أمثلة‬:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

15. ‫تحليل البيانات‬


‫أمثلة‬:
import pandas as pd
data = {"name": ["‫ "سارة‬,"‫]"علي‬, "age": [25, 30]}
df = pd.DataFrame(data)
print(df)

16. ‫تحليل وقت التشغيل‬


‫أمثلة‬:
import time
start = time.time()
for i in range(1000000):
pass
end = time.time()
print(f"‫الوقت‬: {end - start}")
17. ‫الحسابات العلمية‬
‫أمثلة‬:
from scipy.integrate import quad
result, _ = quad(lambda x: x**2,

You might also like