Literals in Python | CBSE Class 12
Introduction
In this tutorial of “Literal values in Python“, we will explore what the Python’s literals are?, as well as the various types of literals available in the Python programming language.
Python literals are constant values that are used to represent data in a program.
Understanding literals in Python is essential for everyone looking to learn and improve their Python programming skills in 2024. Also.
we will cover all the basics of Python literals and their various types, including numeric literals, string literals, and Boolean literals. We will also discuss the Python syntax for literals and provide examples to help you understand how to use them in your code.
After reading this tutorial, You will be having a thorough understanding of Python literals and how to use them effectively in your code to create efficient and effective programs. So let’s get started and explore the world of “Literal values in Python“.
Firstly, we shall be learning about values that are literals.
Basically, Literals are the values that may be given to identifiers.
After allocating the memory location to an identifier we need some values or literals to use by our Python program while execution as you can see in the blow figure.
Now the question arises that how many types of literals are there in Python? that can be used to assign values to the different identifiers
for example
name = “qissba”
age = 32
height = 5.75
status = true
grade = a
Let’s discuss these types of literal values one by one.
Types of Literal values in Python
STRING LITERALS IN PYTHON:
A string literal is a sequence of characters surrounded by quotes and quotes may be single or double and also a string literal can be of the following two types:
- single line string in python: A single line string must be terminated in one line, that is the closing quotes should be in the same line as the open quotes are given.
Example
name = “my name is Qissba.”
- multiline string in python: A multiline string main spread across multiple lines with only one opening and closing quote and each line is ended with an and character\
- Note: no backslash ( / ) is needed if you are using couple quotes for enclosing the springs.
Example
name = “my name /
is qissba”
but name =’’’ my name
is qissba’’’
- Note: In string literal you can use non graphical characters through escape sequences important escape sequences are given in the following table.
TABLE FIGURE
NUMERIC LITERALS IN PYTHON:
Numeric literals are the values which are used to represent the quantity of identifier or variable at the time of execution in a Python program
Example
Age= 32
height=8.5 and
sum = 2 + 3j.
So, the numeric value literals that are used in Python Programming are one of the following types
Integers:
Integer literals are the whole numbers as used in mathematics which are used to represent the value of complete entity like table or chair or person
Example: you cannot purchase that half chair or table
that means integer literals are positive or negative whole numbers with no decimal points these integers literals can be written in following forms in Python programming.
Decimal form:
when an integer is written with the help of digits from 0 to 9 it is called decimal form for example 1249 4002
Octal:
When an integer is written with digits 0 1 2 3 4 5 6 7 and followed by Oz 0 followed by letter O note remember that digit 89 are excluded in this form for example 4035 4074.
Hexadecimal:
When an integer is written with the help of digits from 0 to 9 and letters A to F and also followed by 0X (zero followed by letter X), where.
# Read more on of these NUMBER SYSTEMS in detail. Click Here
FLOATING POINT ITERALS
When we want to represent things with fractional part like 1.5 meter or 3.5 liter we use floating point values and Sporting point values literals represent real number and are written with the decimal point dividing the integer and fractional Parts
for example
age = 30.5
weight = 68.9
- Note that floating point values can also be written in exponent form, for example 17305 64 I will learn in detail.
COMPLEX LITERALS
If a python literal is written in the form like a+bj, then this type of literal is called Complex Literal. Where
a,b are integers or floating point values and
j represents , which is an imaginary value.
Example: We have an equation like
S=3+5j
And we want our python program to solve this by executing it ,then what will be the output of this equation?, obviously here j is a variable that requires an instant value at the time of execution of this equation and, the value you give at execution time (let’s say ) is 2, then the output will be
S=3+5×2 j=2
S=13
But another time, when you execute this program, you may check it for other values of j like 1, 2, 3,4,5,6 ……. and so on.
Then the output will come differ every time. It means the value of S is always depends on the value of j and the value of j may be different and rely on users imagination. Hence this concept makes this form of literal complex because the output always depends on user’s imagination and create a complexity about the final output of this python program.
Finally we can say that , a complex number is made up of two parts
- Real part a ( a is 3)
- Imaginary part bj (b is 5)
BOOLEAN LITERALS IN PYTHON:
Boolean literals in Python is to represent only two values. These are true and false and we can assign only one Boolean value true or false to an identifier for a variable in Python.
SPECIAL LITERALS IN PYTHON:
Similar to the Boolean literals special literals has only one value that is none and this null literal is used to include the absence of any value it mean that there is no value even not is zero for that identifier or variable but a compiled by the variable
for example
house_number = 57
owner = Mr Ashok
Number_of_persons = None
LITERALS COLLECTION IN PYTHON:
In Python, literals collection refers to the different types of literals that can be used to represent different types of data. Python supports several types of literals, including:
- Numeric literals: These are used to represent numeric data types such as integers, floating-point numbers, and complex numbers.
- String literals: These are used to represent textual data and can be enclosed in either single or double quotes.
- Boolean literals: These are used to represent the truth values True and False.
- None literal: This represents the absence of a value.
In addition to these basic literals, Python also supports other types of literals such as:
- List literals: These are used to represent a collection of values, and are enclosed in square brackets.
- Tuple literals: These are similar to list literals, but are enclosed in parentheses and cannot be modified once created.
- Dictionary literals: These are used to represent key-value pairs, and are enclosed in curly braces.
- Set literals: These are used to represent an unordered collection of unique values, and are enclosed in curly braces.
EXAMPLES OF LITERS IN PYTHON
- Numeric literals: These represent numeric values and can be integers, floating-point numbers, or complex numbers. Examples of numeric literals include 42, 3.14, and 2+3j.
- String literals: These represent textual data and can be enclosed in single quotes (‘…’) or double quotes (“…”). Examples of string literals include ‘ Hello, World ‘ and “Python is Fun !“
- Boolean literals: These represent the truth values True and False. Examples of boolean literals include True and False.
- None literal: This represents the absence of a value and is denoted by the keyword None. An example of a None literal is x = None.
EXAM TIME
Here are some important questions related to the topic “Literal values in Python”: for CBSE Class 11 & 12
Question 1: What are literals in Python?
Question 2: What are the different types of literals in Python?
Question 3: How are numeric literals represented in Python?
Question 4: What are string literals in Python?
Question 5: What is a boolean literal in Python?
Question 6: What is the significance of the special literal ‘None’ in Python?
Question 7: What are literal collections in Python?
Question 8: How are literal collections used in Python programming?
Question 9: Can literals be modified in Python?
Question 10: How do literals differ from variables in Python?
Question 11: What are Python literals and variable?
Also Read Other Topics for Computer Science Class 11th and 12th
How to Learn Python free online
Tag:cbse computer science class 12, free python tutorial, How to learn python programming, How to learn python programming at home, How to learn python programming FREE, How to learn python programming STEP BY STEP, Introduction to literals in python, Literals In Python, Python Data Types, What are literals in python