Python Syntax

Python Introduction By CodeHind Image


The instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language such as assignment statements, conditional statements, looping statements, etc. All these help the user to get the required output. For example, n = 50 is an assignment statement.

Multi-Line Statement

Statements in Python can be extended to one or more lines by using parenthesis (), braces {}, square brackets [], semi-colon (;), continuous character slash (\). When the programmer needs to do long calculations and cannot fit his statements in one line, one can use these characters.

Declared using Continuation Character (\):
s = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9

Declared using parentheses () :
n = (1 * 2 * 3 + 7 + 8 + 9)

Declared using square brackets [] :
footballer = ['MESSI',

Declared using braces {} :
x = {1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9} Declared using semicolons(;) :
flag = 2; ropes = 3; pole = 4

Whitespace and Indentation

If you've been working in other programming languages such as Java, C#, or C/C++, you may know that these languages use semicolons (;) to separate statements.
However, Python uses whitespace and indentation to build the code structure. The following shows a snippet of Python code:

# define main function to print out something
def main():
i = 1
max = 10
while (i < max):
i = i + 1
# call function main

It is not important for you to understand the meaning of the code right now. Please focus on the code structure instead.

At the end of each line, you don't see a semicolon (;) to end the statement. and uses code indentation to format the code.

By using indentation and whitespace to organize code, Python code gains the following benefits:

  • First, you will never miss the start or end code of a block like in other programming languages such as Java or C#.

  • Second, the coding style is essentially the same. If you have to maintain another developer's code, that code will look very similar to yours.

  • Third, the code is more readable and clear as compared to other programming languages.

Python Comments

Comments are just as important as code because they describe why a piece of code was written.

When Python executes interpreter code, it ignores comments.

In Python, a single line comment begins with the hash (#) symbol and is followed by the comment.

# This is a single line comment in Python

Python Identifiers

Identifiers are names that identify variables, functions, modules, classes, and other objects in Python.

Name identifiers must be a letter or an underscore (_). The following characters can be alphanumeric or underscore.

Python identifiers are case-sensitive. For example, counter and counter are different identifiers.

Also, you cannot use Python keywords for naming identifiers.

Python Keywords

Some words have special meanings in Python. They are called keywords. The following is a list of keywords 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

Python is a growing and evolving language. So its keywords will keep growing and changing.

Python provides a special module for listing its keywords called keywords.

To find the current keyword list, you can use the following code:

import keyword

String Literals

Python uses single quotes ('), double quotes ("), triple-single quotes (''') and triple-double quotes (""") to represent string literals.

String literals need to be sourced with the same type of quotes. For example, if you use single quotes to start a string literal, you need to use the same single quotes to end it.

The following are some examples of string literals:

s = 'This is a string'
s = "Another string using double quotes"
s = ''' string can span
multiple line '''