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 |
|---|
| Welcome to Python |
|---|
| 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) |
|---|
| 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.
| var1 = 0 var2 = 1 var3 = -8.6 print(bool(var1)) print(bool(var2)) print(bool(var3)) |
|---|
| False True True |
|---|
| #declaring variables a = 10 b = 20 #comparing variables print(a= =b) |
|---|
| False |
|---|
| var1 = 100 var2 = None print(var1) print(var2) |
|---|
| 100 None |
|---|
| list=[Andrew Peterson, 35, M] list1=[12354677, Ouee lane, Zurich] print(list) print(list+list1) |
|---|
| Andrew Peterson, 35, M [Andrew Peterson, 35, M, 12354677, Ouee lane, Zurich] |
|---|
| dic = {'Roll no':01, 'Name':'Ayushmaan', 'Age':14 print(dic) |
|---|
| 'Roll no':01, 'Name':'Ayushmaan', 'Age':14 |
|---|
| tuple = (1, 2, "Ayushmaan Khurrana", [89, 76, 45]) print(tuple) |
|---|
| (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 (,).
| set = ('guava', 'grapes', 'papaya', 'apple') print(set) |
|---|
| ('apple', 'guava', 'papaya', 'grapes') |
|---|
No comments:
Post a Comment