Python Basic Syntax

Syntax refers to the set of rules and conventions that determine how code should be structured and written.

Python has a simple and straightforward syntax that makes it easy to read and write code. The syntax of Python is similar to other programming languages such as Java, C and Perl with little differences. In contrast to other programming languages, Python uses new lines to complete statements rather than semicolons or parentheses.

The following are some fundamental aspects of Python's syntax:

Mode of Programming

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

  1. Interactive Mode:
  2. In Interactive mode, you can write code directly in terminal. To write code in interactive mode, open terminal and run python command as shown below:

    python

    Next, you will see something like below:

    C:\Users\hello>python
    Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
    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. For example:

    >>> 5+5
    10
    >>>
    
  3. Script Mode:
  4. In Script mode, you write code in a script file with a .py extension. Let's create a main.py file and write code to add and print two numbers:

    a = 5
    print(a)
    

    Next, run the script file (main.py) in terminal using the command as follows:

    python main.py
    

    The output of the above is as follows:

    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 code will result in an indentation error:

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

The correct way to write the above code using proper indentation is as follows:

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

Here is another example of code that will result in an indentation error:

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

The correct way to write the above code using proper indentation is as follows:

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.

Here's how comments are used in Python:

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

The output of the above code is as follows:

Hello World 2

Variables

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

Here's how variables are created and assigned values in Python:

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)

The output of the above code is as follows:

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.

Here are examples of valid identifiers in Python:

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

Examples of invalid identifiers in Python:

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.