Python Course 1

Python Variables & Data Types & Numbers & Casting

Python Variables

Python يتم إنشاء المتغيرات عندما تقوم بتعيين قيمة لها:

x = 3

y = “Hello, world! “

في هذه الحالة تكون قد قيمة المتغير x والمتغير y

لتشغيل البرنامج نكتب

print(x) # the output will be 3

print(y) # the output will be Hello, world!



declaring a variable

لا يلزم التصريح عن المتغيرات بأي نوع معين، بل يمكن تغيير نوعها بعد تعيينها.

x = 6

x = “Ali”

print(x) # the output will be Ali



هناك انواع متعددة من المتغيرات مثلاً ممكن ان يكون سلسلة أي “str” وتحدد السلسلة بأحد علامات Quotes ‘ ‘ ، ” “

ويمكن ان يكون رقم صحيح “int” او ان يكون برقم ذو فواصل “float”

يمكن تحديد نوع المتغير بالشكل التالي

x = str(3) # the output will be ‘x’

x = int(3) # the output will be 3

x = float (3) # the output will be 3.0 لمعرفة النوع

x = 1

print(type(x)) # the output will be <class ‘int’>

ملاحظة: عند كتابة str لايوجد فرق بين تحديدها ب Double quotes او single quotes مثلاً

x = ‘Mais’

x = “Mais”

هما نفس الشيء

ملاحظة: لغة python حساسة اي انه المتغير A ليس نفسه المتغير a
variable names

يمكن أن يكون للمتغير اسم قصير (مثل x و y) أو اسم وصفي أكثر (age, size, name).

_‌يجب أن يبدأ اسم المتغير بحرف أو

‌ لا يمكن أن يبدأ اسم المتغير برقم

‌ لا يمكن أن يحتوي اسم المتغير إلا على أحرف أبجدية او ارقام او _ (A-z او 0-9 او _)

‌ أسماء المتغيرات حساسة لحالة الأحرف (Name و name و NAME ثلاثة متغيرات مختلفة)

مثال:

myname = “Ahmed”

my_name = “Ahmed”

_my_name = “Ahmed”

myName = “Ahmed”

MYNAME = “Ahmed”

myname1 = “Ahmed”

العديد من القيم لمتغيرات متعددة

بيثون يسمح لك لتعيين قيم للمتغيرات متعددة في سطر واحد:

x, y, z = “Orange”, “Banana”, “Cherry”

print(x) # the output will be Orange

print(y) # the output will be Banana

print(z) # the output will be Cherry

ملاحظة: تأكد من أن عدد المتغيرات يطابق عدد القيم ، وإلا فسوف تحصل على خطأ.

قيمة واحدة لمتغيرات متعددة

ويمكنك تعيين نفس القيمة لمتغيرات متعددة في سطر واحد:

x = y = z = “Orange”

print(x) # the output will be Orange

print(y) # the output will be Orange

print(z) # the output will be Orange. unpack a collection

إذا كانت لديك مجموعة من القيم في list, tuple, etc تسمح لك Python باستخراج القيم إلى متغيرات. وهذا ما يسمى unpacking.

fruits = [“apple”, “banana”, “cherry”]

x, y, z = fruits

print(x) # the output will be apple

print(y) # the output will be banana

print(z) # the output will be cherry

Output Variables

غالبا نستخدم وظيفة print لإخراج المتغيرات

‌x = “Python is awesome”

print(x) # the output will be Python is awesome



x = “Python”

y = “is”

z = “awesome”

print(x, y, z) # the output will be Python is awesome



مع الارقام يمكن استخدامه ك آلة حاسبة:

x = 3

y = 2

print(x + y) # the output will be 5. Global Variable

تُعرف المتغيرات التي يتم إنشاؤها خارج دالة (كما في جميع الأمثلة أعلاه) باسم المتغيرات العامة.

يمكن للجميع استخدام المتغيرات العامة ، سواء داخل الوظائف أو خارجها

x = “awesome”

def myfunc():

print(“Python is ” + x)



myfunc() # the output will be Python is awesome

Python Data Types

Python Data Types

Built-in Data Types

في البرمجة ، يعد نوع البيانات مفهومًا مهمًا

يمكن للمتغيرات تخزين البيانات من أنواع مختلفة ، ويمكن للأنواع المختلفة القيام بأشياء مختلفة

:تحتوي Python على أنواع البيانات التالية المضمنة افتراضيًا ، في هذه الفئات

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Getting the Data Type

يمكنك الحصول على نوع البيانات لأي كائن باستخدام دالة type ()

