Table of Contents
TogglePython literals या constants वो notation हैं जो fixed value को source code में represent करते हैं। Variables के विपरीत, literals (जैसे 123, 4.3, “Hello”) static values होते हैं, जो program या application के operation के दौरान नहीं बदलते।
x = 10 # यहाँ 10 एक literal है।
Python में निम्नलिखित literals होते हैं:
Integer literals केवल digit symbols (0 से 9) होते हैं। इसके अलग-अलग प्रकार हैं:
Decimal literals signed या unsigned numbers को represent करते हैं।
x = 10
y = -25
z = 0
Octal number को 0o या 0O से prefix किया जाता है।
x = 0O34 # Octal notation
Hexadecimal numbers 0x या 0X से शुरू होते हैं।
x = 0X1C # Hexadecimal notation
# Using Octal notation
x = 0O34
print("0O34 in octal is", x, type(x))
# Using Hexadecimal notation
x = 0X1c
print("0X1c in Hexadecimal is", x, type(x))
Float literals में integral और fractional part होता है, जो decimal point (.) से अलग होते हैं।
x = 25.55
y = 0.05
z = -12.2345
x = 1.23E5 # 123000.0
x = 1.23E-2 # 0.0123
x = 1.23
print("1.23 in normal float literal is", x, type(x))
x = 1.23E5
print("1.23E5 in scientific notation is", x, type(x))
x = 1.23E-2
print("1.23E-2 in scientific notation is", x, type(x))
Complex number real और imaginary component से बनता है।
x = 2 + 3j
print("2 + 3j complex literal is", x, type(x))
String literals को single quotes (‘hello’), double quotes (“hello”) या triple quotes (”’hello”’ या “””hello”””) में लिखा जाता है।
var1 = 'hello'
var2 = "hello"
var3 = '''hello'''
var4 = """hello"""
var1 = 'hello'
print("'hello' in single quotes is:", var1, type(var1))
var2 = "hello"
print('"hello" in double quotes is:', var2, type(var2))
var3 = '''hello'''
print("'''hello''' in triple quotes is:", var3, type(var3))
var4 = """hello"""
print('"""hello""" in triple quotes is:', var4, type(var4))
List literals को items को comma से अलग करके square brackets [] में लिखा जाता है।
L1 = [1, "Ravi", 75.50, True]
print(L1, type(L1))
Tuple literals को comma से अलग करके parentheses () में लिखा जाता है।
T1 = (1, "Ravi", 75.50, True)
print(T1, type(T1))
Dictionary में key-value pairs होते हैं, जिन्हें curly brackets {} में लिखा जाता है।
capitals = {"USA": "New York", "France": "Paris", "Japan": "Tokyo", "India": "New Delhi"}
print(capitals, type(capitals))
# Dictionary Example
capitals = {"USA": "New York", "France": "Paris", "Japan": "Tokyo", "India": "New Delhi"}
print(capitals, type(capitals))
