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 stores multiple values in sequence.
Each value in the 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.
Lessons in this chapter · Master the Fundamentals of Python
- 1. What is Programming and What Benefits Can You Gain from Learning It?
- 2. Coding and Viewing Your Results in Real-Time
- 3. Are There Levels in Programming Languages?
- 4. Features of Python and Simple Code Example
- 5. Reasons Why Python is Popular
- 6. Why is Indentation Important in Python?
- 7. Printing Output in Python
- 8. Getting User Input in Python
- 9. Multiple-choice quiz
- 10. Statements and Expressions
- 11. Predefined Reserved Words in Python - Keywords
- 12. How to Name Variables and Functions Sensibly
- 13. Explaining Code with Comments
- 14. What are Identifiers?
- 15. A Container for Data, Variables
- 16. Important Points When Assigning Values to Variables
- 17. Symbols for Performing Operations, Operators
- 18. Literals Representing Data
- 19. Coding Quiz - Return 10 Times the Given Number
- 20. Multiple-choice quiz
- 21. Fill-in-the-blank quiz
Which of the following is properly represented as a string literal in Python?
123
[1, 2, 3]
'Programming'
True
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help