What are Literals in Python and explain about different Literals?
- Naveen
- 0
A literal in python source code represents a fixed value for primitive data types. There are 5 types of literals in Python-
String literals
A string literal is created by assigning some text enclosed in single of double quotes to a variable. To create multiline literals, assign the multiline text enclosed in triple quotes. E.g., Name = ‘Tanya’
A character literal
It is created by assigning a single in triple quotes. e.g., A = ‘p’.
Numeric literal
It include numeric values that can be either integer, floating point value, or a complex number. e.g., A = 40.
Boolean literal
These can be 2 values – True or False.
Literal Collections
These are of 4 types.
A) – List collections – e.g. A = [1, 3, 5, ‘Amit’]
B) – Tuple literals – e.g. A = (4, 5, 6, 7).
C) – Dictionary literals – e.g., dict = {1: ‘apple’, 2: ‘mango’, 3:’banana’}.
D) – Set literals – e.g. {‘Tanya’, ‘Rohit’, ‘Sandesh’}.
Special literal
Python has 1 special literal None which is used to return a null variable.
Popular Posts
Author
-
Naveen Pandey has more than 2 years of experience in data science and machine learning. He is an experienced Machine Learning Engineer with a strong background in data analysis, natural language processing, and machine learning. Holding a Bachelor of Science in Information Technology from Sikkim Manipal University, he excels in leveraging cutting-edge technologies such as Large Language Models (LLMs), TensorFlow, PyTorch, and Hugging Face to develop innovative solutions.
View all posts