Important Notice:

Literals in Python

Literals in Python

2 views 4 min read

Literals in Python (पायथन में लिटरल्स) :-

Literal (लिटरल) वह निश्चित (Fixed) Value (मान) होती है जिसे Programmer Program में सीधे (Directly) लिखता है। Literal किसी Variable (वैरिएबल) का नाम नहीं होता, बल्कि वह वास्तविक (Actual) Data होता है जिसे Program में उपयोग किया जाता है।

जब भी हम किसी संख्या, शब्द, सत्य/असत्य (True/False) या किसी Collection (संग्रह) को सीधे Program में लिखते हैं, तो उसे Literal कहा जाता है।

उदाहरण के लिए, 10, 3.14, "Python" और True सभी Literals हैं।

English

A Literal is a fixed value written directly in the source code by the programmer. It is the actual data used in a program, not the name of a variable.

Whenever we write a number, text, Boolean value, or collection directly in a program, it is called a Literal.

For example, 10, 3.14, "Python", and True are all literals.

Example (उदाहरण) -

age = 20
name = "Rahul"
pi = 3.14159
is_pass = True
Explanation (व्याख्या) -
  • 20 → Integer Literal
  • "Rahul" → String Literal
  • 3.14159 → Float Literal
  • True → Boolean Literal
Types of Literals (लिटरल्स के प्रकार) -

Python में मुख्य रूप से निम्न प्रकार के Literals होते हैं—

  1. Numeric Literals
  2. String Literals
  3. Boolean Literals
  4. None Literal
  5. Collection Literals

1. Numeric Literals (संख्यात्मक लिटरल) -

जो Literals संख्याओं (Numbers) को दर्शाते हैं, उन्हें Numeric Literals कहते हैं।

Literals that represent numbers are called Numeric Literals.

Numeric Literals चार प्रकार के होते हैं—

  • Integer Literal
  • Float Literal
  • Complex Literal
  • Binary, Octal और Hexadecimal Literals
(a) Integer Literal (पूर्णांक लिटरल) -

Integer Literal वे संख्याएँ होती हैं जिनमें कोई दशमलव (Decimal Point) नहीं होता।

An Integer Literal is a whole number without a decimal point.

Example
a = 100
b = -25
c = 0
(b) Float Literal (दशमलव लिटरल) -

Float Literal वे संख्याएँ होती हैं जिनमें दशमलव (Decimal Point) होता है।

A Float Literal is a number that contains a decimal point.

Example-
pi = 3.14159
price = 99.95
(c) Complex Literal (कॉम्प्लेक्स लिटरल) -

Complex Number में वास्तविक (Real) और काल्पनिक (Imaginary) भाग होता है। Python में Imaginary Part के लिए j का उपयोग किया जाता है।

A Complex Literal consists of a real part and an imaginary part. Python uses j for the imaginary part.

Example-
z1 = 3 + 4j
z2 = 5j
(d) Binary Literal (बाइनरी लिटरल) :-

Binary Number केवल 0 और 1 से बनता है तथा 0b या 0B से शुरू होता है।

A Binary Literal starts with 0b or 0B and contains only 0 and 1.

Example -
x = 0b1010
print(x)
 
Output-
 10

 

(e) Octal Literal (ऑक्टल लिटरल) :-

Octal Number 0o या 0O से शुरू होता है और इसमें केवल 0–7 अंक होते हैं।

An Octal Literal starts with 0o or 0O and contains digits from 0 to 7.

Example -
x = 0o17
print(x)
 
Output -
15
(f) Hexadecimal Literal (हेक्साडेसिमल लिटरल) -

Hexadecimal Number 0x या 0X से शुरू होता है।

इसमें 0–9 तथा A–F अक्षरों का उपयोग किया जाता है।

A Hexadecimal Literal starts with 0x or 0X and uses digits 0–9 and letters A–F.

Example-
x = 0x1A
print(x)
Output -
26
 
2. String Literal (स्ट्रिंग लिटरल) :-

Quotes (' ' या " ") के अंदर लिखे गए Characters (अक्षरों) के समूह को String Literal कहते हैं।

A String Literal is a sequence of characters enclosed in single or double quotes.

Example-
name = "Python"
city = 'Delhi'
print(name)
print(city)
 
