Python Keywords and Escape Characters

Python Keywords and Escape Characters

Today in this tutorial we will discuss the python keywords and escape characters. These python keywords and escape characters help a programmer to gain output in a desirable form. Some of the python keywords and escape characters we will discuss are none, pass, the new line character etc.  So let’s start!

None:

As the name suggests it exactly means nothing i.e. empty, null, or void. When in a program we need to handle such kind of cases where no values are associated then this data type is used.

Example:

>>>def fun1(): #creating function 1
n1=1
>>def fun2(): print(“None data type”) #creating function 2
>>>fun2() #calling function 2
>>> print(fun1()) #calling function 1

Output:

None data type #output from 1st function
None #output from 2nd function

Here we create two functions fun1 and fun2 and then call them one by one. As we can see that fun1 contains a variable declaration and so when we try to print the output of the function it would absolutely be nothing as it stores no value. Hence, in such a case the output will be ‘None’.


Pass:

The pass keyword is similar to the general meaning of the word ‘pass’ in the real world. When we need to pass the flow of control in the program from one place to another then we use the flow keyword.

Example:

>>>def fun1():
pass
It can also be used in looping statements.

Example:

>>> if a>b:
print(a)
>>>else:
pass

Here, the if block contains some executable statements whereas the else block is empty and the use of ’pass’ keyword passes on the flow of control to the following statement.


Escape Characters:

here are few characters followed by ‘\’known as the escape characters, like:

  • \n – next line
  • \t – horizontal tab
  • \r – ASCII carriage return
  • \b – ASCII backspace
  • \f – ASCII formfeed
  • \’ – print  single quotes
  • \” – prints double quotes
  • \\ – prints backslash
  • \v – vertical tab

Example:

>>>st= “meow \ncat”
>>>print(st)
>>>st= “meow \tcat”
>>>print(st)

Output:

meow
cat
meow      cat

In the example above we see the use of escape characters to change the line first and then have spacing between the words.

Example:

>>>st= ‘Easier \“said\” than done’
>>>print(st)

Output: Easier “said” than done

In the above example we have a string literal named ’st’ in which we want “said” to be inside double quotes. To do this we use the escape character (\“). Also, we put the string between single quotes so that it doesn’t conflict with the double quotes inside.


Constants:

These are the values that once declared cannot be changed later i.e. it remains constant. In Python, there exists no possible way through which we can specify a particular variable to be a constant. However, if we need a particular value to stay constant throughout the code then we declare it in capitals.

Example:

>>>AMOUNT =20 #declaring a constant variable
>>>print(AMOUNT)

Output: 20

With this, we come to the end of this tutorial. The python keywords and escape characters will definitely be used by us in the next tutorials, whereas constants in python are something that you need not be worried about.

Leave a Comment

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