home
bytes
tutorials
python
python literals
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.
Python Literals
There was a software engineer named Alie who needed to learn Python. She chose 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 in python, boolean literals, None literal, and container literals 📦. To better understand these literals in python, Alie decided to utilize them in a story. 📖 This lesson will examine diverse types of literals in python and their utilization.
Types of Literals
Alie knew Python supports several types of literals in python, 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
Numeric literals in python 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
String literals in python represent strings of characters in Python. They are enclosed in single quotes, double quotes, or triple quotes. Examples include:
- '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'''
Boolean literals represent the truth values True and False in Python. Examples include:
- 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
The None literal represents a null value in Python. Examples include:
None # a None literal representing null
Container literals represent data structures that can contain other literals in python.
- [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 in python are used to represent constant values in Python programs, and they cannot be modified once they are defined.
In conclusion, Python literals 🔢🔤📃 represent constant values in Python code. They cannot be modified during the execution of the program. Understanding literals in python is crucial for writing efficient and readable code. Python supports different types of literals, such as numeric literals 🔢, string literals 🔡, boolean literals, None literal, and container literals 📦.
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
Related Tutorials to watch
Top Articles toRead
Read