Course topics

By WebNest Studio

Python Tutorial

Python Literals

A literal is a fixed value written directly in your code: 42, 3.14, "hello", True, None, [1, 2, 3]. Knowing every kind of literal — and the special forms such as binary numbers, raw strings and byte strings — helps you write values exactly the way you mean them.

Numeric Literals

Integers can be written in decimal (255), binary (0b11111111), octal (0o377) or hexadecimal (0xFF); underscores improve readability (1_000_000). Floats use a decimal point or exponent (2.5, 6.02e23). Complex numbers use a j suffix (3 + 4j).

String and Bytes Literals

Strings use single, double or triple quotes (triple quotes span lines). Escape sequences such as \n (newline) and \t (tab) start with a backslash. Prefixes change meaning: r"..." raw strings keep backslashes literally (useful for regex and Windows paths), f"..." f-strings embed expressions, and b"..." creates bytes instead of text.

Boolean, None and Collection Literals

True and False are the Boolean literals, and None represents "no value". Collection literals create lists [1, 2], tuples (1, 2), dictionaries {"a": 1} and sets {1, 2}; note that {} is an empty dict — an empty set is written set().

Examples

Numeric literals in different bases

Python
print(255, 0b11111111, 0o377, 0xFF)
print(1_000_000)
print(2.5, 6.02e23, 1.5e-3)
z = 3 + 4j
print(z, z.real, z.imag, abs(z))
Output
255 255 255 255
1000000
2.5 6.02e+23 0.0015
(3+4j) 3.0 4.0 5.0

String, raw, bytes and multi-line literals

Python
print("Line one\nLine two")
print("Name:\tAsha")
print(r"C:\new\table")          # raw string: backslashes kept
print(b"hello", type(b"hello"))
poem = """Roses are red,
Python is great."""
print(poem)
Output
Line one
Line two
Name:	Asha
C:\new\table
b'hello' <class 'bytes'>
Roses are red,
Python is great.

Boolean, None and collection literals

Python
print(True, False, None)
print(type([]), type(()), type({}), type(set()))
print({1, 2, 2, 3})
print({"course": "Python", "lessons": 130})
Output
True False None
<class 'list'> <class 'tuple'> <class 'dict'> <class 'set'>
{1, 2, 3}
{'course': 'Python', 'lessons': 130}

Common Mistakes

  • Writing Windows paths like "C:\new\file" without a raw string, so \n becomes a newline.
  • Using {} expecting an empty set — it is an empty dict.
  • Writing true/false/none in lowercase; Python literals are True, False, None.
  • Leading zeros in integers (007) are a syntax error; use 7 or 0o7.

Key Points to Remember

  • Integers can be decimal, binary (0b), octal (0o) or hex (0x); underscores aid readability.
  • Floats support exponents; complex numbers use j.
  • String prefixes: r (raw), f (formatted), b (bytes); triple quotes span lines.
  • True, False and None are literals; {} is an empty dict, set() an empty set.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.