Saturday, November 16, 2019

Python Literals

Python Literals


- Literals define set of rules to represents value.
- A literals is a function for representing a fixed value.

- Python support two types of literals.
1. Numeric Literals.
2. Non-Numeric Literals.

1. Numeric Literals.
a. Integer Literals
>>> x=100
>>> print("Numeric literal is: ",x)
Numeric literal is:  100

b. Floating point Literals
>>> y=100.23
>>> print("Numeric Float literal is: ",y)
Numeric Float literal is:  100.23

c. List

d. Complex
>>> z=complex(1,2)
>>> print("Numeric Complex literal is: ",z)
Numeric Complex literal is:  (1+2j)

2. Non-Numeric Literals.
a. String Literals
>>> Mystr="wercopypastedeveloper.blogspot.com"
>>> print("String literal is: ",Mystr)
String literal is:  wercopypastedeveloper.blogspot.com

b. Escape Sequence.

3. Boolean literals.
a. True.
>>> a=True
>>> print("Boolean literal is: ",a)
Boolean literal is:  True

b. False.
>>> a=False
>>> print("Boolean literal is: ",a)
Boolean literal is:  False

4. Special literals.
In Python is same as “null”, means, non-existent not known or empty.

5. Literal Collections.

a. Tuples.

b. Lists

c. Sets

d. Dictionaries
>>> b=[1,2,3,4]
>>> print("List literal is: ",b)
List literal is:  [1, 2, 3, 4]
>>> c=(1,2,3,4)
>>> print("Tuple literal is: ",c)
Tuple literal is:  (1, 2, 3, 4)
>>> d={1,2,2,2,3,4}
>>> print( literal is: ",c)
>>> print(" literal is: ",d)
 literal is:  {1, 2, 3, 4}
>>> e={1:"ONE",2:"TWO"}
>>> print("Dict literal is: ",e)
Dict literal is:  {1: 'ONE', 2: 'TWO'}

Example:
>>> x=100
>>> print("Numeric literal is: ",x)
Numeric literal is:  100
>>> y=100.23
>>> print("Numeric Float literal is: ",y)
Numeric Float literal is:  100.23
>>> z=complex(1,2)
>>> print("Numeric Complex literal is: ",z)
Numeric Complex literal is:  (1+2j)
>>> Mystr="wercopypastedeveloper.blogspot.com"
>>> print("String literal is: ",Mystr)
String literal is:  wercopypastedeveloper.blogspot.com
>>> a=True
>>> print("Boolean literal is: ",a)
Boolean literal is:  True
>>> b=[1,2,3,4]
>>> print("List literal is: ",b)
List literal is:  [1, 2, 3, 4]
>>> c=(1,2,3,4)
>>> print("Tuple literal is: ",c)
Tuple literal is:  (1, 2, 3, 4)
>>> d={1,2,2,2,3,4}
>>> print(" literal is: ",d)
 literal is:  {1, 2, 3, 4}
>>> e={1:"ONE",2:"TWO"}
>>> print("Dict literal is: ",e)
Dict literal is:  {1: 'ONE', 2: 'TWO'}

1 comment: