2.2.1.1 Python literals – the data in itself
Literals are actual data values in source code, like 42 or "Alice". They differ from variables or expressions. For example, in x = 42, 42 is a literal. Literals are self-contained data values, unlike symbols like C, which can mean many things.
2.2.1.2 Python literals – print() example
Using print("2") and print(2) shows two types of literals: string and integer. Although both print the same output, they are stored differently—strings as character sequences, numbers as binary machine values.
2.2.1.3 Python literals – Integers
Integers are whole numbers with no fractional parts. Python recognizes integers by a sequence of digits without extra characters. Underscores (_) can be used for readability (e.g., 11_111_111). Negative numbers use a minus (-), and optional plus (+) can indicate positives. Binary literals start with 0b, like 0b1101, and can also use underscores.
2.2.1.4 Integers – Octal and Hexadecimal
Python supports octal and hexadecimal notation:
Octal: prefix 0o or 0O, uses digits 0–7. 0o123 equals 83 in decimal.
Hexadecimal: prefix 0x or 0X, uses digits 0–9 and A–F. 0x123 equals 291 in decimal.
2.2.1.5 Python literals – Floats
Floats represent numbers with a fractional part, like 2.5 or -0.4. Use a point (.), not a comma. Zeros can be omitted: .4 = 0.4; 4. = 4.0.
2.2.1.6 Ints vs. Floats
4 is an integer, but 4.0 is a float—decimal point makes the difference. Floats can also use scientific notation: 3e8 = 3 × 10⁸. The base before e can be a float or integer, but the exponent must be an integer.
2.2.1.7 Coding Floats
Small float values can be written using scientific notation: 6.62607E-34. Python may present values differently, using a compact form (e.g., 0.0000000000000000000001 → 1e-22).
2.2.1.8 Strings
Strings store text and require quotes: "I am a string.". To include quotes inside a string:
Use escape characters: \" for quotes inside a string delimited by quotes.
Use alternate quote styles: 'I like "Monty Python"'.
2.2.1.9 Coding Strings
To include apostrophes, escape them: 'I\'m Monty Python.' or use double quotes: "I'm Monty Python.". Strings can be empty: '' or "".
2.2.1.10 Boolean Values
Boolean literals represent truth values: True or False. Python uses these to answer logical questions. They are case-sensitive and used in expressions like:
print(True greater than False) # True
print(True less than False) # False
2.2.1.11 LAB – Strings
Objective: Use print() with escape characters and newline to display:
"I'm"
""learning""
"""Python"""
This can be done with a combination of escaped quotes and newline characters.
2.2.1.12 SECTION SUMMARY
Literals are fixed values like 123, "hello" in code.
Binary, Octal, Hexadecimal use base 2, 8, 16 respectively.
Integers are whole numbers (256, -1).
Floats are numbers with decimal parts (1.27, .5).
Quotes or apostrophes inside strings must be escaped or managed with alternate quotes.
Boolean values are True and False, used in logical contexts.
Python also includes None, representing absence of a value.
EXERCISES
Exercise 1: "Hello ", "007" are string literals.
Exercise 2: "1.5" (string), 2.0 (float), 528 (int), False (boolean).
Exercise 3: Binary 1011 = Decimal 11.
Python in Tamil. Learn Python in Tamil from Govi. YouTube Channel: @Python_In_Tamil.
Информация по комментариям в разработке