अजगर चर, लगातार और साहित्य

इस ट्यूटोरियल में, आप पायथन चर, स्थिरांक, शाब्दिक और उनके उपयोग मामलों के बारे में जानेंगे।

वीडियो: अजगर चर और प्रिंट ()

अजगर चर

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

 number = 10 

यहां, हमने एक वैरिएबल नंबर बनाया है। हमने वैरिएबल को 10 मान दिया है।

आप इसमें पुस्तकों को संग्रहीत करने के लिए चर के बैग के रूप में सोच सकते हैं और उस पुस्तक को किसी भी समय बदला जा सकता है।

 number = 10 number = 1.1 

प्रारंभ में, संख्या का मान 10 था। बाद में, इसे बदलकर 1.1 कर दिया गया था।

नोट : पायथन में, हम वास्तव में चर के मानों को निर्दिष्ट नहीं करते हैं। इसके बजाय, पायथन चर को वस्तु (मूल्य) का संदर्भ देता है।

पायथन में वेरिएबल्स को मान निर्दिष्ट करना

जैसा कि आप ऊपर दिए गए उदाहरण से देख सकते हैं, आप =किसी वेरिएबल को वैल्यू असाइन करने के लिए असाइनमेंट ऑपरेटर का उपयोग कर सकते हैं ।

उदाहरण 1: एक वैरिएबल के लिए मूल्य घोषित करना और असाइन करना

 website = "apple.com" print(website) 

आउटपुट

 सेब। com 

उपरोक्त कार्यक्रम में, हमने वैरिएबल वेबसाइट को एक मान apple.com सौंपा है। फिर, हमने वेबसाइट यानी apple.com को दी गई वैल्यू को प्रिंट किया

नोट : पायथन एक प्रकार की अनुमान-आधारित भाषा है, इसलिए आपको चर प्रकार को स्पष्ट रूप से परिभाषित करने की आवश्यकता नहीं है। यह स्वचालित रूप से जानता है कि apple.com एक स्ट्रिंग है और वेबसाइट चर को एक स्ट्रिंग के रूप में घोषित करता है।

उदाहरण 2: परिवर्तनशील का मान बदलना

 website = "apple.com" print(website) # assigning a new value to website website = "programiz.com" print(website) 

आउटपुट

 apple.com programiz.com 

उपर्युक्त कार्यक्रम में, हमने ऐप्पल.कॉम को शुरू में वेबसाइट वेरिएबल को सौंपा है। फिर, value को programiz.com में बदल दिया जाता है।

उदाहरण 3: एकाधिक चर के लिए कई मान निर्दिष्ट करना

 a, b, c = 5, 3.2, "Hello" print (a) print (b) print (c) 

यदि हम एक ही मूल्य को एक से अधिक वेरिएबल में असाइन करना चाहते हैं, तो हम यह कर सकते हैं:

 x = y = z = "same" print (x) print (y) print (z) 

दूसरा प्रोग्राम सभी तीन चर x, y और z को एक ही स्ट्रिंग प्रदान करता है।

लगातार

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

आप कुछ पुस्तकों को संग्रहीत करने के लिए एक बैग के रूप में स्थिरांक के बारे में सोच सकते हैं जिन्हें बैग के अंदर एक बार प्रतिस्थापित नहीं किया जा सकता है।

पायथन में स्थिर करने के लिए मान निर्दिष्ट करना

पायथन में, स्थिरांक आमतौर पर एक मॉड्यूल में घोषित और असाइन किए जाते हैं। यहां, मॉड्यूल एक नई फ़ाइल है जिसमें चर, फ़ंक्शन आदि मुख्य फ़ाइल में आयात किए जाते हैं। मॉड्यूल के अंदर, स्थिरांक सभी बड़े अक्षरों में लिखे गए हैं और शब्दों को अलग करने वाले अंडरस्कोर हैं।

उदाहरण 3: मूल्य की घोषणा करना और उसे स्थिर करना

एक निरंतर बनाएँ :

 PI = 3.14 GRAVITY = 9.8 

एक मेनहोम बनाएँ :

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

आउटपुट

 3.14 9.8 

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

Note: In reality, we don't use constants in Python. Naming them in all capital letters is a convention to separate them from variables, however, it does not actually prevent reassignment.

