What do you mean by Python literals ?

  • Literals are a notation for representing a fixed value in source code and it can be defined as raw value or data given in variables or constants.

String Literals

  • A string literal can be created by writing a text with a group of characters surrounded by the single (‘), double (“) or triple quotes.
  • We can write multi-line strings or display in the desired way by using triple quotes.

Sample Code

[pastacode lang=”python” manual=”%23%20string%20literals%0A%0A%23%20in%20single%20quote%0A%0As%20%3D%20’Welcome%20to%20Wikitechy’%0A%0A%0A%0A%0A%23%20in%20double%20quotes%0A%0At%20%3D%20%22Welcome%20to%20Wikitechy%22%0A%0A%0A%0A%0A%23%20multi-line%20String%0A%0Am%20%3D%20”’Welcome%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20to%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20Wikitechy”’%0A%0A%0A%0A%0Aprint(s)%0A%0Aprint(t)%0A%0Aprint(m)” message=”” highlight=”” provider=”manual”/]

Output

Numeric Literals

  • They are immutable and it consists of three data types, they are Integer, Float, Complex.
  • In integer both positive and negative numbers including 0 and it should not take any fractional part.
  • Float data type are real numbers having both integer and fractional part.

Sample Code

[pastacode lang=”python” manual=”%23%20integer%20literal%0A%0A%0A%0A%0A%23%20Binary%20Literals%0A%0Aa%20%3D%200b10100%0A%0A%0A%0A%0A%23%20Decimal%20Literal%0A%0Ab%20%3D%2030%0A%0A%0A%0A%0A%23%20Octal%20Literal%0A%0Ac%20%3D%200o320%0A%0A%0A%0A%0A%23%20Hexadecimal%20Literal%0A%0Ad%20%3D%200x12b%0A%0A%0A%0A%0Aprint(a%2C%20b%2C%20c%2C%20d)” message=”” highlight=”” provider=”manual”/]

Output

Boolean Literals

  • In python there are only two Boolean literals they are true and false.

Sample Code

[pastacode lang=”python” manual=”x%20%3D%20(1%20%3D%3D%20True)%0A%0Ay%20%3D%20(2%20%3D%3D%20False)%0A%0Az%20%3D%20(3%20%3D%3D%20True)%0A%0Ar%20%3D%20(1%20%3D%3D%20True)%0A%0Aa%20%3D%20True%20%2B%2010%0A%0Ab%20%3D%20False%20%2B%2010%0A%0A%0A%0A%0Aprint(%22x%20is%22%2C%20x)%0A%0Aprint(%22y%20is%22%2C%20y)%0A%0Aprint(%22z%20is%22%2C%20r)%0A%0Aprint(%22a%3A%22%2C%20a)%0A%0Aprint(%22b%3A%22%2C%20b)%0A%0A%0A” message=”” highlight=”” provider=”manual”/]

Output

Literal Collections

  • In this there are four different types of literal collections, they are Lis literals, Tuple literals, Dict literals, Set literals.
  • In list literals the values are stored separated by comma (,) and enclosed within square brackets ([ ]).
  • Tuple literals are enclosed by the parentheses ‘()‘ and each element is separated by the comma(,) and it is immutable.
  • Dictionary literals is enclosed by curly-braces ‘{}‘ and each pair is separated by the commas(,).
  • Set literals is the collection of the unordered data set and enclosed by the {} and each element is separated by the comma (,).

Sample Code

[pastacode lang=”python” manual=”%23%20Tuple%20literals%0A%0Aeven_number%20%3D%20(2%2C%204%2C%206%2C%208)%0A%0Aodd_number%20%3D%20(1%2C%203%2C%205%2C%207)%0A%0A%0A%0A%0Aprint(even_number)%0A%0Aprint(odd_number)” message=”” highlight=”” provider=”manual”/]

Output

Special Literals

  • Python contains one special literal where ‘None’ is used to define a null variable.
  • If ‘None’ is compared with anything else other than a ‘None’, it will return false.

Sample Code

[pastacode lang=”python” manual=”%23%20Special%20literals%0A%0Awater_remain%20%3D%20None%0A%0Aprint(water_remain)” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like