Variable

Variable 

A variable is a name which is used to refer memory location and used to hold value. In Python, variable also called as an identifier.

एक वैरिएबल एक ऐसा नाम है, जिसका उपयोग मेमोरी लोकेशन को संदर्भित करने और वैल्यू को होल्ड करने के लिए किया जाता है। पायथन में, वैरियेबल को आईडेंटिफायर भी कहा जाता है।

How to declare a variable – एक वैरियेबल को कैसे घोषित करें?

In C or C++ or Java:
int a = 100;
float f = 10.5f;

Means here we are declaring a variable ‘a’ with the value ‘100’ of type ‘int’ and we are declaring another variable ‘f with the value ‘10.5f of type float. Like that we have to declare a variable. Otherwise in compile time, compiler shows an error message.

यहाँ पर हम एक वैरियेबल की घोषणा कर रहें हैं ‘a’ जिसका मान ‘100’ है जिसका प्रकार ‘int’ और हम एक और वैरिएबल ‘f’ की घोषणा कर रहे हैं जिसका मान ‘10.5f’ फ्लोट प्रकार का है। उस तरह हमें एक वैरियेबल घोषित करनाहोगा। अन्यथा कंपाइल करते समय कंपाइल एक एरर मैसेज दिखाता है।

But in Python – लेकिन पायथन में
a = 100

Here we declare variable ‘a’ with the value ‘100’. We are not mentioned of type of variable. In Python, we are not required to declare a variable of type explicitly. Based on your provided value, the type will be considered automatically by the Python virtual machine. Such types of programming languages are called ‘dynamically typed languages’. Best examples of dynamically typed languages are Python, JavaScript, Ruby, Lisp and Tcl etc. But in C or C++ or Java language compulsory we have to declare a variable of type explicitly, so that, these languages are called statically typed languages.

यहाँ हम ‘100’ वैल्यू के साथ वैरियेबल ‘a’ घोषित करते हैं। हम वैरियेबल के प्रकार का उल्लेख नहीं कर रहे हैं। पायथन में, हमें स्पष्ट रूप से एक प्रकार का वैरियेबल घोषित करने की आवश्यकता नहीं है। आपके प्रदान किए गए वैल्यू के आधार पर, type को पायथन वर्चुअल मशीन द्वारा ऑटोमेटिक रूप से माना जाएगा। इस प्रकार की प्रोग्रामिंग भाषाओं को ‘डॉयनेमिक रूप से टाइप्ड लैंग्वेज कहा जाता है। डायनामिक टाइप्ड भाषाओं के सर्वश्रेष्ठ उदाहरण पायथन, जावास्क्रिप्ट, रूबी, लिस्प और टेल आदि हैं, लेकिन C या C++ या जावा भाषा में हमें अनिवार्य रूप से एक वेरिएबल घोषित करना होगा, ताकि, इन भाषाओं को स्टेटिकली टाइप्ड भाषाएं कहा जा सके।

Let us understand through some example – हम कुछ उदाहरण के माध्यम से समझते हैं

>>> x = 100 
>>> type (x) # In Python, type is a function to know about type of variable 

Result: – परिणाम

>>><class ‘int’>

Important points: –महत्वपूर्ण बिन्दु

  • Variables are created when first assigned.

वैरियेबल तब बनाई जाती हैं जब पहली बार असाइन किया जाता है।

  • Variables must be assigned before being referenced.

संदर्भित होने से पहले वैरियेबल को असाइन किया जाना चाहिए।

  • The value stored in a variable can be accessed or updated later.

एक वैरियेबल में संग्रहीत मूल्य को बाद में एक्सेस या अपडेट किया जा सकता है।

  • No declaration is required

किसी घोषणा की आवश्यकता नहीं है।

  • The type (string, int, float etc.) of the variable is determined by Python. The interpreter allocates memory on the basis of the data type of a variable.

वैरियेबल का प्रकार (स्ट्रिंग, इंट, फ्लोट आदि) पायथन द्वारा निर्धारित किया जाता है। इंटरप्रेटर एक वैरियेबल के डेटा प्रकार के आधार पर मेमोरी आवंटित करता है।

Literal- लिटरल

Literal is a raw data given in a variable or constant. Literals can be defined as a data that is given in a variable or constant.

लिटरल एक वैरियेबल या कान्सटेन्ट में दिया गया एक कच्चा डेटा है। लिटरल्स को एक डेटा के रूप में परिभाषित किया जा सकता है जो एक वैरियेबल या कान्सटेन्ट में दिया जाता है।

Python support the following literals- पायथन निम्नलिखित लिटरल का समर्थन करता है

  1. String literals: String literals can be formed by enclosing a text in the quotes. We can use both

single as well as double quotes for a String.

स्ट्रिंग लिटरल कोट्स में एक टेक्स्ट संलग्न करके बनाया जा सकता है। हम एक स्ट्रिंग के लिए सिंगल और साथ ही दोहरे कोट्स चिह्नों का उपयोग कर सकते हैं।

Example: उदाहरण

name='P Tindale' 
fName="A K Tindale" 
print (name) 
print (fName)

Output:
P Tindale
A K Tindale

  1. Numeric literals: Number stores numeric values. Python creates Number objects when anumber is assigned to a variable.

संख्या संख्यात्मक मान संग्रहीत करती है। जब एक वैरियेबल के लिए एक नंबर असाइन किया जाता है तो पायथन नंबर ऑब्जेक्ट बनाता है।

For example:
a = 30, b = 50
Constants – कन्सटेंट

A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later.

एक कन्सटेंट एक प्रकार का वैरियेबल है जिसकी वैल्यू को बदला नहीं जा सकता है। यह उन कंटेनरों के बारे में सोचने में मददगार होता है, जो कन्सटेंट्स की जानकारी रखते हैं, जिन्हें बाद में बदला नहीं जा सकता है।

Example: Declaring and assigning value to a constant

एक कन्सटेंट के लिए मूल्य घोषित करना और असाइन करना।
Create a constant.py

PI = 3.14
GRAVITY = 9.8 

Create a main.py

import constant 
print (constant.PI) 
print (constant.GRAVITY)

When you run the program, the output will be:

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा
3.14
9.8

In the above program, we create a constant.py module file. Then, we assign the constant value to PI and GRAVITY. After that, we create a main.py file and import the constant module. Finally, we print the constant value.

उपरोक्त कार्यक्रम में, हम एक constant.py मॉड्यूल फाइल बनातें हैं। फिर हम PI और GRAVITY को कान्सटेन्ट वैल्यू असाइन करते हैं। उसके बाद, हम एक main.py फाइल बनाते हैं और कॉन्सटेंट मॉड्यूल को इंपोर्ट करते हैं। अंत में, हम कॉन्सटेन्ट वैल्यू को प्रिंट करते हैं।

Note: In reality, we don’t use constants in Python. The global or constants module is used throughout the Python programs.

वास्तव में, हम पायथन में कॉन्सटेंट का उपयोग नहीं करते हैं। ग्लोबल्स या कॉन्सटेन्ट मॉड्यूल का उपयोग पायथन प्रोग्रामों में किया जाता है।

Chapter wise Model Paper Link

About Me