Python Basic Syntax

Python Basic Syntax

1) Modes of Programming in Python
2) Python Identifiers
3) Reserved Words
4) Lines and Indentation
5) Comments in Python
6) Quotation in Python

First Download & Install Python Software, Launch Python Shell, Write & Execute Python Statements using Interactive Mode or script Mode.

Python is a case sensitive programming language, difference is there between upper case and lower characters and Python reserved words are in lowercase characters. But you can use Upper case or Lower case for user defined identifiers.

Class names should start with an uppercase letter. All other identifiers start with a lowercase letter.

1) Modes of Programming in Python

i) Interactive Mode
ii) Script Mode

i) Interactive Mode

Interactive mode provides us with a quick way of running blocks or a single line of Python code.

The code executes via the Python shell, which comes with Python installation. Interactive mode is handy when you just want to execute basic Python commands or you are new to Python programming.

Install Python Software in your computer, launch Python Shell and To run your Python statements(just type them and hit the enter key).

Then you can get the Program output on the Python Shell Console


a) Helpful when your script is extremely short and you want immediate results.

b) Good for beginners who need to understand Python basics.


a) Editing the code in interactive mode is hard as you have to move back to the previous commands or else you have to rewrite the whole command again.

b) It’s very tedious to run long pieces of code.

ii) Script Mode

> If you need to write a long piece of Python code or your Python script spans multiple files, interactive mode is not recommended. Script mode is the way to go in such cases. In script mode, You write your code in a text file then save it with a .py extension which stands for “Python”.

Note that you can use any text editor for this, If you are in the standard Python shell, you can click “File Menu” then choose “New” to open a blank script in which you can write your code, You can save the file after writing a program.

After writing your code, you can run it by clicking “Run” then “Run Module” or simply press F5.

Then you can get the Program output on the Python Shell Console


a) It is easy to run large pieces of code.

b) Editing your script is easier in script mode.

b) Good for both beginners and experts.


a) It can be tedious when you need to run only a single or a few lines of cod.

b) You must create and save a file before executing your code.

Key Differences Between Interactive and Script Mode

> In script mode, a file must be created and saved before executing the code to get results. In interactive mode, the result is returned immediately after pressing the enter key.

> In script mode, you are provided with a direct way of editing your code. This is not possible in interactive mode.

2) Python Identifiers

A Python identifier 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 (_)

3) Reserved Words / Keywords

Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can’t be used as identifiers for other programming elements like name of variable, function etc.

Python Keywords:

and – A logical operator

as – To create an alias

assert – For debugging

break – To break out of a loop

class – To define a class

continue – To continue to the next iteration of a loop

def – To define a function

del – To delete an object

elif – Used in conditional statements, same as else if

else – Used in conditional statements

except – Used with exceptions, what to do when an exception occurs

False – Boolean value, result of comparison operations

finally – Used with exceptions, a block of code that will be executed no matter if there is an exception or not

for – To create a for loop

from – To import specific parts of a module

global – To declare a global variable

if – To make a conditional statement

import – To import a module

in – To check if a value is present in a list, tuple, etc.

is – To test if two variables are equal

lambda – To create an anonymous function

None – Represents a null value

nonlocal – To declare a non-local variable

not – A logical operator

or – A logical operator

pass – A null statement, a statement that will do nothing

raise – To raise an exception

return – To exit a function and return a value

True – Boolean value, result of comparison operations

try – To make a try…except statement

while – To create a while loop

with – Used to simplify exception handling

yield – To end a function, returns a generator

4) Lines and Indentation

a) Lines

A Python program is divided into a number of logical lines and every logical line is terminated by the token NEWLINE.

Split a Logical line in to multiple physical lines using Semicolon (;) and Join multiple physical lines into one logical line using (\)

b) Indentation

Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.

Python uses indentation to indicate a block of code.

Correct Syntax:
if 5 > 2:
print(“Five is greater than two!”)

Incorrect Syntax:
if 5 > 2:
print(“Five is greater than two!”)

5) Comments in Python

A hash 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.

Note: You can type a comment on the same line after a statement.

6) Quotation in Python

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.

python tutorial

Python Tutorial for Beginners

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *