Skip to main content

Learning outcomes

By the end of this chapter, you should be able to:
  • define literal and variable clearly
  • identify numeric, string, boolean, and special literals
  • write valid escape sequences in strings

30-minute recording plan

  • 0-7 min: variable vs literal concept
  • 7-15 min: numeric, string, boolean, and None literals
  • 15-22 min: escape sequences and raw strings
  • 22-27 min: mini coding example
  • 27-30 min: exam-focused summary

Quick idea

  • Variable = container name/reference
  • Literal = actual fixed value written in code
  • Program statements usually contain both together

Variable vs literal

  • Variable: name that stores a value
  • Literal: fixed value written directly in code
x = 25
Here:
  • x is variable
  • 25 is literal

Types of literals

Numeric literals

a = 10      # int literal
b = 3.14    # float literal
c = 2+5j    # complex literal
d = 1.5e3   # scientific notation (1500.0)

String literals

name = "Dhruv"
city = 'Delhi'

Boolean literals

is_valid = True
is_empty = False

Special literal

data = None
None means no value / null reference.

Escape sequences in strings

Escape sequence means a backslash-based code inside a string. It is used when normal typing cannot represent formatting/special characters directly.
print("Hello\nWorld")
print("Name:\tDhruv")
print("She said: \"Python\"")
print("Path: C:\\Users\\Dhruv")
What they do:
  • \n: new line
  • \t: horizontal tab space
  • \": include " inside a double-quoted string
  • \\: print one backslash character
If you need many backslashes (common in paths), raw strings are cleaner:
path = r"C:\Users\Dhruv\notes"
print(path)

Mini showcase: invoice line formatter

item = "Notebook"     # string literal
qty = 3               # int literal
price = 49.5          # float literal
discount = 0.10       # float literal

subtotal = qty * price
final = subtotal * (1 - discount)

print(f"Item: {item}")
print(f"Subtotal: {subtotal}")
print(f"Final: {final}")
This example combines variable names with multiple literals in one real calculation.

Exam-focused points

  • literals are constants directly written in source code
  • True, False, and None are keywords
  • string literals can use single or double quotes

Practice questions

  1. Classify these as variable or literal: x, 99, "hi", False.
  2. Write one example each of int, float, bool, and None literal.
  3. Print this exact output using escape sequences:
Name: Dhruv
Branch: CSE