Multi-line String Literal :-
message = """
Welcome
to
Python
"""
print(message)
 
output-
Welcome
to
Python
  3. Boolean Literals (बूलियन लिटरल)

Boolean Literal केवल दो Values रखता है

Boolean Literals have only two values:

  • True
  • False

इनका उपयोग Conditions में किया जाता है।

They are used in conditions and logical operations.

Example-
is_student = True
is_login = False
4. None Literal (नन लिटरल)

None एक विशेष (Special) Literal है जो किसी Value के न होने (No Value) को दर्शाता है।

None is a special literal that represents the absence of a value.

Example-

data = None

5. Collection Literals (कलेक्शन लिटरल):-

Collection Literal (कलेक्शन लिटरल) वह Literal होता है जिसका उपयोग एक से अधिक Values (मानों) को एक ही Object (ऑब्जेक्ट) में Store (संग्रहित) करने के लिए किया जाता है।

English

A Collection Literal is a literal used to store multiple values in a single object.

Types of Collection Literals (कलेक्शन लिटरल के प्रकार) -

Python में मुख्य रूप से चार (4) प्रकार के Collection Literals होते हैं—

  1. List Literal (लिस्ट लिटरल)
  2. Tuple Literal (टपल लिटरल)
  3. Set Literal (सेट लिटरल)
  4. Dictionary Literal (डिक्शनरी लिटरल)
1. List Literal (लिस्ट लिटरल) -

List Literal का उपयोग कई Values को Square Brackets [ ] के अंदर Store करने के लिए किया जाता है।

  • List Ordered (क्रमबद्ध) होती है।
  • List Mutable (परिवर्तनीय) होती है अर्थात इसके Elements को बदला जा सकता है।
  • इसमें Duplicate (दोहराए गए) Values की अनुमति होती है।
English

A List Literal stores multiple values inside Square Brackets [ ].

  • Lists are Ordered.
  • Lists are Mutable (modifiable).
  • Duplicate values are allowed.
Example-

numbers = [10, 20, 30, 40]

2. Tuple Literal (टपल लिटरल) -

Tuple Literal का उपयोग कई Values को Parentheses ( ) के अंदर Store करने के लिए किया जाता है।

  • Tuple Ordered (क्रमबद्ध) होती है।
  • Tuple Immutable (अपरिवर्तनीय) होती है अर्थात इसके Elements को बदला नहीं जा सकता।
  • Duplicate Values की अनुमति होती है।
English

A Tuple Literal stores multiple values inside Parentheses ( ).

  • Tuples are Ordered.
  • Tuples are Immutable (cannot be modified).
  • Duplicate values are allowed.
Example -

marks = (85, 90, 95)

3. Set Literal (सेट लिटरल) -

Set Literal का उपयोग कई Unique (अद्वितीय) Values को Curly Braces { } के अंदर Store करने के लिए किया जाता है।

  • Set Unordered (अक्रमबद्ध) होता है।
  • Set में Duplicate Values की अनुमति नहीं होती।
  • Set Mutable होता है, लेकिन उसके Elements Immutable होने चाहिए।
English

A Set Literal stores unique values inside Curly Braces { }.

  • Sets are Unordered.
  • Duplicate values are not allowed.
  • A set is mutable, but its elements must be immutable.
Example-

colors = {"Red", "Green", "Blue"}

4. Dictionary Literal (डिक्शनरी लिटरल) -

Dictionary Literal का उपयोग Key–Value Pair (कुंजी–मान युग्म) के रूप में Data Store करने के लिए किया जाता है।

इसे Curly Braces { } के अंदर लिखा जाता है।

  • प्रत्येक Key Unique (अद्वितीय) होती है।
  • प्रत्येक Key का एक Value होता है।
  • Dictionary Mutable होती है।
English

A Dictionary Literal stores data as Key–Value Pairs.

It is written inside Curly Braces { }.

  • Each key must be unique.
  • Every key has a corresponding value.
  • Dictionaries are mutable.
Example
student = {
    "name": "Rahul",
    "age": 20,
    "city": "Delhi"
}
print(student)
 
output-
{'name': 'Rahul', 'age': 20, 'city': 'Delhi'}

Related Notes