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 SC urges the public to refer to the SC’s I nvestor Alert List before investing. The list contains details of unauthorised websites, investment products, companies and individuals. Members of the public who suspect that they have been approached by unauthorised firms or individuals offering schemes that promise unrealistic returns If you initiate a Secret Chat, however, then these communications are end-to-end encrypted and are tied to the device you are using. That means it’s less convenient to access them across multiple platforms, but you are at far less risk of snooping. Back in the day, Secret Chats received some praise from the EFF, but the fact that its standard system isn’t as secure earned it some criticism. If you’re looking for something that is considered more reliable by privacy advocates, then Signal is the EFF’s preferred platform, although that too is not without some caveats. Official government accounts have also spread fake fact checks. An official Twitter account for the Russia diplomatic mission in Geneva shared a fake debunking video claiming without evidence that "Western and Ukrainian media are creating thousands of fake news on Russia every day." The video, which has amassed almost 30,000 views, offered a "how-to" spot misinformation. Perpetrators of such fraud use various marketing techniques to attract subscribers on their social media channels. These entities are reportedly operating nine Telegram channels with more than five million subscribers to whom they were making recommendations on selected listed scrips. Such recommendations induced the investors to deal in the said scrips, thereby creating artificial volume and price rise.
from cn


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