Python Literals

 Python Literals



Python Literals can be characterized as information that is given in a variable or consistent.

Python upholds the accompanying literals:

1. String literals:

String literals can be framed by encasing a text in the statements. We can utilize both single just as twofold statements to make a string.

1. "Aman" , '12345'

Kinds of Strings:

There are two sorts of Strings upheld in Python:

a) Single-line String-Strings that are ended inside a solitary line are known as Single line Strings.

Model:


1. text1='hello'

b) Multi-line String - A piece of text that is written in numerous lines is known as different lines string.

There are two methods for making multiline strings:

1) Adding dark cut toward the finish of each line.

Model:

1. text1='hello\

2. user'

3. print(text1)

'hellouser'

2) Using triple quotes:-

Model:


1. str2='''''welcome

2. to

3. SSSIT'''

4. print str2

Yield:

welcome

to

SSSIT

II. Numeric literals:

Numeric Literals are unchanging. Numeric literals can have a place with following four distinctive mathematical sorts.

Int(signed integers) Long(long integers) float(floating point) Complex(complex)

Numbers( can be both positive and negative) with no fragmentary part.eg: 100 Integers of limitless size followed by lowercase or capitalized L eg: 87032845L Real numbers with both number and partial part eg: - 26.2 In the type of a+bj where a structures the genuine part and b frames the nonexistent piece of the complicated number. eg: 3.14j

Model - Numeric Literals

1. x = 0b10100 #Binary Literals

2. y = 100 #Decimal Literal

3. z = 0o215 #Octal Literal

4. u = 0x12d #Hexadecimal Literal

5.

6. #Float Literal

7. float_1 = 100.5

8. float_2 = 1.5e2

9.

10. #Complex Literal

11. a = 5+3.14j

12.

13. print(x, y, z, u)

14. print(float_1, float_2)

15. print(a, a.imag, a.real)

Yield:

20 100 141 301

100.5 150.0

(5+3.14j) 3.14 5.0

III. Boolean literals:

A Boolean strict can have any of the two qualities: True or False.

Model - Boolean Literals

1. x = (1 == True)

2. y = (2 == False)

3. z = (3 == True)

4. a = True + 10

5. b = False + 10

6.

7. print("x is", x)

8. print("y is", y)

9. print("z is", z)

10. print("a:", a)

11. print("b:", b)

Yield:

x is True

y is False

z is False

a: 11

b: 10

IV. Exceptional literals.

Python contains one exceptional strict i.e., None.

None is utilized to indicate to that field that isn't made. It is additionally utilized for the finish of records in Python.

Model - Special Literals


1. val1=10

2. val2=None

3. print(val1)

4. print(val2)

Yield:

10

None

V. Exacting Collections.

Python gives the four sorts of strict assortment like List literals, Tuple literals, Dict literals, and Set literals.

List:

o List contains things of various information types. Records are changeable i.e., modifiable.

o The esteems put away in List are isolated by comma(,) and encased inside square brackets([]). We can store various kinds of information in a List.

Model - List literals


1. list=['John',678,20.4,'Peter']

2. list1=[456,'Andrew']

3. print(list)

4. print(list + list1)

Yield:

Word reference:

o Python word reference stores the information in the key-esteem pair.

o It is encased by wavy supports {} and each pair is isolated by the commas(,).

Model


1. dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}

2. print(dict)

Yield:

{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}

Tuple:

o Python tuple is an assortment of various information type. It is permanent which implies it can't be adjusted later creation.

o It is encased by the brackets () and every component is isolated by the comma(,).

Model


1. tup = (10,20,"Dev",[2,3,4])

2. print(tup)

Yield:

(10, 20, 'Dev', [2, 3, 4])

Set:

o Python set is the assortment of the unordered dataset.

o It is encased by the {} and every component is isolated by the comma(,).

Model: - Set Literals


1. set = {'apple','grapes','guava','papaya'}

2. print(set)

Yield:

{'guava', 'apple', 'papaya', 'grapes'}

Post a Comment

0 Comments