Here you will learn the basic syntax of Python 3.x
Like natural languages, a computer programming language consists of a set of predefined words called keywords. A prescribed rule of usage for each keyword is called the syntax.
There are 33 keywords defined in the Python 3.x interpreter. Since they are accompanied by a predefined meaning, they cannot be used for any other purpose. The list of Python keywords can be retrieved with the following help command in the Python shell.
help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more help.
False | class | from | or |
None | continue | global | pass |
True | def | if | raise |
and | del | import | return |
as | elif | in | try |
assert | else | is | while |
async | except | lambda | with |
await | finally | nonlocal | yield |
break | for | not |
With the exception of the first three (False, None and True), the other keywords are all lower-case.
Python Identifiers
In addition to keywords, a Python program can have variables, functions, classes, modules, packages, etc. Identifier is the name given to these programming elements. An identifier must begin with a letter of the alphabet (lower or upper case) or an underscore (_). Then, more than one letter of the alphabet (a-z or A-Z), digits (0-9), or underscores can be used to form an identifier. Other characters are not allowed.
* Conventionally, the class name begins with an uppercase letter of the alphabet. Others begin with lowercase letters of the alphabet.
* The use of one or two underscores has a special meaning when naming the instance attributes of a class. More information about this will follow in the discussion about inheritance.
* Two underlinings, the first and second, are used in the language itself for a particular purpose. For example (e.g. add, init).
Python Declaration:
By default, a Python translator treats a piece of text ending with a hard ambulance (new line sign) as a statement. This means that each line in a Python script is a declaration. (As in C/C++/C#, a semicolon ; indicates the end of the declaration).
text="Hello World"
number=457
name="Mike"
However, you can display the extended text on more than one line as a single statement, using a backslash () as a continuation character. Take a look at the following examples.
text="However, you can display \
the extended text on more than\
one line as a single statement,\
using a backslash..."
Similarly, use a semicolon; to write several statements on one line.
text="Hello World"; number=457; name="Mike"
Indents in Python
It is often necessary to build a block from more than one statement. For example, there are often several declarations that are part of a function definition. There may be one or more declarations in a loop construction.
Different programming languages use different techniques to define the scope and length of a block of declarations in constructions such as class, function, condition, and loop. In C, C++, C#, or Java, declarations within square brackets { and } are treated as a block.
Python uses a uniform indentation to denote a block of declarations. To start a block, type the exclamation mark (:) and press Enter. Any Python-enabled editor (such as IDLE) moves to the next line, leaving an extra space (called an indentation). Subsequent instructions in the block follow the same level of indentation. To mark the end of a block, space is removed by pressing the backspace key. If your editor is not set up for Python, you may need to make sure that the statements in a block have the same indentation level by pressing the space bar or the Tab key. The Python interpreter issues an error if the indentation level in the block is not the same.
The following example illustrates the use of indentation in the python shell:
As you can see, in the Python shell, the block of the WelCome() function is started after: and the Enter key is pressed. Then it was shown… to mark the block. Now use Tab to indent, and then write a statement. To finish the block, press the Enter key twice.
The same function can be written to an IDLE or any other GUI IDE.
Comments on Python
A shortcut character (#) that is not inside the character string starts a comment. All characters after the # and through the physical line are part of the comment, and the Python interpreter ignores them.
#!/usr/bin/python
#First comment..
print("Hello First comment.") #second comment
This produces the below result.
Hello First comment.
You can enter a comment in the same line after a statement or expression.
fname = "Mike" # This is again comment
You can comment multiple lines as below:
# This is a 1st comment.
# This is a 2nd comment.
# This is a 3rd comment.
# 4th python comment.
The below triple quoted string is also ignored by the Python interpreter and can be used as a multiline comment:
'''
This is a multiline
comment..
Comments..
'''
Python coding style:
– Uses 4 spaces per indentation and no tabs.
– Do not mix tabs and spaces. The tabs create confusion and it is recommended to only use spaces.
– Maximum line length: 79 characters to help users with a small screen.
– Use blank lines to separate top-level function and class definitions, and a single blank line to separate method definitions within a class and larger code blocks within functions.
– Add inline comments if possible (these should be full sentences).
– Use spaces around expressions and statements.