Python Literals
Python Literals can be defined as data that is given in a variable or constant.
Python supports the following literals:
- String Literals
- Numeric Literals
- Boolean Literals
- Special Literals
- Literal Collections
- String Literals:-
a) Single-line String- Strings that are terminated within a single-line are known as Single line Strings. ex.text1='hello'
b) Multi-line String - A piece of text that is written in multiple lines is known as multiple lines string. ex. str2='''''welcome to aencyanalytics''' print str2
2. Numeric Literals :-
Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.
Int(signed integers) | Long(long integers) | float(floating point) | Complex(complex) |
---|---|---|---|
Numbers( can be both positive and negative) with no fractional part.eg: 100 | Integers of unlimited size followed by lowercase or uppercase L eg: 87032845L | Real numbers with both integer and fractional part eg: -26.2 | In the form of a+bj where a forms the real part and b forms the imaginary part of the complex number. eg: 3.14j |
3. Boolean Literals :-
A Boolean literal can have any of the two values: True or False.
Example - Boolean Literals
4. 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 the end of lists in Python.
Example - Special Literals
5.Literal Collections
Python provides the four types of literal collection such as List literals, Tuple literals, Dict literals, and Set literals.
List:
- List contains items of different data types. Lists are mutable i.e., modifiable.
- The values stored in List are separated by comma(,) and enclosed within square brackets([]). We can store different types of data in a List.
Dictionary:
- Python dictionary stores the data in the key-value pair.
- It is enclosed by curly-braces {} and each pair is separated by the commas(,).
Tuple:
- Python tuple is a collection of different data-type. It is immutable which means it cannot be modified after creation.
- It is enclosed by the parentheses () and each element is separated by the comma(,).
Set:
- Python set is the collection of the unordered dataset.
- It is enclosed by the {} and each element is separated by the comma(,).
Tags
Python Tutorial