Literals Concepts in Python Language

By
Literal is defined as a raw data that is given in a variable or constant.
There are various types of Literals in python as given below:-
  1. Numeric Literals
  2. String Literals
  3. Boolean Literals
  4. Special Literals
  5. Collections Literals
    1.) Numeric Literals:- Numeric Literals are immutable(unchangeable).There are three different numerical types integer, Float and complex present in Numeric Literals.
e.g.
Integer Literals:-
a=0b1111   #binary Literal
b=400      #decimal Literal
c=0o62     #octal Literal
d=0x66     #hexadecimal Literal
print("value of a=",a)
print("value of b=",b)
print("value of c=",c)
print("value of d=",d)
Output of above Python codes:-
value of a= 15
value of b= 400
value of c= 50
value of d= 102
Note:-
  • In above program .I have assigned different literals(binary,decimal,octal and hexadecimal) in different variables(a,b,c,d).
  • When we run the program,all literal values convert into decimal values.
e.g.
Float Literals:-Real numbers with integer and fractional part.
float_a=20.12
float_b=26e2
float_c=12.43
print("value of float_a=",float_a)
print("value of float_b=",float_b)
print("value of float_c=",float_c)
Output of above Python codes:-
value of float_a= 20.12
value of float_b= 2600.0
value of float_c= 12.43
e.g
Complex Number:
c=3+12.25j
print('complex number c=',c)
print('complex number real value=',c.real)
print('complex number imaginary value=',c.imag)
Output of above Python codes:-
complex number c= (3+12.25j)
complex number real value= 3.0
complex number imaginary value= 12.25
     2.) String Literals :- A string Literal can be generated by enclosing the text in the quotes.we can use both quotes(single,double as well as triple) for a string.
e.g.
a='i am a student'               # string literals
b="hello friends !"
c='''How are you'''
d='c'                             #character literal    
e="""welcome                      #multi line string
         To
My website:
https://www.msdotnet.co.in """              
f= r"raw''\nvalues"            #raw string
g=u"\u00dcnic\u00f6de"         #unicode string
print('value of a=',a)
print('value of b=',b)
print('value of c=',c)
print('value of d=',d)
print('value of e=',e)
print('value of f=',f)
print('value of g=',g)
Output of above Python codes:-
value of a= i am a student
value of b= hello friends !
value of c= How are you
value of d= c
value of e= welcome                      #multi line string
         To
My website
https://www.msdotnet.co.in 
value of f= raw''\nvalues
value of g= Ünicöde

     3.) Boolean Literals:-A Boolean literals have only two values:true or false.
a=(True==1)
b=(False==0)
c=(True+10)
d=(False+20)
e=(False==1)
print('value of a=',a)
print('value of b=',b)
print('value of c=',c)
print('value of d=',d)
print('value of e=',e)
Output of above Python codes:-
value of a= True
value of b= True
value of c= 11
value of d= 20
value of e= False
     4.) Special Literals :- Python contains one special literal i.e. None. we use this literal(None) that field is not created.
value_1=400
value_2=200
value_3=None
print('value_1=',value_1)
print('value_2=',value_2)
print('value_3=',value_3)
Output of above Python codes:-
value_1= 400
value_2= 200
value_3= None

     5.) Collection Literals:-There are four different types of collection literal :
  • List
  • Tuple
  • Sets
  • Dictionary
List,Tuple,set,dictionary literals:-List contains items of different data types.list are mutable(changeable).
e.g.
                  0     1        2        3     4      5
list_1=['ram','sita',200,35.45,105,0b101]      # list contains different types of values
   #we can retrieve list value using slice operator([] and [:])
list_2=list_1[0:3]
list_3=list_1[2:5]
list_4=[]
# + sign is used to concatenation(add) two list and * sign is used for repetition .
list_a=['ram','20','12.32']
list_b=['sita','mohan',500]
list_c=(list_a)+(list_b)
list_d=(list_a)*2
list_e=(list_b)*2
tuple=(1,2,3,4,5,6)
dictionary={'a':'apple','b':'bag','c':'cat','d':'dog'}
set={'a','e','i','o','u'}
print(list_1)
print(list_2)
print(list_3)
print(list_4)
print(list_c)
print(list_d)
print(list_d)
print(tuple)
print(dictionary)
print(set)
Output of above Python codes:-
['ram', 'sita', 200, 35.45, 105, 5]
['ram', 'sita', 200]
[200, 35.45, 105]
[]
['ram', '20', '12.32', 'sita', 'mohan', 500]
['ram', '20', '12.32', 'ram', '20', '12.32']
['ram', '20', '12.32', 'ram', '20', '12.32']
(1, 2, 3, 4, 5, 6)
{'a': 'apple', 'b': 'bag', 'c': 'cat', 'd': 'dog'}
{'u', 'a', 'e', 'o', 'i'}

Special Notes:-
Input () built-in function:- This function(or method) reads a line from console input,converts into a string and return it. This function is more important in python to read a input line from console(prompt).
e.g.
a=int(input("Enter the First value:"))
b=int(input("Enter the second value:"))
sum=a+b
multi=a*b
print("your sum of a and b =",sum)
print("your multi of a and b =",multi)

c=input("Enter Your name: ")
print("my name is= ",c)
print("Your best friend's name: ")
d=input()
print("my friend's name="+d)
Output of above Python codes:-

Enter the First value:10
Enter the second value:20
your sum of a and b = 30
your multi of a and b = 200
Enter Your name: RAM
my name is=  RAM
Your best friend's name: 
MOHAN
my friend's name=MOHAN
Watch Complete Lecture Video:-

0 comments:

Post a comment

Powered by Blogger.