Print Variable Name in Python

Print Variable Name in Python

For every Python program, a special variable _name_ will be added internally. This variable stores information regarding whether the program is executed as an individual program or as a module. If the program executed as individual program then the value of this variable is _main_. If the program executed as a module from some other program then the value of this variable is the name of module where it is defined.

प्रत्येक पायथन प्रोग्राम के लिए, एक विशेष वैरियेबल _name_ आंतरिक रूप से जोड़ा जाएगा। यह वैरियेबल इस जानकारी को संग्रहीत करता है कि क्या प्रोग्राम को एक इंडीविजुअल प्रोग्राम या एक मॉड्यूल के रूप में निष्पादित किया गया है। यदि प्रोग्राम को इंडीविजुअल प्रोग्राम के रूप में निष्पादित किया जाता है, तो इस वैरियेबल का वैल्यू _main_ है। यदि प्रोग्राम को किसी अन्य प्रोग्राम से मॉड्यूल के रूप में निष्पादित किया जाता है, तो इस वेरिएबल का वैल्यू मॉड्यूल के name है जहां इसे परिभाषित किया गया है।

Hence by using this _name- variable we can identify whether the program executed directly or as a module.

इसलिए इस _name- वैरिएबल का उपयोग करके हम यह पहचान सकते हैं कि प्रोग्राम सीधे निष्पादित हुआ या मॉड्यूल के रूप में।
Example : demo1.py

def fun (): 
if ' name' == ' main ':
print ("The code executed as a program") 
else:
print("the code executed as a module from some other program")
fun ()

test1.py
import demo1
demo1.fun ()

Output:

the code executed as a module from some other program the code executed as a module from some other program

Working with built-in modules
बिल्ट-इन मॉड्यूल के साथ काम करना

Python has a ton of standard modules available. You can check out the full list of Python standard modules and what they are for (listed @ end of this chapter). These files are in the Lib directory inside the location where you installed Python.

पायथन में एक ton स्टैण्डर्ड मॉड्यूल उपलब्ध हैं। आप पायथन स्टैण्डर्ड मॉड्यूल की पूरी सूची की जाँच कर सकते हैं और वे किसके लिए हैं (इस अध्याय के अंत में सूचीबद्ध हैं)। ये फाइलें उस स्थान के अंदर Lib डायरेक्टरी में हैं जहां आपने ‘ पायथन को स्थापित किया था।

Standard modules can be imported the same way as we import our user-defined modules.

स्टैण्डर्ड मॉड्यूल उसी तरह से इंपोर्ट किए जा सकते हैं जैसे हम अपने यूजर-डिफाईन मॉड्यूल को इंपोर्ट करते हैं।

Working with math module: Python provides built-in module math. This module defines several functions which can be used for mathematical operations.

मैथ मॉड्यूल के साथ काम करनाः पायथन बिल्ट-इन मॉड्यूल मैथ प्रदान करता है। यह मॉड्यूल कई फंक्शंस को परिभाषित करता है जिनका उपयोग मैथमेटिकल फंक्शंस के लिए किया जा सकता है।

Example 1:
from math import *
import math
print (“sqrt (4) = “, sqrt (4))
print(“ceil(10.1) = “, ceil(10.1))
print(“floor (10.8) = “, floor (10.8))
print(“fabs (-10.7) = “, fabs (-10.7))
print (“fabs (10.8) = “, fabs (10.8))
print (“module math properties : \n”, dir (math))

Output:
sqrt (4) =  2.0
ceil(10.1) =  11
floor (10.8) =  10
fabs (-10.7) =  10.7
fabs (10.8) =  10.8
module math properties :
[‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘comb’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘dist’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘isqrt’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘perm’, ‘pi’, ‘pow’, ‘prod’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]

Note: We can find help fro any module by using help() function.
नोट: हम help() फंक्शन का उपयोग करके किसी भी माडल को मदद कर सकते है ।

Example 2:
>>>import example
>>>help (example)

Output:
Help on module example:
NAME
    example
FUNCTIONS
    add(a, b)
    mul(a, b)
DATA
    var = 1979
FILE
c:\users\hp\desktop\example.py

Example 3: Write a program to display the date and time using the time module.

आउटपुट 3: टाईम मॉड्यूल का उपयोग करके दिनांक और टाईम प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

import time
localtime = time.asctime (time.localtime (time.time () ) )
print(“Local current time :'”, localtime)

Output:
Local current time :’ Mon Jun 15 14:29:18 2020

Example 4: Write a program that prints the calendar of a particular month.

उदाहरण 4: एक प्रोग्राम लिखें जो किसी विशेष महीने के कैलेंडर को प्रिंट करता है। .

>>>import calendar
>>>print(calendar.month(2020,3))

Output:
     March 2020
Mo Tu We Th Fr Sa Su
                                           1
 2     3    4    5   6    7   8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Chapter wise Model Paper Link

About Me