By: Archana Shukla and Rajesh Shukla
Introduction To Python
Python is a powerful, high-level programming language. Python is an interpreted language as its programs are executed by an interpreter. Python IDLE provides two working modes – interactive( Python shell) mode and script mode. Using an interactive mode, the python commands are directly typed at the command prompt(>>>) and as soon as we press enter the interpreter displays the output. To work in the script mode, we have to click on the File menu to open a new file. In this mode, we can save python programs for later use which we cannot do in interactive mode.
Structure of a Python program
A python program constitutes of several elements such as expressions, statements, comments, functions etc.
Tokens in Python
The smallest individual unit in a program is known as a token or a lexical unit. Python has the following tokens:- keywords, identifiers, literals, operators, punctuators.
Keywords
Keywords are predefined words with special meaning to the language compiler or interpreter. These are reserved words and must not be used as normal identifier names. For example, some of the keywords in Python language are for, in, from, break, elif, else, if, import etc.All the keywords except True, False and None are in lowercase and they must be written as it is.
Identifiers(names)
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Naming rules for Python identifiers are :-
1. The name of the variable must always start with either a letter or an underscore (_). For example _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in Python which means num and NUM are two different variables in python.
Literals / values
In programming, a value written exactly as it’s meant to be interpreted. In contrast, a variable is a name that can represent different values during the execution of the program. And a constant is a name that represents the same value throughout a program. But a literal is not a name — it is the value itself. A literal can be a number, a character, or a string. For example, in the expression,
x = 3 , x is a variable, and 3 is a literal.