Python 3.5 - Constants and literals

 

Constants

A constant is a variable whose value can never be changed during the execution of program. We know that, the value of a variable can be changed anytime in a program. But if the variable is a constant and if once we assigned a value to it, then its value can never be changed later in the program. In languages like C, C++ and Java, you may have worked with constants but Python does not have concept of constants. We cannot create constants in Python.

But you can indicate a variable as constant by writing its name in all capital letters so that you will remember that it is a constant and I should not change the value of it. But again you can change its value. This is just so that you remember that it is a constant. You can indicate a variable as constant. For example – MAX_VALUE, PIE etc. can be treated as constants.

age = 28 # a variable

PIE = 3.14 # we indicate the variable as constant but we can change its value later 

You can use a variable as constant when you think the value of this variable is fixed throughout the program and you do not need to change it.

Literals

A literal is a value that is stored into a variable in a program. A literal is also called a constant value. Observe the following statement – 

x = 10

Here, ‘x’ is the variable into which the constant value ‘10’ is stored. Hence, the value 10 is called ‘literal’. Since 10 indicates integer value, it is called ‘integer literal’.

Following are different types of literals –

·         Numeric literal

·         Boolean literal

·         String literal

1.   Numeric literals

The different types of numbers that we use in our program are called numeric literals. The following list shows all numeric literals –

Examples

Literal name

5, -34, 43442

Integer literal

8.5, -56.6, 324.32

Float literal

5+6j, 5.4+3j

Complex literal

0B0101010

Binary literal

0O25437

Octal literal

0x53ACF

Hexadecimal literal


A binary number is a number that has only two values 0 and 1 and it starts with ‘0b’ or ‘0B’ in Python. An octal number contains eight values from 0 to 7 i.e. 0,1,2,3,4,5,6,7 and starts with ‘0O’ or ‘0o’ (zero and oh) in Python. A hexadecimal number has 16 values from 0 to 15. In hexadecimal number system, values 10,11,12,13,14,15 are represented as A, B, C, D, E, F. So a hexadecimal number contains values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F and it starts with ‘0x’ or ‘0X’ in Python. These three number systems are very useful in computer memory. A computer handles only binary data. Hexadecimal numbers can be used to represent memory locations. Octal numbers can be used to store very large binary numbers in memory to save the space.

2.   Boolean literals

Boolean literals have only two values, True and False. We can either store True or False in a variable. For example – 

On = True

Off = False 

In above example, True is a Boolean literal and False is also a Boolean literal. Remember that when we use conditions in our program, then conditions internally evaluate to a Boolean type i.e. True or False. For example, condition 5>3 will evaluate to a bool value i.e. True since 5 is greater than 3.

3.   String literals

We have already discussed strings. So a string literal is a string value stored into a variable. For example – 

Name = “Pradeep” # “Pradeep” is a string literal

Profession = ‘teacher’ # ‘teacher’ is a string literal

Knowing the data type of a variable

Knowing the type of data can be very useful. We can determine the type of a variable or object by using the type() function. Syntax to use type() function is –

type(variable name or any value)

We can pass any object or any value to this function and it will tell the type of that object. For example – 

x = 20

print(type(x)) # knowing the type of x 

Output –

<Class ‘int’>

In above code we asked interpreter to tell the type of variable ‘x’. Since we stored an integer number 20 into variable ‘x’, the type() function returned ‘int’ (type of variable x). We can also directly pass a literal or value to type() function. For example – 

print(type(“india”))

Output –

<Class ‘str’> 

As you can see, the Python told that, the type of “india” is ‘str’ (a string). Remember that everything in Python is an object. Observe the output. It shows Class ‘int’ and Class ‘str’. So in above example, variable x is an object of Class ‘int’ since ‘x’ is an integer variable, literal “india” is an object of Class ‘str’ since it is a string literal. Let’s see type of some more objects – 



Escape sequences

Escape characters are used for special purpose. In general, most of the escape sequences or escape characters are used for those characters which we cannot directly type from a keyboard. An escape character can only be written within single quotes, double quotes, triple single quotes or triple double quotes i.e. within strings.

An escape character always starts with a backslash \ followed by one or more characters. Following table shows mostly used escape sequences –

Escape character

Meaning

\n

New line

\b

Backspace

\v

Vertical space (tab)

\t

Horizontal space (tab)

\’

Display a single quote

\”

Display a double quote

\\

Display a backward slash \

\

New line continuation

   

We cannot directly type a new line, a tab, a backspace etc. from the keyboard in our program. So we can use an escape character for this purpose. For example, to go to a new line we can use ‘\n’ to tell the Python interpreter that it is not a normal character but an escape character so just go to a new line. Do you remember, how do we write strings? We use double, single or triple quotes to write strings. Okay! So if I write a string like “India is my soul” then it is fine. And what if I want to write a string like this – “India is my ”soul” ”. So Python will be confused as to where the string is starting? and where is it ending. We also seem confused. So this string will give an error.

So we can write the second string “soul” by using an escape character like this– “India is my \”soul\” ”. Just look at the table above on how to print a double quote in a string. When Python sees \” and \” then it will understand that they are escape characters and user wants to print double quotes. We can also use the mixture of single and double quotes – ‘India is my ”soul” ‘. The mostly used escape characters are ‘\n’ and ‘\t’. Observe the code below –




As you can see, how much important the escape characters are. So when I want Python to go to new line I can just add ‘\n’ at that point. I can also type ‘\t’ to add a tab space etc.

Comments

Popular posts from this blog

Python 4.4 - Points to remember

Python 3.6 - Points to remember

Python 3.3 - Datatypes in Python