Python Comprehensive Guide
Python Comprehensive Guide
1. Python Basics
Variables and Data Types
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
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
Garbage Collection
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!")
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)
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)}
def greet(self):
return f"Hello, my name is {self.name}!"
Inheritance
class Employee(Person):
def __init__(self, name, age, job):
super().__init__(name, age)
self.job = job
1. أساسيات بايثون
تعريف المتغيرات وأنواع البيانات:
أمثلة:
x = 10 # عدد صحيح
y = 3.14 # عدد عشري
name = " نص# "علي
is_active = True # قيمة منطقية
:أمثلة
:الصيغة العامة
if condition:
الكود الذي سيتم تنفيذه إذا تحقق الشرط #
elif another_condition:
الكود إذا تحقق شرط آخر #
else:
الكود إذا لم يتحقق أي شرط #
:أمثلة
x = 10
if x > 5:
)"أكبر من print("x 5
elif x == 5:
)"يساوي print("x 5
else:
)"أصغر من print("x 5
:التحكم بالحلقات
:أمثلة
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:حلقات
:أمثلة
while:حلقات
:أمثلة
count = 0
while count < 5:
)print(count
count += 1
القوائم 4.
:تعريف القوائم
:أمثلة
:تراكيب القوائم
:أمثلة
النصوص 5.
:التعامل مع النصوص
:أمثلة
:أمثلة
x = 10
حذف المتغير del x #
الدوال 7.
:تعريف الدوال
:أمثلة
def greet(name):
"!}، {nameمرحًبا"return f
))"علي"(print(greet
دوال المساعد:
أمثلة:
print(calculate_total(100, 0.15))
8. البحث
البحث الخطي:
أمثلة:
البحث الثنائي:
أمثلة:
أمثلة:
with open("file.txt", "w") as file:
file.write(")"!مرحًبا بالعالم
معالجة األخطاء:
أمثلة:
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"] = ""مهندس
13. مكتبةNumPy
أمثلة:
import numpy as np
arr = np.array([1, 2, 3])
print(arr + 1)