Rules and Naming Convention for Variables and constants

  1. Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:
     snake_case MACRO_CASE camelCase CapWords
  2. Create a name that makes sense. For example, vowel makes more sense than v.
  3. If you want to create a variable name having two words, use underscore to separate them. For example:
     my_name current_salary
  4. Use capital letters possible to declare a constant. For example:
     PI G MASS SPEED_OF_LIGHT TEMP
  5. Never use special symbols like !, @, #, $, %, etc.
  6. Don't start a variable name with a digit.

Literals

Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as follows:

Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types: Integer, Float, and Complex.

Example 4: How to use Numeric literals in Python?

 a = 0b1010 #Binary Literals b = 100 #Decimal Literal c = 0o310 #Octal Literal d = 0x12c #Hexadecimal Literal #Float Literal float_1 = 10.5 float_2 = 1.5e2 #Complex Literal x = 3.14j print(a, b, c, d) print(float_1, float_2) print(x, x.imag, x.real) 

Output

 10 100 200 300 10.5 150.0 3.14j 3.14 0.0 

In the above program,

  • We assigned integer literals into different variables. Here, a is binary literal, b is a decimal literal, c is an octal literal and d is a hexadecimal literal.
  • When we print the variables, all the literals are converted into decimal values.
  • 10.5 and 1.5e2 are floating-point literals. 1.5e2 is expressed with exponential and is equivalent to 1.5 * 102.
  • We assigned a complex literal i.e 3.14j in variable x. Then we use imaginary literal (x.imag) and real literal (x.real) to create imaginary and real parts of complex numbers.

To learn more about Numeric Literals, refer to Python Numbers.

String literals

A string literal is a sequence of characters surrounded by quotes. We can use both single, double, or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes.

Example 7: How to use string literals in Python?

 strings = "This is Python" char = "C" multiline_str = """This is a multiline string with more than one line code.""" unicode = u"u00dcnicu00f6de" raw_str = r"raw string" print(strings) print(char) print(multiline_str) print(unicode) print(raw_str) 

Output

 This is Python C This is a multiline string with more than one line code. Ünicöde raw string 

In the above program, This is Python is a string literal and C is a character literal.

The value in triple-quotes """ assigned to the multiline_str is a multi-line string literal.

The string u"u00dcnicu00f6de" is a Unicode literal which supports characters other than English. In this case, u00dc represents Ü and u00f6 represents ö.

r"raw string" is a raw string literal.

Boolean literals

A Boolean literal can have any of the two values: True or False.

Example 8: How to use boolean literals in Python?

 x = (1 == True) y = (1 == False) a = True + 4 b = False + 10 print("x is", x) print("y is", y) print("a:", a) print("b:", b) 

Output

 x is True y is False a: 5 b: 10 

In the above program, we use boolean literal True and False. In Python, True represents the value as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value of y is False because 1 is not equal to False.

Similarly, we can use the True and False in numeric expressions as the value. The value of a is 5 because we add True which has a value of 1 with 4. Similarly, b is 10 because we add the False having value of 0 with 10.

Special literals

Python contains one special literal i.e. None. We use it to specify that the field has not been created.

Example 9: How to use special literals in Python?

 drink = "Available" food = None def menu(x): if x == drink: print(drink) else: print(food) menu(drink) menu(food) 

Output

 Available None 

In the above program, we define a menu function. Inside menu, when we set the argument as drink then, it displays Available. And, when the argument is food, it displays None.

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

Example 10: How to use literals collections in Python?

 fruits = ("apple", "mango", "orange") #list numbers = (1, 2, 3) #tuple alphabets = ('a':'apple', 'b':'ball', 'c':'cat') #dictionary vowels = ('a', 'e', 'i' , 'o', 'u') #set print(fruits) print(numbers) print(alphabets) print(vowels) 

Output

 ('apple', 'mango', 'orange') (1, 2, 3) ('a': 'apple', 'b': 'ball', 'c': 'cat') ('e', 'a', 'o', 'i', 'u') 

In the above program, we created a list of fruits, a tuple of numbers, a dictionary dict having values with keys designated to each value and a set of vowels.

To learn more about literal collections, refer to Python Data Types.

दिलचस्प लेख...