Python Literals: Brilliant Fundamentals to Master in 2024

python literals

Python literals are nothing but the notation for constant values of some in-built data types. As per google, in computer programming language, a literal is a notation for representing a fixed value in source code.

String Python Literals

  • Following are valid string literals:
    ‘Mumbai’
    “Delhi”
    “Your’s faithfully”
    ‘He is “very” good’
    ”’He is “very” good”’
    a=’good\
    morning’
    ‘Good\nMorning\nWorld’

Formatting String Literals

  • Formatting needs to prefix string with ‘f’ or ‘F’.
  • It may contain replacement fields or expression delimited by curly brackets { }
  • Formatting is evaluated at run time.
  • In formatting, escape sequences are decoded.
  • Double curly braces {{ or }} are replaces with single curly brace.
  • Conversions are introduced with exclamation point ‘!’
  • Format specifiers are introduced with colon ‘:’
  • Triple quoted strings cannot contain comments.
  • Replacement expression is evaluated in the context where the formatted string literals appears in order from left to right.
  • On the result, ‘!s’ calls str(), ‘!r’ calls repr() and ‘!a’ calls asci()

Examples of String Literal Formatting:

city = "Mumbai"
message = f"He is living in {city}"
print(message)

Output:
'He is living in Mumbai'

value = 123.456789
width = 10
precision = 3
formatted_value = f"The value is {value:{width}.{precision}}"
print(formatted_value)

Output:
'The value is 1.23e+02'

value = 123.456789
width = 10
precision = 4
formatted_value = f"The value is {value:{width}.{precision}}"
print(formatted_value)

Output:
'The value is 123.5'

from datetime import date
bd = date(year=1972, month=10, day=4)
formatted_bd = f"Date of birth is {bd:%B %d, %Y}"
print(formatted_bd)

Output:
'Date of birth is October 04,1972'

a=22/7
print(a)

Output:
3.142857142857143

a = 12.3456  # Define 'a' with a value
formatted_a = '{:0.2f}'.format(a)
print(formatted_a)

Output: 
12.35

a = 3.14159265  # Define 'a' with the value of Pi
formatted_string = 'Value of Pi is {:0.2f}'.format(a)
print(formatted_string)

Output: Value of Pi is 3.14

a = 3.14159265  # Define 'a' with the value of Pi
formatted_string = f'Value of Pi is {a:0.2f}'
print(formatted_string)

Output:'Value of Pi is 3.14'

Numeric Python Literals

  • Numeric literals are of three types:
    1) Integer literals
    2) Floating point literals
    3) Imaginary literals
  • Note that there are no complex literals in Python.
  • Numeric literals do not include sign.

1) Integer Literals

  • This includes decimal, binary, octal and hexadecimal numbers.
  • Decimal includes digits 0 to 9.
  • Octal includes digits 0 to 7.
  • Hexadecimal includes digits 0 to 9 and alphabets a…f and A…F
  • There is no limit for the length of integer literals.
  • We can use underscore(_) in number to increase readability and for grouping digits but are ignored in value.(not allowed in version 3.6)
  • Leading zeros in non-zero decimal number are not allowed.

Examples of Integer Python Literals:

print(12345)

Output: 12345

print(12_568)

Output: 12568

print(0x123)

Output:291

print(0o50)

Output: 40

print(0b1011001)

Output: 89

2) Floating Point Literal

  • Base of floating point numbers is always 10.
  • Like in integer literals, underscore(_) is also allowed in floating point literal for grouping digits. (not allowed in version 3.6).

Example of Floating Point Python Literals:

print(123.456)

Output: 123.456

print(12_34.56_78)

Output:1234.5678

print(1_234.56_78)

Output: 1234.5678

print(0.00001)

Output: 1e-05

print(0.002)

Output: 0.002

print(1.23e3)

Output:1230.0

print(1.23e-3)

Output: 0.00123

print(0e0)

Output:0.0

print(100)

Output: 100.0

print(77e05)

Output:7700000.0

3) Imaginary Literal

  • Imaginary literals are used to represent imaginary part of complex number.
  • An imaginary literals is a complex number with real part 0.0
  • Imaginary literals may be any of integer literal or floating point literal with character j or J.
  • There must not be any white space between number and character j or J.

Example of Imaginary Python Literals:

print(1.23j)

Output: 1.23j

print(5J)

Output: 5j

print(0.02j)

Output: 0.02j

print(.01j)

Output:0.01j

print(1.23e3j)

Output: 1230j

print(1_234.12_34J)

Output:1234.1234j

print(123.111e-4j)

Output: 0.0123111j

Boolean Literal

  • In Python, there are two boolean literals: true and false.
  • These literals represent the values 1 and 0, respectively.
  • In the below example, ‘a’ is true because it is equal to 1, while ‘b’ is false.

Example of Boolean Python Literals:

a=(1==True)
b=(1==False)
c=True+3

print("a is", a)
print("b is", b)
print("c:",c)

Output:
a is True
b is False
c:4

Leave a Reply

Your email address will not be published. Required fields are marked *