Telegram Group & Telegram Channel
🎲#گام_به_گام

💻 کدنویسی

6️⃣ دستورات پایه در پایتون: قسمت ششم
🔹 متغیرهای سراسری (Global Variables)
متغیرهایی که خارج از یک تابع ایجاد می‌شوند (مانند تمام مثال‌های پست‌های قبل) به عنوان متغیرهای سراسری شناخته می‌شوند.

◀️ متغیرهای سراسری می‌توانند برای همه استفاده شوند، چه در داخل توابع و چه در خارج آن.

🟣 مثال
یک متغیر خارج از یک تابع ایجاد کنید و از آن در داخل تابع استفاده کنید:
x = "awesome"

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

myfunc()
خروجی پایتون:
Python is awesome

◀️ اگر متغیری با همین نام در داخل یک تابع ایجاد کنید، این متغیر محلی خواهد بود و فقط در داخل تابع قابل استفاده است. متغیر سراسری با همان نام به همان شکلی که بود، سراسری و با مقدار اصلی باقی می‌ماند.

🟣 مثال:
یک متغیر در داخل یک تابع، با همان نام متغیر سراسری ایجاد کنید.
x = "awesome"

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

myfunc()

print("Python is " + x)
خروجی پایتون:
Python is fantastic
Python is awesome

🔴 کلمه کلیدی سراسری (Global keyword)
🟡 به طور معمول، وقتی یک متغیر را در داخل یک تابع ایجاد می‌کنید، آن متغیر محلی است و فقط می‌تواند در داخل آن تابع استفاده شود.

🟡 برای ایجاد یک متغیر سراسری در داخل یک تابع، می‌توانید از کلمه کلیدی global استفاده کنید.

🟣 مثال
اگر از کلمه کلیدی global استفاده می کنید، متغیر به دامنه جهانی تعلق دارد:
def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
خروجی پایتون:
Python is fantastic

◀️ همچنین اگر می‌خواهید متغیر سراسری را در یک تابع تغییر دهید، از کلمه کلیدی global استفاده کنید.
🟣 مثال:
برای تغییر مقدار یک متغیر سراسری در یک تابع، با استفاده از کلمه کلیدی global به متغیر مراجعه کنید:
x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

خروجی پایتون:
Python is fantastic
👈 ادامه دارد....

#️⃣#کدنویسی
#️⃣#IDSchools
#️⃣#IDS
#️⃣#IDS_Math

✉️@IDSchools
✉️@IDS_Math
Please open Telegram to view this post
VIEW IN TELEGRAM



group-telegram.com/IDS_Math/126
Create:
Last Update:

🎲#گام_به_گام

💻 کدنویسی

6️⃣ دستورات پایه در پایتون: قسمت ششم
🔹 متغیرهای سراسری (Global Variables)
متغیرهایی که خارج از یک تابع ایجاد می‌شوند (مانند تمام مثال‌های پست‌های قبل) به عنوان متغیرهای سراسری شناخته می‌شوند.

◀️ متغیرهای سراسری می‌توانند برای همه استفاده شوند، چه در داخل توابع و چه در خارج آن.

🟣 مثال
یک متغیر خارج از یک تابع ایجاد کنید و از آن در داخل تابع استفاده کنید:
x = "awesome"

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

myfunc()
خروجی پایتون:
Python is awesome

◀️ اگر متغیری با همین نام در داخل یک تابع ایجاد کنید، این متغیر محلی خواهد بود و فقط در داخل تابع قابل استفاده است. متغیر سراسری با همان نام به همان شکلی که بود، سراسری و با مقدار اصلی باقی می‌ماند.

🟣 مثال:
یک متغیر در داخل یک تابع، با همان نام متغیر سراسری ایجاد کنید.
x = "awesome"

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

myfunc()

print("Python is " + x)
خروجی پایتون:
Python is fantastic
Python is awesome

🔴 کلمه کلیدی سراسری (Global keyword)
🟡 به طور معمول، وقتی یک متغیر را در داخل یک تابع ایجاد می‌کنید، آن متغیر محلی است و فقط می‌تواند در داخل آن تابع استفاده شود.

🟡 برای ایجاد یک متغیر سراسری در داخل یک تابع، می‌توانید از کلمه کلیدی global استفاده کنید.

🟣 مثال
اگر از کلمه کلیدی global استفاده می کنید، متغیر به دامنه جهانی تعلق دارد:
def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
خروجی پایتون:
Python is fantastic

◀️ همچنین اگر می‌خواهید متغیر سراسری را در یک تابع تغییر دهید، از کلمه کلیدی global استفاده کنید.
🟣 مثال:
برای تغییر مقدار یک متغیر سراسری در یک تابع، با استفاده از کلمه کلیدی global به متغیر مراجعه کنید:
x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

خروجی پایتون:
Python is fantastic
👈 ادامه دارد....

#️⃣#کدنویسی
#️⃣#IDSchools
#️⃣#IDS
#️⃣#IDS_Math

✉️@IDSchools
✉️@IDS_Math

BY ریاضی، آمار و علوم کامپیوتر - مدارس میان‌رشته‌ای


Warning: Undefined variable $i in /var/www/group-telegram/post.php on line 260

Share with your friend now:
group-telegram.com/IDS_Math/126

View MORE
Open in Telegram


Telegram | DID YOU KNOW?

Date: |

Stocks dropped on Friday afternoon, as gains made earlier in the day on hopes for diplomatic progress between Russia and Ukraine turned to losses. Technology stocks were hit particularly hard by higher bond yields. The news also helped traders look past another report showing decades-high inflation and shake off some of the volatility from recent sessions. The Bureau of Labor Statistics' February Consumer Price Index (CPI) this week showed another surge in prices even before Russia escalated its attacks in Ukraine. The headline CPI — soaring 7.9% over last year — underscored the sticky inflationary pressures reverberating across the U.S. economy, with everything from groceries to rents and airline fares getting more expensive for everyday consumers. Individual messages can be fully encrypted. But the user has to turn on that function. It's not automatic, as it is on Signal and WhatsApp. In addition, Telegram's architecture limits the ability to slow the spread of false information: the lack of a central public feed, and the fact that comments are easily disabled in channels, reduce the space for public pushback. "We're seeing really dramatic moves, and it's all really tied to Ukraine right now, and in a secondary way, in terms of interest rates," Octavio Marenzi, CEO of Opimas, told Yahoo Finance Live on Thursday. "This war in Ukraine is going to give the Fed the ammunition, the cover that it needs, to not raise interest rates too quickly. And I think Jay Powell is a very tepid sort of inflation fighter and he's not going to do as much as he needs to do to get that under control. And this seems like an excuse to kick the can further down the road still and not do too much too soon."
from ca


Telegram ریاضی، آمار و علوم کامپیوتر - مدارس میان‌رشته‌ای
FROM American