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: |

The gold standard of encryption, known as end-to-end encryption, where only the sender and person who receives the message are able to see it, is available on Telegram only when the Secret Chat function is enabled. Voice and video calls are also completely encrypted. To that end, when files are actively downloading, a new icon now appears in the Search bar that users can tap to view and manage downloads, pause and resume all downloads or just individual items, and select one to increase its priority or view it in a chat. There was another possible development: Reuters also reported that Ukraine said that Belarus could soon join the invasion of Ukraine. However, the AFP, citing a Pentagon official, said the U.S. hasn’t yet seen evidence that Belarusian troops are in Ukraine. Continuing its crackdown against entities allegedly involved in a front-running scam using messaging app Telegram, Sebi on Thursday carried out search and seizure operations at the premises of eight entities in multiple locations across the country. Apparently upbeat developments in Russia's discussions with Ukraine helped at least temporarily send investors back into risk assets. Russian President Vladimir Putin said during a meeting with his Belarusian counterpart Alexander Lukashenko that there were "certain positive developments" occurring in the talks with Ukraine, according to a transcript of their meeting. Putin added that discussions were happening "almost on a daily basis."
from br


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