bytes
tutorials
python
python literals
Overview
In Python, a literal 🔢 is a notation representing a fixed value in the code. It is a constant value that doesn't change 🚫 during the execution 💻 of the program. 📚 This lesson will provide 📝 a clear understanding 🧠 of Python literals.
What are Literals?
There was a software engineer named Alie who needed to learn Python. She chosen to begin with the basics; one of the primary things she learned about was literal. 💡 Alie knew that in Python, literals are fixed values that represent constant data in Python code. They are utilized to assign values to variables, define constants, and perform different operations within the code. 💻 She was presented to a few types of literals, such as numeric literals, string literals, boolean literals, None literal, and container literals 📦. To better understand these literals, Alie decided to utilize them in a story. 📖 This lesson will examine diverse types of Python literals and their utilization.
Types of Literals:
Alie knew Python supports several types of literals, including:
In Python, a numeric literal is a notation representing a fixed numeric value in the code. Python has three types of numeric literals: integer literals, floating-point literals, and complex literals.
42 # positive integer
-123 # negative integer
0 # zero
3.14 # positive floating-point number
- 2.5 # negative floating-point number
4.2e-3 # positive floating-point number written in scientific notation
- 2 + 3j # complex number with a real part of 2 and an imaginary part of 3
- 1.5 - 2.5j # complex number with a real part of -1.5 and an imaginary part of -2.5
Python numeric literals are used in mathematical operations, comparisons, and other numeric computations. She started using numeric literal to represent the ages of different characters in her story. For example, she wrote:
alie_age = 25
bobby_age = 30
john_age = 35
- 'hello' # a string literal enclosed in single quotes
- "world" # a string literal enclosed in double quotes
- '''Python is a programming language. It is used for web development, scientific computing,artificial intelligence, and more.''' # a string literal enclosed in triple quotes
Next, Alie used string literals to represent the characters' names in her story. She wrote:
alie_name = 'Alie'
bobby_name = "Bobby"
john_name = '''John'''
- True # a boolean literal representing true
- False # a boolean literal representing false
Alie also used boolean literals to represent whether the characters were friends. She wrote:
alie_and_bob_are_friends = True
bob_and_charlie_are_friends = False
None # a None literal representing null
📝
📝
Container literals represent data structures that can contain other literals.
- [1, 2, 3] # a list literal containing integers
- {'name': 'Alie', 'age': 25} # a dictionary literal containing key-value pairs
- (1, 2, 3) # a tuple literal containing integers
Finally, Alie used container literals to different group types of literals. For example, she used a literal list to represent the different items in Alie's backpack:
alie_backpack = ['book', 'water bottle', 'snacks']
alie_info = {'name': 'Alie', 'age': 25, 'is_friend': True}
bobby_info = {'name': 'Bobby', 'age': 30, 'is_friend': True}
john_info = {'name': 'John', 'age': 35, 'is_friend': False}
These literals are used to represent constant values in Python programs, and they cannot be modified once they are defined.
Conclusion:
In conclusion, Python literals 🔢🔤📃 represent constant values in Python code. They cannot be modified during the execution of the program. Understanding Python literals is crucial for writing efficient and readable code. Python supports different types of literals, such as numeric literals 🔢, string literals 🔡, boolean literals :blue_circle:🔴, None literal :man_shrugging:, and container literals 📦.
Key Takeaways
Quiz
Answer: a) Fixed values that represent constant data in Python code.
Answer: b) 3
Answer: c) In single quotes, double quotes, or triple quotes
Answer: a) A null value
Answer: b) Tuple literals
Answer: d) Container literals