Python Basic Syntax

The syntax of Python is similar to other programming languages such as Java, C and Perl with little differences that we will discuss in this tutorial.

Mode of Programming

Python has two basic modes of programming which means you can write Python code in two modes:

  1. Interactive Mode
  2. Script Mode

Interactive Mode

In Interactive mode, you can write code directly in terminal. To write code in interactive mode, open terminal and enter python <version-numner> as shown below:


python3.7

Next, you will see something like below:


    $ python3.7
    Python 3.7.3 (default, Jul 24 2020, 19:19:03)
    [GCC 7.5.0] on linux
    Type "help", "copyright", "credits" or "license" for
    more information.
    >>>

Now, you can play with the code in interactive mode. Type 5+5 and hit enter, you will get the output as 10. Example:


    >>> 5+5
    10
    >>>

Script Mode

In Script mode, you write code in a script file with .py extension. Let's create a test.py file and write a code to add and print two numbers:

test.py

a = 5 + 5
print(a)
  

Next run the script file in terminal:


python3.7 test.py
Output
10

Python Indentation

Indentation is the number of spaces before the starting of every code line. Indentation in Python is used to indicate a block of code.

Example 1

if 10 < 15:
    print("10 is lesser than 15")
  
Example 2

if 4 > 10:
    print("4 is greater than 10")
if 4 > 2:
    print("4 is greater than 2")
    print("Hello World")
  

The following code are examples will give indentation error:

Error Example 1

if 10 < 15:
print("10 is lesser than 15")
  
Error Example 2

if 4 > 10:
print("4 is greater than 10")
if 4 > 2:
print("4 is greater than 2")
  
Error Example 3

if 4 > 10:
print("4 is greater than 10")
if 4 > 2:
print("4 is greater than 2")
print("Hello World")
  

Comments

Comments are used to ignore statements. Comments are also used for code documentation. In Python, # is used for commenting.

Example

#This is a comment
#print("Hello World 1")
print("Hello World 2")
  
Output
Hello World 2

Variables

A variable is a name that is used to reserve space in memory for storing values.

Example

v = "Hello World"
x = 2 + 5.8
y = x
z = v + " " + str(y)
print("The value of v = ", v)
print("The value of x = ", x)
print("The value of y = ", y)
print("The value of z = ", z)
  
Output
The value of v = Hello World
The value of x = 7.8
The value of y = 7.8
The value of z = Hello World 7.8

Identifier

An identifier is a name that is used to identify an object, variable, class, function, and module. An identifier must always start with a letter A to Z or a to z or an underscore __ followed by letters or numbers 0 to 9.

Python does not allow special characters such as ~, !, @, #, $, %, ^, &, *, (, ), ., {, }, <,>, - within an identifier name.

Valid Identifier Examples

message = "Hello World"
_customer_id = "12345"
_status = True
_socket123 = 99000
_name_ = "Danny"
School_Name = "ABC"
  
Invalid Identifier Examples

message@ = "Hello World"
_customer#_id = "12345"
!_status = True
_socket%123 = 99000
_name_^ = "Danny"
School_&Name* = "ABC"
  

Identifiers Naming Convention in Python

  • Class - Start each word of a class name with a capital letter. Example: MyClass, Student, Customer.
  • Function - Use small letters and in case of multiple words, separate words by underscore. Example: my_function, send_message, record.
  • Variable - Use small letters and in case of multiple words, separate words by underscore to improve readability. Example: x, user_name, email, status.
  • Constants - Use capital letters and in case of multiple words, separate words by underscore. Example: MY_CONSTANT, TARGET_URL, USER_TYPE, ROLE.
  • Module - Use small letters and in case of multiple words, separate words by underscore. Example: module.py, my_module.py.
  • Package - Use small letters to name a package without underscore. Example: mypackage, package.