Python Literal, Variables and Constants

 

Python Literal, Variables and Constants

What are Literals:

In Python literals are the notations used to represent fixed values in source code. For example, numbers, strings, Boolean values. Following are some examples to represent literals,

10  it is numeric literal

“Hello World”  it is string literal

‘True’ or ‘False’  these are Boolean literals

Following are list of literals used in Python

1.     String Literal:

String literal in Python created by writing single quotation (‘ ‘), double quotation ( “ “) or triple quotation (‘’’  ‘’’). These are sequences of characters.

For example

String in single quote

my_text1= 'Hello world'

String in double quote

my_text2="hello world"

String in triple quote

my_text3='''Hello World'''

By using triple quotes, we can write multiline strings

For example,

my_text4=   '''hello

Students

Good Morning

This is

String Literal'''

print(my_text4)

O/P:

hello

Students

Good Morning

This is

String Literal

 

2.     Numeric Literals:

Numeric literal is used to store numeric values into variables. For example, integer literal 100, floating literal 100.50, complex literal 10.50j etc.

3.     Boolean Literals:

The Boolean literal is used to store True or False values in a variable.

For example, result=” True”, str=’False’ etc.

4.     Special literals:

The special literal includes the None. Whenever the particular value is absent in data set then you can use None.

5.     List Literals:

The list literals are enclosed in square brackets and each element of list is separated by commas. For example, a= [1,2,3,4,5]

6.     Tuple Literals:

These are literals which are enclosed within pair of parenthesis and the values are separated by comas. For example, (1,2,3,4)

7.     Dictionary Literals:

Dictionary literals are enclosed within curly bracket and values are separated by commas.  For example, key.value form is {‘a’:1, ‘b’:2,’c’:3}

8.     Set Literals:

The set literals are enclosed in curly brackets and separated by commas.

For example, {1,2,3,4}

Example of Literals:

 

Str= “Hello World”    It is string Literal

A= 100                      It is Integer literal

B= 10.5                     It is Float literal

List1=[1,2,3,4,5]        It is list literal

Tuple1=(1,2,3,4)        It is tuple literal

Dict={‘a’:1,’b’:2}      Dictionary Literal

Set1={1,2,3,4}          It is set literal

 

Python Variables and Constants:

Variables in Python:

Variables in Python programming is a location on memory which hold data. It is a place which is used to store any type of values and then these values are used to perform the operations.

Rules to declare variables:

1.     Variable cannot start with number. You can use either alphabet or underscore as a first character of variable.

2.     Variable names can include alphanumeric character including underscore character.

3.     These are case sensitive

4.     Variable cannot contain white space

5.     Variables cannot be declared with reserved words

How to declare variables in Python:

 

Variable_name= value

e.g. a=100

     b=10.5

     my_name=”My Name”

my_name is string variable

a is integer variable

b is float variable.

 

Constants in Python:

Constants are the variables whose values cannot be changed once it is initialized to variable. During the execution of program, the values assigned to these variables are not changed.  In Python we do not have any explicit declaration of constant declaration. For explicitly declaration we need to follow some conventions.

One way to use constant variable in your source code is to use by the capital letters.

For example,

PI=3.14

MAX_SIZE=50

We can declare variable as a constant variable for those values which are universally proven and that cannot have changed. 

One another way to create constant variable in Python is by importing constant module in Python file.

For example,

 

import constant

print(“The value of PI:”, constant.PI)

 

Output:

The value of PI: 3.14

टिप्पणी पोस्ट करा

0 टिप्पण्या