متغيرات Python

إنشاء المتغير

المتغير هو وعاء يحتوي على قيمة بيانات.

مختلف عن لغات البرمجة الأخرى، لا يحتوي Python على أمر إعلان المتغير.

يتم إنشاء المتغير فقط عند إسناده لأول مرة.

Example

x = 10
y = "Bill"
print(x)
print(y)

Run Instance

لا تحتاج المتغيرات إلى استخدام أي إعلان نوع معين، حتى يمكنك تغيير نوعها بعد إعدادها.

Example

x = 5 # x من نوع int
x = "Steve" # x الآن من نوع str
print(x)

Run Instance

يمكن استخدام علامات الترقيم البسيطة أو المزدوجة لإنشاء متغيرات نصية:

Example

x = "Bill"
# هو نفسه
x = 'Bill'

Run Instance

اسم المتغير

يمكن للمتغير أن يستخدم اسمًا قصيرًا (مثل x و y) أو اسمًا أكثر وضوحًا (مثل age،carname،total_volume).

قواعد تسمية المتغيرات في Python:

  • يجب أن تبدأ أسماء المتغيرات بحرف أو رمز خط عبري
  • لا يمكن أن تبدأ أسماء المتغيرات بالرقم
  • لا يمكن أن تحتوي أسماء المتغيرات على سوى الأحرف الأبجدية والأرقام وخط العبرية (A-z،0-9 و _)
  • تفرق أسماء المتغيرات بين الحروف الكبيرة والصغيرة (age،Age و AGE هي متغيرات مختلفة)

تذكر أن أسماء المتغيرات تفرق بين الحروف الكبيرة والصغيرة

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

يُسمح لـ Python بإسناد قيمة إلى متغيرات متعددة في سطر واحد:

Example

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Run Instance

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

Example

x = y = z = "Orange"
print(x)
print(y)
print(z)

Run Instance

إخراج المتغير

Python print الجملة عادة ما تستخدم للإخراج.

لدمج النص والمتغيرات، يستخدم Python + الرمز:

Example

x = "awesome"
print("Python is " + x)

Run Instance

يمكنك أيضًا استخدام رمز + لتضيف المتغيرات مع متغير آخر:

Example

x = "Python is "
y = "awesome"
z =  x + y
print(z)

Run Instance

For numbers,+ Characters are used as mathematical operators:

Example

x = 5
y = 10
print(x + y)

Run Instance

If you try to combine strings and numbers, Python will give an error:

Example

x = 10
y = "Bill"
print(x + y)

Run Instance

Global Variables

Variables created outside the function (as shown in all the above examples) are called global variables.

Global variables can be used by everyone inside and outside the function.

Example

Create a variable outside the function and use it inside the function:

x = "awesome"
def myfunc():
  print("Python is " + x)
myfunc()

Run Instance

If you create a variable with the same name inside a function, then the variable will be a local variable and can only be used within the function. The global variable with the same name will remain unchanged and retain the original value.

Example

Create a variable with the same name as a global variable inside a function:

x = "awesome"
def myfunc():
  x = "fantastic"
  print("Python is " + x)
myfunc()
print("Python is " + x)

Run Instance

global keyword

Generally, when creating a variable inside a function, the variable is a local variable and can only be used within the function.

To create a global variable inside a function, you can use the global keyword.

Example

If you use the global keyword, then the variable belongs to the global scope:

def myfunc():
  global x
  x = "fantastic"
myfunc()
print("Python is " + x)

Run Instance

Additionally, if you want to change a global variable inside a function, please use the global keyword.

Example

To change the value of a global variable inside a function, please use the global keyword to refer to the variable:

x = "awesome"
def myfunc():
  global x
  x = "fantastic"
myfunc()
print("Python is " + x)

Run Instance