Python Literals and example

aimtocode

Literals Definition

Literals are a sequence of characters that represent constant values.

These constant values occupy memory but do not have any reference like variables.

Constants can be defined as any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.

There are varius different constants as well.

Types of Literals

There are basically four types of literals in python programming.

  • Integer Literals
  • Float-point Literals
  • Character Literals
  • String Literals

1) Integer Literals

An Integer literals are the primary literals used in python programming.

During the declaration of an integer constant, a prefix is used along with the value to specify the base or radix.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. It can be either uppercase or lowercase format and it also can be in any order.

constant can be specified in three ways:

Decimal:-

Decimal(base 10) literals appear as ordinary numbers with no special notation.

Example:

An integer literal for the decimal number 12 is represented in C as 12 in decimal.

Hexadecimal:-

Hexadecimal numbers (base 16) appear with a leading 0x or 0X.

Octal:-

Octal (base 8) numbers appear with a leading in front of the digits.

Example:

an integer literal for the decimal number 12 is represented in C as 014 in octal.

 12     /* decimal */
 014    /* octal */
 0x4c  /* hexadecimal */
 45    /* int */
 20u   /* unsigned int */
 20l   /* long */
 20ul  /* unsigned long */

2) Floating point literals

Floating-point literals represent decimal numbers with fractional parts, such as 3.142. They can be expressed in either standard or scientific notaion, meaning that the number 563.84 also can be expressed as 5.6384e2.

Example:

 
 3.14159    /* Legal */
 314159E-5L /* Legal */
 510E /* incomplete exponent */
 210f /* no decimal or exponent */
 .e55 /* missing integer or fraction */


3) Character literals

Character literals represent a single Unicode character and appear within a pair of single quotation marks.

Example:


 'A' (or) '5' (or) 'p' etc.


4) String Literals

String literals represent multiple characters and apear within a pair of double quotation marks.

String literals are implemented in python by the using double quotes("").

Example:


 "hello, world"

 "hello, \

  world"

 "hello, " "wo" "rld"

aimtocode