Python Tokens and Comments

Introduction

 
In this chapter, we will discuss python tokens and Python comments.
 

Python Character Set

 
The following are then character set recognized by python. It uses the traditional ASCII character set.
  1. Letters
    A-Z, a-z
     
  2. Digits
    0-9
     
  3. Special Symbols
    Special Symbols available over Keyboard (some of the symbols like rupee symbol may not be available)
     
  4. White Spaces
    blank space, tab, carriage return, newline, form feed
     
  5. Other Characters
    Unicode

Python Tokens

 
The smallest individual unit in a program is known as a token.
  1. Keywords
    There are in total of 31 reserved keywords. Ex: and, finally, not, etc.
     
  2. Identifiers
    apython identifier is a name used to identify a variable, function,class, module, or other objects. Ex: MyBook, myBook, mybook, etc.
     
  3. Literals
    Literalsin Python can be defined as numbers, text, or other data that representvalues to be stored in variables. Ex: age = 22, escape sequence, etc.
     
  4. Operators
    It can be defined as symbols that are used to perform operations on operands. Ex: arithmetic operators, etc.
     
  5. Punctuators
    Used to implement the grammatical and structure of a Syntax. Ex: &=, >>=, <<=, etc.

Python | Identifiers

 
An identifier, in general, programming terms they can be defined as in the following, 
 
"A name that can be used for identifying a variable, function, class, object, name, and so on in a program, is called an Identifier."
 
This same definition and concept applies also to Python.
 
Here are some basic details one needs to understand about Python identifiers (common),
  • Must start with upper case (A … Z) or lowercase (a … z) letters
  • It can be also started with an underscore "_", followed by more letters
  • It can also be the combination of underscore and numbers
PinPoints
  • Since Python is a case sensitive programming language, "Abhishek" and "abhishek" are different things for it.
  • Python doesn't allow you to use some special characters (@, #, $, %) in your identifier name.
  • Class names start with an uppercase letter followed by the lowercase.

Rules to define Python Identifiers

  1. An identifier starts with a letter A to Z or a to zor an underscore (_) followed by zero or more letters, underscores, anddigits (0 to 9).
  2. Python does not allow special characters
  3. An identifier must not be a keyword of Python.
  4. Python is a case sensitive programming language.
  5. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
  6. Starting an identifier with a single leading underscore indicates that the identifier is private.
  7. Starting an identifier with two leading underscores indicates a strongly private identifier.
  8. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Python | Keywords

 
A keyword, in general, programming terms can be defined as, 
 
"These are reserved words that are already used in language for defining some special tasks or functionalities and you can't use them as a constant or variable name in your code.
 
Let's quickly have a look at those.
 
And Assert Break
class Continue def
del elif else
except exec finally
for from global
if import in
is lambda Not
or pass print
raise return try
while with yeild
 

Python | Punctutors

 
punctuators 
 

Python | Comments

 

Single-Line Comments

 
In Python, a line that starts with a hash "#" is considered to be a comment by the Python interpreter.
 
So write comments and enjoy Python, write as much as you can but in a limit and trust me it will help you too. How? It will make your code more understandable, readable and it also considered as one of the guidelines of programming. So cheers.
 
Have a look at the following snippet.
  1. # So let's Print Something- Comment 1  
  2.    
  3. print “A chunk of text!” # Print- Comment2  

Multiline Comments

 
No worries, if you want a multiline comment in Python. You can easily do that using this structure:
 
“””
 
Your comments go here…
 
“””
 
For Example
  1. “””  
  2.    
  3. This is my multiline comment in Python...  
  4.    
  5. “””  
  6.    
  7. print “A chunk of text!”  

Conclusion

 
In the next chapter, we will learn how we can install python on various operating systems.
Author
Abhishek Jaiswal
90 19.8k 9.5m
Next » Python Operators