Python Literals are the data that is given to a variable or constant.
Python supports the following literals.
1. String Literals: String Literals can be formed by enclosing a text in a single or double quotes.
Example: 'SUMAN', "10256"
Types of Strings: String Literals are mainly of two types.
Single line string: Strings completed within a single line are called single line string.
Example:
x = 'Hello Python' print x |
---|
Output:
Hello Python |
---|
Multi line string: Strings need more than one line, are called multi line strings. There are two ways to create multi line strings.
A. Using back slash at the end of each line.
Example:
x = 'Hello/ Python' print x |
---|
Output:
HelloPython |
---|
B. Using triple quotation mark.
Example:
x = '''Welcome to Python''' print x |
---|
Output:
Welcome to Python |
---|
2. Numeric Literals: Numeric literals belong to the following four types:
Int (integers): Numbers are positive or negative with no fractional part. Examples are : 100, -876 etc.
Long (Long integers): Integers of unlimited size followed by lowercase or uppercase L. Example: 8764567876L
Float (Floating point): Reals numbers with both integer and fractional part. Example: -897.76
Complex: In the form of a + bj, where a forms real part and b forms the imaginary part of the complex number. Example 3.14J
Example:
x = 0b11001 # Binary Literal y = 1000 # Decimal Literal z = 0o225 # Octal Literal t = 0x25b #Hexadecimal Literal #Float Literals f1= 100.5 f2 = -765.098 #Complex Literal c = 76 + 3.14J print(x, y, z, t) print(f1, f2) print(c, c.real, c.imag) |
---|
Output:
25 1000 149 603 100.5 -765.098 (76 + 3.14J) 76 3.14 |
---|
3. Boolean Literals: A boolean literal can have any of the two values: True or False. False can be represented by '0' or letter F and True can be represented by '1' or any number other than 0 or letter T.
Example of boolean literals:
var1 = 0 var2 = 1 var3 = -8.6 print(bool(var1)) print(bool(var2)) print(bool(var3)) |
---|
Output
False True True |
---|
Other example:
#declaring variables a = 10 b = 20 #comparing variables print(a= =b) |
---|
Output
False |
---|
4. Special Literal: Python contains one special literal i.e. None. None is used to specify to that field that is not created. It is also used for the end of the lists in Python.
Example:
var1 = 100 var2 = None print(var1) print(var2) |
---|
Output
100 None |
---|
5. Literals collections: Python provides the four types of literal collection such as list literals, dictionary collection, tuple literals and set literals.
List literals: List contains items of different data types. Each item is seperated by comma(,) and enclosed within a square brackets[].
Lists are mutable.
Example of list literals:
list=[Andrew Peterson, 35, M] list1=[12354677, Ouee lane, Zurich] print(list) print(list+list1) |
---|
Output
Andrew Peterson, 35, M [Andrew Peterson, 35, M, 12354677, Ouee lane, Zurich] |
---|
Dictionary literals: Python dictionary stores the data in the key value pair. It is enclosed by curly braces{} and each pair is seperated by the commas(,).
Example of dictionary literals:
dic = {'Roll no':01, 'Name':'Ayushmaan', 'Age':14 print(dic) |
---|
Output
'Roll no':01, 'Name':'Ayushmaan', 'Age':14 |
---|
Tuple literals: Python tuple is a collection of different data type. It is immutable, means it can't be modified after creation. It is enclosed by the parentheses () and each element is seperated by the comma.
Example of tuple literals:
tuple = (1, 2, "Ayushmaan Khurrana", [89, 76, 45]) print(tuple) |
---|
Output
Set literals: Python set is the collection of the unordered dataset. It is enclosed by {} and each item is separated by comma (,).
(1, 2, 'Ayushmaan Khurrana', [89, 76, 45] |
---|
Set literals: Python set is the collection of the unordered dataset. It is enclosed by {} and each item is separated by comma (,).
Example of set literals:
set = ('guava', 'grapes', 'papaya', 'apple') print(set) |
---|
Output
('apple', 'guava', 'papaya', 'grapes') |
---|