A Python Identifiers 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).
Python doesnot allow punctuation characters such as &commat, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.
Here are naming conventions for Python identifiers -
- Class names with an upercase letter. All other identifiers start with the lowercase letter.
- Starting an identifier with a single leading underscore indicates that the identifier is private.
- Statrting an identifier with two leading underscores indicates a strongly private identifier.
- If the identifier also ends with two trailing underscores, the identifier is a language - defined special name.
The following list shows the Python keywords. These are reserved words and you cannot use them as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.
1. and 11. exec 21. not
2. assert 12. finally 22. or
3. break 13. for 23. pass
4. class 14. from 24. print
5. continue 15. global 25. raise
6. def 16. if 26. return
7. del 17. import 27. try
8. elif 18. in 28. while
9. else 19. is 29. with
10. except 20. lambda 30. yield
Python provides no braces to indicate blocks of code for class and function definitions or flow control. Block of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be indented in the same amount. For example -
if 10 > 5:
print("10 is greater than 5")
else:
print("10 is not greater than 5")
However, the following block generates an error -
if 10 > 5:
print("10 is greater than 5")
else:
print("10 is not greater than 5")
^
IndentationError: expected an indented block
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character () to denote that the line should continue. For example -
total = 5 +
10 +
30
print(total)
Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example -
months = ['Jan', 'Feb', 'March',
'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept',
'Oct', 'Nov', 'Dec']
print(months)
Python accepts single ('), double (") and triple (" or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following are legal -
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
A high sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.
# First comment
print("Hello, Python!") # second comment
This produces like following result -
Hello, Python!
You can type a comment on the same line after a statement or expression -
name = "Anuj Bisht" # This is a comment
You can comment multiple lines as follows -
# This is a comment.
# This is a comment, too.
Following triple-quoted string is also ignored by Python interpreter and can be used as a mutiline comments:
'''
This is a multiline
comment.
'''
A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it. In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.
The following lone of the program displays the prompt, the statement saying, "Press the enter key to exit", and waits for the user to take the action -
input("nnPress the enter key to exit.")
Here, "nn" is used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.
The semicolon (;) allows multiple statements on the single line given that neither statement starts a new code block. Here is a new code block. Here is a sample snip using the semicolon -
import sys; x = 'foo'; sys.stdout.write(x + 'n')
A group of individual statements, which, make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon (:) and are followed by one or more lines which make up the suite. For example -
if expression:
suite
elif expression:
suite
else:
suite
Many programs can be run to provide you with some basic information about how they should be run. Python enables you to do this with-h -
>python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b : issue warnings and str(bytes_instance), str(bytearray_instance)
and comparing bytes/bytearray with str. (-bb: issue errors)
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser; also PYTHONDEBUG=x
-E : ignore PYTHON* environment variables (such as PYTHONPATH)
-h : print this help message and exit (also --help)
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O : remove assert and __debug__-dependent statements; add .opt-1 before
.pyc extension; also PYTHONOPTIMIZE=x
[ etc. ]