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.

In contrast to other programming languages, Python uses new lines to complete statements rather than semicolons or parentheses.

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 run python <version-number> command 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 using the following command:


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. The scope of loops, functions, and classes in Python is defined by indentation and whitespace. Curly brackets are commonly used in other programming languages for this.

The following example will give indentation error:


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

The correct way of writing using indentation in the above code is:


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

Another example code that will give indentation error:


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

Correct way of writing the above code:


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

Comments

Comments are used to ignore statements or code. 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:

[email protected] = "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.