Python LiteralsPython Literals can be defined as data that is given in a variable or constant. Python supports the following literals: 1. String literals:String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes to create a string. Example: 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. Example: b) Multi-line String – A piece of text that is written in multiple lines is known as multiple lines string. There are two ways to create multiline strings: 1) Adding black slash at the end of each line. Example: 'hellouser' 2) Using triple quotation marks:- Example: Output: welcome to SSSIT II. Numeric literals:Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.
Example – Numeric Literals Output: 20 100 141 301 100.5 150.0 (5+3.14j) 3.14 5.0 III. Boolean literals:A Boolean literal can have any of the two values: True or False. Example – Boolean Literals Output: x is True y is False z is False a: 11 b: 10 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 the end of lists in Python. Example – Special Literals Output: 10 None V. Literal Collections.Python provides the four types of literal collection such as List literals, Tuple literals, Dict literals, and Set literals. List:
Example – List literals Output: ['John', 678, 20.4, 'Peter'] ['John', 678, 20.4, 'Peter', 456, 'Andrew'] Dictionary:
Example Output: {'name': 'Pater', 'Age': 18, 'Roll_nu': 101} Tuple:
Example Output: (10, 20, 'Dev', [2, 3, 4]) Set:
Example: – Set Literals Output: {'guava', 'apple', 'papaya', 'grapes'} |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263702.html