Literals Representing Data
Literals
in programming refer to fixed values directly represented in the code.
In Python, the most commonly used literals include the following:
Number Literals
Number literals are divided into integers (int), floating-point numbers (float), and complex numbers (complex).
123 # Integer literal 3.14 # Float literal
String Literals
Strings are text data enclosed within single quotes ('') or double quotes ("").
"Hello, World!" # String literal enclosed in double quotes 'Python' # String literal enclosed in single quotes
Boolean Literals
Boolean literals represent truth values as True or False.
True # Boolean literal representing true False # Boolean literal representing false
Lists
A list is a data type that holds multiple values in sequence. Each value in a list is separated by a comma (,) and enclosed in square brackets ([]).
[1, 2, 3] # List literal containing the values 1, 2, 3
Tuples
Tuples are similar to lists, but the values they hold cannot be changed once defined. Each value in a tuple is separated by a comma (,) and enclosed in parentheses (()).
(1, 2, 3) # Tuple literal containing the values 1, 2, 3
Dictionary: {'key': 'value'}
Dictionaries are a data type used to structure data. Each key-value pair is separated by a comma (,) and enclosed in curly braces ({}).
{'name': 'Alice', 'age': 24} # Dictionary literal with two key-value pairs: 'name' and 'age'
The detailed aspects of each literal and data type will be introduced in subsequent chapters.
number = 42 # Number literal greeting = "Hello" # String literal is_active = True # Boolean literal
Coding Exercise
In the highlighted part of the coding exercise screen, type my_list = [1, 2, 3]
.
[1, 2, 3]
is a list (literal) which contains the values 1, 2, and 3 in a specific order.
Which of the following is properly represented as a string literal in Python?
123
[1, 2, 3]
'Programming'
True
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result