Basic Python Syntax
Now that we've covered how to install and get started with Python in our previous modules, let's look into and learn the basic syntax and structure of Python code along with some simple examples
1. Whitespace and indentation
If you've been working with other programming languages such as Java or C/C++, you already know that they use semicolons (;) to separate different code statements.
But with Python, it uses whitespace and indentations to build up the code structure.
Here is a snippet of Python code to give you a rough idea:
>>> # defining the main function to print out something
>>> def main():
>>> i = 3
>>> max = 20
>>> while (i < max):
>>> print(i)
>>> i = i + 1
>>> # calling the main function
>>> main()
The meaning of this code snippet isn't important to you as of yet. Pay attention to the structure of the code instead.
At the end of each line, there aren't any semicolons that terminate the statement. And indentation is used to format the code.
By making use of indentation and whitespaces to organize the code, Python code provides some great advantages:
- To start with, you'll never miss the beginning and ending code of a specific block like in other programming languages such as Java.
- Secondly, the coding structure is very uniform. Basically what that means is, if you have to maintain some other developer's code, that code looks the same as your code.
- And finally, the code is way more readable when compared with other programming languages.
2. Comments
Comments are as quintessential as the actual code since they describe WHY a piece of code was written and what it actually means and does.
The comments are ignored by the Python Interpreter when it executes the code.
In Python, single-line comments begin with a hash (#) symbol which is then followed by the actual comment. For example:
>>> # This is an example of a single line comment in Python
Python supports other kinds of comments as well.
3. Continuation of statements
In Python, a newline character is used for separating the statements. It places each new statement on one different line.
But, a long statement can span multiple lines by making use of the backslash () character.
Refer to the example given below on how to use the backslash () character to continue a statement in the second line:
>>> if (p == True) and (q == False) and \
>>> (r == True):
>>> print("This is continuation of statements")
4. Identifiers
Identifiers are basically names that identify certain variables, functions, modules, classes, and other objects in Python.
The name of an identifier in Python must begin with a letter or an underscore (_). The characters that follow can be alphanumeric or underscores.
Python identifiers are also case-sensitive which means that 'counter' and 'Counter' are two completely different identifiers.
Addedly, you cannot use Python 'keywords' for naming identifiers.
5. Keywords
Some specific words have special meanings in Python. These words are known as keywords.
Given below is a list of the keywords available in Python:
False
class
finally
is
return
None
continue
for
lambda
try
True
def
from
nonlocal
while
and
del
global
not
with
as
elif
if
or
yield
assert
else
import
pass
break
except
in
raise
As Python is constantly growing and evolving, so will this list of keywords. Python provides a special module in order to list keywords known as 'keyword'.
To find the current and most updated keyword list, type the following code:
>>> import keyword
>>> print(keyword.kwlist)
6. String literals
Python makes use of single quotes ('), double quotes ("), triple single quotes (''') as well as triple-double quotes (""") in order to denote a string literal.
String literals need to be surrounded by the same type of quotes. For instance, if you use a single quote at the beginning of a certain string literal, you must use the same single quote to end it as well.
An example to help you understand better:
>>> s = 'This is a simple string'
>>> print(s)
>>> s = "This is a string using double quotes"
>>> print(s)
>>> s = ''' A string can span
>>> multiple lines '''
>>> print(s)
Concluding Thoughts…
The topics covered in this module form the very base of coding with Python. Make sure to go through them and practice them thoroughly so that you can write efficient and high-grade code.
These topics will be explored in-depth in the upcoming modules where you will learn about all of their individual aspects in detail.