By WebNest Studio Engineering Team
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.
Why it matters
A literal is a value written directly in the code, and Python has several ways to write the same kind of value. You will meet hexadecimal numbers in colour codes and file permissions, underscores in large numbers, and raw strings in file paths and regular expressions. Recognising these forms stops them from looking like mistakes.
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
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))
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
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)
Line one
Line two
Name: Asha
C:\new\table
b'hello' <class 'bytes'>
Roses are red,
Python is great.
Boolean, None and collection literals
print(True, False, None)
print(type([]), type(()), type({}), type(set()))
print({1, 2, 2, 3})
print({"course": "Python", "lessons": 130})
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.
Exercise
Print the sum of a binary, a hexadecimal and an octal number, then a large number written with underscores, then a Windows path written as a raw string.
Expected output: 56, 1000000, C:\new (one per line)
# TODO: print the sum of binary 1010, hexadecimal 1F and octal 17
# TODO: print one million, written with underscores for readability
# TODO: print the path C:\new using a raw string
Show hints
- The prefixes are
0bfor binary,0xfor hexadecimal and0ofor octal. - Without the
rprefix,\ninside a string is a line break.
Show solution
print(0b1010 + 0x1F + 0o17)
print(1_000_000)
print(r"C:\new")
Quick quiz
1. What does {} create?
- A. An empty set
- B. An empty tuple
- C. An empty list
- D. An empty dictionary
Show answer
D. An empty set is written set().
2. What is the value of 0x10?
- A. 16
- B. 10
- C. 2
- D. 8
Show answer
A. The 0x prefix means hexadecimal, and 10 in base 16 is sixteen.
3. What is the effect of the underscores in 1_000_000?
- A. None; they only make the number easier to read
- B. They multiply the parts
- C. They make it a string
- D. They cause a syntax error
Show answer
A. Underscores between digits are ignored by Python.
Interview questions
What is a raw string and when is it used?
Show answer
A raw string is written with an r before the opening quote, and in it a backslash is an ordinary character, not the start of an escape sequence. It is used for regular expressions and Windows file paths, where backslashes are frequent and would otherwise each need to be doubled.
What is the difference between a str literal and a bytes literal?
Show answer
A str, written "text", is a sequence of Unicode characters. A bytes object, written b"text", is a sequence of raw bytes, each a number from 0 to 255. Files opened in binary mode and network connections deal in bytes; encode() converts a string to bytes and decode() converts back.
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 in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering