If we go by the dictionary meaning ‘Variable’ is something which is ‘able to be changed or adapted’. Which is true to much extent in terms of Python programming language also. Variable is basically a reference to the memory location where an object is stored by a computer program. The names which we use for the variables and even for the programs/classes etc. are called identifiers and there are certain rules which we must follow while naming such identifiers:-
They can be of any length.
They can contain any number of uppercase alphabets(A-Z),lowercase alphabets(a-z), digits(0-9) and underscore(_).
However they must start with an alphabet or an underscore i.e. we can not use the digits at the very beginning e.g. ‘_a_valiable’ and ‘onevariable’ are allowed, but ‘1variable’ is not allowed.
The identifiers are case sensitive i.e. ‘a_variable’ and ‘A_variable’ are different identifiers.
There are certain words which are reserved for special use in Python. Such words are called keywords. Identifiers can not be keywords. The following image contains the list of all the keywords. You needn’t cram all of them. Open an iteractive shell and type following code to get the list of keywords:-
help()
keywords
Assigning values to the Variables
Unlike C and C++ you do not need to declare the variables and its data type at the very beginning. If you need a variable at any time in your program, just think of a name and assign a value to it and start using it and the interpreter will automatically detect the data-type e.g.
a_variable = "hello"
Watch our one minute video on variables and identifiers in python
One thing you need to note here is that the equal”=” sign should not be interpreted as “is equal to” however it should be read as “is set to” because we use “==” as a comparison operator to see the value is equal to something.
You can assign values to single variable or mutliple variable at a time.
x = "single assignment"
y,z = "multiple assignments 1", "mutliple assignments 2"
print(x)
print(y)
print(z)
Output will be:-
single assignment
multiple assignments 1
mutliple assignments 2
Another salient feature of the variable in Python is that its value as well as data-type can be changed at any time in the program. In the below example, the variable is first set to 28 then changed to 29 and thereafter to 29.1 (which also changed its type to float) and finally it was changed into a string with value ‘a_string’.
Datatypes in Python
The standard Data-Types of Python are:-
Numbers - They can be integer, long integer (Python 2.7 only. They have been discontinued in Python 3), float or complex number.
Stings
Lists
Tuples
Dictionaries
Boolean - True and False.
We will be learning more about each data-type in the upcoming lessons.
अगर हम डिक्शनरी शब्द के अर्थ पर चलते हैं, तो ‘Variable’ एक ऐसी चीज है, जिसको बदला जा सकता है। जो कि Python के संदर्भ में भी काफी हद तक सही है। Variable मूल रूप से मेमोरी लोकेशन का एक संदर्भ है जहां एक कंप्यूटर प्रोग्राम द्वारा किसी ऑब्जेक्ट को स्टोर किया जाता है। वे नाम जो हम ‘Variable’ व function/class इत्यादि के लिए इस्तमाल करते है उन्हें identifiers कहा जाता है और ऐसे कुछ नियम हैं जिनका हमें ऐसे identifiers का नामकरण करते समय पालन करना चाहिए: -
वे किसी भी लम्बाई के हो सकते हैं।
वे अपरकेस अल्फाबेट्स (A-Z), लोअरकेस अल्फाबेट्स (a-z), अंकों (0-9) और अंडरस्कोर (_) के किसी भी संख्या को शामिल कर सकते हैं।
हालाँकि उन्हें एक alphabet या एक अंडरस्कोर के साथ शुरू करना चाहिए यानी हम शुरुआत में अंकों का उपयोग नहीं कर सकते हैं। ‘\ _a \ _valiable’ और ‘onevariable’ की अनुमति है, लेकिन ‘1variable’ की अनुमति नहीं है।
Identifiers case-sensitive होते हैं यानी ‘a \ _variable’ और ‘A \ _variable’ अलग-अलग identifier हैं।
कुछ शब्द ऐसे हैं जो Python में विशेष उपयोग के लिए आरक्षित हैं। ऐसे शब्दों को keywords कहा जाता है। पहचानकर्ता कीवर्ड नहीं हो सकते। निम्न छवि में सभी कीवर्ड की सूची है। आपको उन सभी को रटने की ज़रूरत नहीं है। कीवर्ड की सूची प्राप्त करने के लिए interactive shell खोलें और निम्न कोड टाइप करें: -
help()
keywords
Variables को value देना
C और C++ के विपरीत आपको शुरुआत में variable और इसके डेटा प्रकार की घोषणा करने की आवश्यकता नहीं है। यदि आपको अपने program में किसी भी समय एक variable की आवश्यकता है, तो बस एक नाम के बारे में सोचें और उसके लिए एक मूल्य निर्दिष्ट करें और इसका उपयोग करना शुरू करें और interpreter अपने आप Data Type का पता लगा लेगा।
a_variable = "hello"
यहाँ आपको एक बात का ध्यान रखना होगा के बराबर “=” साइन की व्याख्या “बराबर है” (“is equal to”) नहीं है बलके “is set to” है क्यूँकि समानता देखने के लिए हम “==” का इस्तमाल करते है।
आप एक बार में एक variable या एक साथ कई variables को value assign कर सकते है।
x = "single assignment"
y,z = "multiple assignments 1", "mutliple assignments 2"
print(x)
print(y)
print(z)
Output will be:-
single assignment
multiple assignments 1
mutliple assignments 2
Python में variable की एक अन्य मुख्य विशेषता यह है कि program में किसी भी समय इसके value के साथ-साथ data type को भी बदला जा सकता है। नीचे दिए गए उदाहरण में, variable को पहले 28 पर सेट किया गया और फिर 29 में बदल दिया गया और उसके बाद 29.1 (जिसने इसका प्रकार भी बदल दिया) और अंत में इसे ‘a_string’ मान के साथ एक string में बदल दिया गया।
Data Types
Python के मूल data types है:-
Numbers - जो कि integer, long integer (केवल Python 2.7 में। Python 3 में discontinue कर दिए गए हैं), float यां complex number हो सकता है।
Stings
Lists
Tuples
Dictionaries
Boolean - True ओर False.
हम आने वाले chapters में प्रत्येक डेटा-प्रकार के बारे में अधिक सीखेंगे।
Python unittest module - How to test your python code?
Python unittest module
Why to unit test your python source code?
All programmers want their code to be impeccable, but as the saying goes, to err is human, we make mistakes and leave bugs in our source code. Here is where the unit testing comes to our rescue. If you ...
In our previous chapter, we have strongly recommended you to ditch IDLE and use some code-editor and terminal(MacOS/Linux) or Powershell(Windows) to write and execute your python script. In this chapter we will tell you how to do that.