The raw values you write directly in code โ numeric, string, boolean, and the special None โ and how each one is actually represented under the hood.
A literal is raw data (a fixed value) directly given/assigned to a variable in Python. Python has four main categories of literals:
100, "hello", True, None) โ not variables, but what gets stored in variables.True/False) behave like numbers (True = 1, False = 0) in arithmetic.None is Python's special literal representing "no value".NameError.Python supports four numeric literal types: binary, decimal, octal, and hexadecimal integers, plus float and complex literals.
a = 0b1010 # Binary Literal (prefix 0b)
b = 100 # Decimal Literal
c = 0o310 # Octal Literal (prefix 0o)
d = 0x12c # Hexadecimal Literal (prefix 0x)
# Float Literal
float_1 = 10.5
float_2 = 1.5e2 # scientific notation -> 150.0
float_3 = 1.5e-3 # scientific notation -> 0.0015
# Complex Literal
x = 3.14j # 'j' suffix denotes the imaginary part
print(a, b, c, d)
print(float_1, float_2, float_3)
print(x, x.imag, x.real)
Output:
10 100 200 300
10.5 150.0 0.0015
3.14j 3.14 0.0
| Variable | Literal Written | Value Printed | Notes |
|---|---|---|---|
a | 0b1010 | 10 | Binary โ Decimal conversion |
b | 100 | 100 | Plain decimal |
c | 0o310 | 200 | Octal โ Decimal conversion |
d | 0x12c | 300 | Hex โ Decimal conversion |
float_1 | 10.5 | 10.5 | Normal float |
float_2 | 1.5e2 | 150.0 | 1.5 ร 10ยฒ |
float_3 | 1.5e-3 | 0.0015 | 1.5 ร 10โปยณ |
x | 3.14j | 3.14j | Complex number |
x.imag | โ | 3.14 | Imaginary part |
x.real | โ | 0.0 | Real part (0 since only imaginary was given) |
Note: Even though the literals are written in binary/octal/hex, print() always displays the equivalent decimal value.
Strings can be created with single quotes, double quotes, triple quotes (multiline), unicode escapes, or as raw strings.
string = 'This is Python' # single quotes
strings = "This is Python" # double quotes
char = "C" # single character (still a string in Python)
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\U0001f600\U0001F606\U0001F923" # unicode escape sequences (emojis)
raw_str = r"raw \n string" # raw string literal
print(string)
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Output:
This is Python
This is Python
C
This is a multiline string with more than one line code.
๐๐๐คฃ
raw \n string
| Type | Syntax | Purpose |
|---|---|---|
| Single-quoted | 'text' | Basic string |
| Double-quoted | "text" | Basic string (same as single-quoted) |
| Character | "C" | Python has no separate char type โ a single character is just a 1-length string |
| Multiline | """text""" or '''text''' | Allows string to span multiple lines |
| Unicode | u"\U0001f600" | Represents Unicode code points (e.g., emojis) |
| Raw string | r"raw \n string" | Escape sequences like \n are treated as literal characters, not interpreted (i.e., \n prints as \n, not a newline) |
Tip: Raw strings (r"...") are very useful for regex patterns and file paths on Windows, where you don't want\n,\tetc. to be interpreted as escape sequences.
True and False are Python's Boolean literals. Internally, Python treats:
True as 1False as 0This means Booleans can be used directly in arithmetic operations.
a = True + 4 # 1 + 4
b = False + 10 # 0 + 10
print("a:", a)
print("b:", b)
Output:
a: 5
b: 10
Important Point: This works becauseboolis technically a subclass ofintin Python.
NoneNone is Python's special literal used to represent the absence of a value (similar to null in other languages).
a = None
print(a)
Output:
None
NameErrorIf you try to use a variable that hasn't been assigned a value yet, Python raises a NameError.
k
Output:
NameError: name 'k' is not defined
k on its own โ without k = <something> โ caused Python to look up k as an already-existing name, which doesn't exist yet.NameError: name 'k' is not defined.k = None or k = 5.0b), Octal (0o), and Hex (0x) literals all convert to decimal when printed.1.5e2) is a valid float literal format.j suffix; access parts via .real and .imag.char type.""" or ''') are used for multiline strings.r"..." (raw string) disables escape sequence interpretation โ useful for regex/paths.True/False behave as 1/0 in math operations.None represents "no value" โ it is not the same as 0, False, or an empty string.NameError: name 'x' is not defined. Always assign before use.Python literals = the four kinds of raw values you can write directly in code:
0b), decimal, octal (0o), hex (0x), float (incl. e notation), complex (j suffix)."""), unicode escapes (u"\U..."), raw strings (r"..." โ ignores \n etc.).True/False, which act as 1/0 numerically.None, meaning "no value assigned."NameError: name 'x' is not defined.