Python Literals https://www.skillvertex.com/blog Thu, 11 Apr 2024 11:59:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Python Literals https://www.skillvertex.com/blog 32 32 Python Literals https://www.skillvertex.com/blog/python-literals/ https://www.skillvertex.com/blog/python-literals/#respond Thu, 11 Apr 2024 11:59:59 +0000 https://www.skillvertex.com/blog/?p=6953 Read more]]>

Table of Contents

A Python Literal is a syntax that will denote a value of a particular data type. Literals are constants that will be self-explanatory and don’t require to be evaluated or computed. Further, they will give values or directly use them in the expressions.

However, literals are a system of symbols that have a fixed value in the source code. It is also known as the raw values or the data given in variables or constants. Read this article to learn about the different types of literals in Python.

Types of Literals in Python

Python has several types of literals like numeric literals, string literals, and boolean literals. The different types of literals in Python with examples are provided below:

a. String literals

b. Character literals

c. Numeric Literals’

d. Boolean literals

e. literal collection

f. Special literal

Python String literal

A string is considered a literal and will be made by writing a text that is surrounded by single (“), double (“), and triple quotes. Multi-line strings will be displayed with the triple quotes. The example given below illustrates the Python String literal

# in single quote
s = 'skillvertex'
 
# in double quotes
t = "skillvertex"
 
# multi-line String
m = '''skill
           vertex
               blog'''
 
print(s)
print(t)
print(m)

Output

skillvertex
skillvertex
skill       
                 vertex
                             blog

Python Character Literal

Python character literal is referred to as a type of string literal which has a single character and is surrounded by single or double quotes.

# character literal in single quote
v = 'n'
 
# character literal in double quotes
w = "a"
 
print(v)
print(w)

Output

n
a

Python Numeric literal

The three types of Python numeric literal are provided below

a. Integer

b. Float

c. complex

Integer

Integers are those numbers that consist of both positive and negative numbers which include 0. In the example below, we have provided integer literals (0b10100, 50, 0o320, 0x12b)into different variables.

‘a’ is considered as the binary literal ‘b’ is the decimal literal, ‘c’ is referred to as the octal literal, and ‘d’ is the hexadecimal literal.

# integer literal
 
# Binary Literals
a = 0b10100
 
# Decimal Literal
b = 50
 
# Octal Literal
c = 0o320
 
# Hexadecimal Literal
d = 0x12b
 
print(a, b, c, d)

Output

20 50 208 299

Float

Float are those real numbers that have both integer and fractional parts. For example, 24.8 and 45.0 are the floating-point literals as 24.8 and 45.0 are the floating-point numbers.

# Float Literal
e = 24.8
f = 45.0
 
print(e, f)

Output

24.8 45.0

Complex

These numerals are in the form of a+bj. Whereas, a is considered as the real part and b is the complex part.

z = 7 + 5j
 
# real part is 0 here.
k = 7j
 
print(z, k)

Output

(7+5j) 7j

Python Boolean literal

There are two boolean literals in Python. Those are true and false. True will denote the value as 1 and false will give the value as 0. In the example provided below, a is true and b is false as 1 is equal to true.

a = (1 == True)
b = (1 == False)
c = True + 3
d = False + 7
 
print("a is", a)
print("b is", b)
print("c:", c)
print("d:", d)

Output

a is True
b is False
c: 4
d: 7

Python literal collection

Python has four different types of literal collection

a, list literal

b. tuple literal

c.Dict Literal

d. Set Literal

List Literal

List literal has items of different data types. The values that are stored in the lists will be separated by a comma and will be enclosed within the square brackets. Different types of data can be stored in the list. These lists are mutable.

number = [1, 2, 3, 4, 5]
name = ['Amit', 'kabir', 'bhaskar', 2]
print(number)
print(name)

Output

[1, 2, 3, 4, 5]
['Amit', 'kabir', 'bhaskar', 2]

Tuple Literal

Tuple is referred to as the collection of different data types. It will be enclosed by the parentheses “()” and every element will be separated with a comma and is immutable.

even_number = (2, 4, 6, 8)
odd_number = (1, 3, 5, 7)
 
print(even_number)
print(odd_number)

Output

(2, 4, 6, 8)
(1, 3, 5, 7)

Dictionary Literal

This dictionary will store the data in the key-value pair. It will be enclosed by the curly braces ‘{}’ and every pair will be separated by the comma. Different types of data can be stored in the dictionary. These dictionaries are mutable.

alphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'}
information = {'name': 'amit', 'age': 20, 'ID': 20}
 
print(alphabets)
print(information)

Output

{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'name': 'amit', 'age': 20, 'ID': 20}

Set Literal

Set literal is referred to as the collection of the unordered data set. It will be enclosed by the {} and every element will be separated by the comma.

vowels = {'a', 'e', 'i', 'o', 'u'}
fruits = {"apple", "banana", "cherry"}
 
print(vowels)
print(fruits)

Output

{'o', 'e', 'a', 'u', 'i'}
{'apple', 'banana', 'cherry'}

Python Special LIteral

Python has one special literal which is None. None will define a null variable. However, if the None will be compared with anything else other than None. There is a possibility to get the result as false.

water_remain = None
print(water_remain)

Output

None

Conclusion

To conclude, beginners can learn about Python string literals, Python character literal, and Python numeric literal. The three types of numeric literal are also included in this.

Python Literals -FAQs

Q1. How many literals are there in Python?

Ans. There are five types

Q2. What is the difference between a variable and a literal in Python?

Ans. Literals are the raw values that will be stored in a variable or constant. Whereas, constants are immutable.

Q3. What’s literal programming?

Ans. Literals are referred to as the constant values that are given to constant variables.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/python-literals/feed/ 0