91-9990449935 0120-4256464 |
Python LiteralsLiterals can be defined as a data that is given in a variable or constant. Python support the following literals: I. String literals: String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes for a String. Eg: "Aman" , '12345' Types of Strings: There are two types of Strings supported in Python: a).Single line String- Strings that are terminated within a single line are known as Single line Strings. Eg: >>> text1='hello' b).Multi line String- A piece of text that is spread along multiple lines is known as Multiple line String. There are two ways to create Multiline Strings: 1). Adding black slash at the end of each line. Eg: >>> text1='hello\ user' >>> text1 'hellouser' >>> 2).Using triple quotation marks:- Eg: >>> str2='''welcome to SSSIT''' >>> print str2 welcome to SSSIT >>> II.Numeric literals: Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.
III. Boolean literals:
A Boolean literal can have any of the two values: True or False. IV. Special literals.
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 end of lists in Python. Eg: >>> val1=10 >>> val2=None >>> val1 10 >>> val2 >>> print val2 None >>> V.Literal Collections. Collections such as tuples, lists and Dictionary are used in Python. List:
Eg: >>> list=['aman',678,20.4,'saurav'] >>> list1=[456,'rahul'] >>> list ['aman', 678, 20.4, 'saurav'] >>> list[1:3] [678, 20.4] >>> list+list1 ['aman', 678, 20.4, 'saurav', 456, 'rahul'] >>> list1*2 [456, 'rahul', 456, 'rahul'] >>>
Next TopicPython Operators
|