Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: Common Python Syntax
Source: https://www.fatskills.com/python/chapter/python-programming-common-python-syntax

Python Programming: Common Python Syntax

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Python Identifiers
An identifier in any programming language is the name given to identify a variable, function, class, module or another object. In Python language, an identifier begins with an alphabetic letter A to Z or a to z or an underscore (_) followed by zero or more alphabetic letters, underscores and digits (0 to 9).
 

Python programming language does not allow special characters such as @, $, /, and % within identifiers.

Python is a case sensitive programming language. Therefore, identifiers such as ‘Python’ and ‘python’ are two different identifiers in Python programming language.

Below are the naming conventions for identifiers in Python:
Class name in Python always begins with an uppercase letter and all other Python identifiers starts with a lowercase letter.
A Python identifier is private when such identifier begins with a single leading underscore.
A Python identifier is strongly private when such identifier begins with two leading underscores.
A Python identifier is a language-defined special name when such identifier ends with two trailing underscores.

Python Reserve Words
Reserve words in any programming language are special commands that compiler or interpreters understands, and these reserve words cannot be used as a constant or variable, or any other identifier names in that programming language.

Python has the following reserve words, and all such keywords contain lowercase letters only.
 

and

def

exec

if

not

return

assert

del

finally

import

or

try

break

elif

for

in

pass

while

class

else

from

is

print

with

continue

except

global

lambda

raise

yield

 

Python Keywords
Lines and Indentations

Any block of code in Python are denoted by line indentation, which is rigidly enforced. Python has no braces to denote blocks of code for class definitions and function definitions or flow control. The number of spaces used in an indentation can be variable but for all statements in a particular block, the number of spaces should always be same.

For example, below, the block is correctly indented and therefore, there is no error.


In the next example, since the last statement in the block is not properly indented, consequently, the block has an error.


Therefore, the conclusion is that in Python programming language, all the continuous lines indented with the same number of spaces would form a block.

Representing a Statement as Multi-Line
Statements in the Python language ends with a new line. If the statement is required to be continued into the next line, then the line continuation character (\) is used in Python language. This line continuation character (\) denotes that the statement line should continue as shown in the below screenshot.

In the below example, we have three variables result1, result2 and result3 and the final output is copied to the variable named result.

Instead of writing the equation statement in a single line (result=result1+result2+result3), here, we have used line continuation character (\) so that, it could be written in three lines but represents a single statement in Python language.


Also, a Python statement which is defined within braces (), {} and [] does not require the line continuation character (\) when written as a multi-line statement. This kind of Python statements are still interpreted as a single statement without the use of the line continuation character (\).


Quotation in Python
The Python language permits the use of single ('), double (") and triple (''' or """) codes to represent a string literal, making sure that the same type of quote begins and ends that string. In the below example, single, double and triple codes are used to represent a string in a word, sentence or paragraph. When we print this variable, they print the string irrespective of single, double and triple codes used for representing string literal in Python language.


Comments in Python
Any comment in the Python language is represented by a hash sign (#) provided it is not used inside a string literal between codes (single, double or triple). All characters after the hash sign (#) and up to the end of the physical line are the part of comment and Python interpreter ignores this statement while interpreting the whole program. In the below example, the interpreter will just print the string present inside the print command and will ignore the parts mentioned after a sign before and after as comments.


Using Blank Lines
A blank line in the Python language is either a line with white spaces or a line with comments (i.e. statement starting with a hash sign (#)). The Python interpreter while interpreting a blank line, ignores it and no machine readable code will be generated. A multiline statement in Python is terminated after entering an empty physical line.

Waiting for the User
Using the Python programming language, we can set up the prompt which can accept a user’s input. The following line of the program will display a prompt, which says “Press any key to exit”, and waits for the user input or action.

 

#! /usr/bin/python

raw_input ("\n\nPress any key to exit.")


Also, in the above statement, we have used "\n\n". This is used to create two new lines before displaying the actual line. Once the key is pressed by the user, the program will end. By doing this, we can keep a window console open until the user has finished his work with an application.

Multiple Statements on a Single Line
The Python language allows to write multiple statements on a single line if they are separated by a semicolon (;) as demonstrated in the example below.

Multiple Statement Groups as Suites and Header Line
In the Python language, a group of individual statements making a single code block are called suites. Whereas the compound or complex statements, such as if, def, while, and class require a suite and a header line.
Header line is the one that begins a statement (with the keyword like if, elif, else, etc.) and ends with a colon (: ) and is followed by one or more lines which makes up the suite as demonstrated in the below example. Here, if strg==’Hello World’: is a header line which is followed by a suite (suite = ‘Found’).


 

Command Line Arguments
On UNIX OS, which has Python interpreter installed, we can take help and see all the lists of the functions. These are the basic ones. The below screenshot demonstrates the help command on the UNIX system and all the functions or short codes used.

$ python -h

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...

Options and arguments (and corresponding environment variables):

-c cmd: program passed in as string (terminates option list)

-d  : debug output from parser (also PYTHONDEBUG=x)

-E  : ignore environment variables (such as PYTHONPATH)

-h  : print this help message and exit

[etc.]