Example Data Type

x = “Hello World” #str
x = 20 #int
x = 20.5 #float
x = 1j #complex
x = [“apple”, “banana”, “cherry”] #list
x = range(6) #range
x = {“name” : “John”, “age” : 36} #dict
x = {“apple”, “banana”, “cherry”} #set
x = frozenset({“apple”, “banana”, “cherry”}) #frozenset
x = True #bool
x = b”Hello” #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview
x = None #NoneType. Setting the Specific Data Type

:إذا كنت تريد تحديد نوع البيانات ، يمكنك استخدام والدوال التالية

x = str(“Hello World”) #str
x = int(20) #int
x = float(20.5) #float
x = complex(1j) #complex
x = list((“apple”, “banana”, “cherry”)) #list
x = tuple((“apple”, “banana”, “cherry”)) #tuple
x = range(6) #range
x = dict(name=”John”, age=36) #dict
x = set((“apple”, “banana”, “cherry”)) #set
x = frozenset((“apple”, “banana”, “cherry”)) #frozenset
x = bool(5) #bool
x = bytes(5) #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview

Python Numbers

Python Numbers

:هناك ثلاثة أنواع رقمية في بايثون

int
float
complex

:يتم إنشاء متغيرات الأنواع الرقمية عندما تقوم بتعيين قيمة لها

x = 1 # int
y = 2.8 # float
z = 1j # complex

للتحقق من نوع أي كائن في Python ، استخدم دالة type ():

print(type(x)) # the output will be <class ‘int’>
print(type(y)) # the output will be <class ‘float’>
print(type(z)) # the oytput will be <class ‘complex’>

Int

.أو العدد الصحيح ، هو عدد صحيح ، موجب أو سالب ، بدون كسور عشرية ، بطول غير محدود Int

x = 1
y = 35656222554887711
z = -3255522. Float

هو رقم ، موجب أو سالب ، يحتوي على واحد أو أكثر من الكسور العشرية float

x = 1.10
y = 1.0
z = -35.59

.يمكن أيضًا أن تكون Float أرقامًا علمية بحرف “e” للإشارة إلى قوة 10

x = 35e3
y = 12E4
z = -87.7e100

Complex

:تتم كتابة الأعداد المركبة بالحرف “j” كجزء تخيلي

x = 3+5j
y = 5j
z = -5j

Type Conversion

يمكنك التحويل من نوع إلى آخر باستخدام الطرق int () و float () و complex ():

x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

ملاحظة: لا يمكنك تحويل الأعداد المركبة إلى نوع رقم آخر

Random Number

لا تحتوي Python على وظيفة random() لإنشاء رقم عشوائي ، لكن لغة Python بها وحدة نمطية مدمجة تسمى random يمكن استخدامها لإنشاء أرقام عشوائية:

import random

print(random.randrange(1, 10)) # the output will be any number between 1 and 10

Python Casting

Python Casting

Specify a Variable Type

قد تكون هناك أوقات تريد فيها تحديد نوع إلى متغير. يمكن القيام بذلك عن طريق casting الصب. Python هي لغة موجهة للكائنات، وبالتالي فهي تستخدم فئات لتحديد أنواع البيانات، بما في ذلك الأنواع البدائية.

:لذلك يتم إجراء عملية casting في Python باستخدام الدوال

int () – يبني عددًا صحيحًا من عدد صحيح حرفي، أو عدد عشري حرفي (عن طريق إزالة جميع الكسور العشرية) ، أو سلسلة حرفية (توفير السلسلة تمثل عددًا صحيحًا).

.float () – يبني عددًا عشريًا من عدد صحيح حرفي، أو عدد عشري حرفي أو سلسلة حرفية (توفير السلسلة تمثل عددًا عشريًا أو عددًا صحيحًا)

.str () – تُنشئ سلسلة من مجموعة متنوعة من أنواع البيانات، بما في ذلك السلاسل النصية والحرفية الصحيحة والحرفية العائمة

Example
Integers:

x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int(“3”) # z will be 3

Example
Floats:

x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float(“3”) # z will be 3.0
w = float(“4.2”) # w will be 4.2

Example
Strings:

x = str(“s1”) # x will be ‘s1’
y = str(2) # y will be ‘2’
z = str(3.0) # z will be ‘3.0’

Get unlimited access to all courses

Get our Paid Courses

We provide a special education service that includes the fields of programming and Data Analytics and Cyber Security and Ethical Hacking

Hurry up to sign up to give you access to paid courses and start your new career immediately