Home / Computer Science / Python / Basic Syntax in Python

Basic Syntax in Python

Identifiers in Python:

An identifier is a user-defined word for a special purpose. An identifier is a string of alphanumeric characters that begins with an alphabetic character or an underscore character that are used to represent various programming elements such as variables, functions, arrays, structures, unions and so on. 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 (_) followed by zero or more letters, underscores and digits (0 to 9).

 

Python doesn’t allow punctuations as identifiers. Such as @, $, &, *, #, ! and %. Even python is case sensitive HI and hi are both are different identifiers in python.

Naming Conventions:

  1. Identifier names start with lower case letters
  2. Class name start with upper case letters
  3. If any identifier is starting with single underscore (_) that means that identifier is private and if the identifier starts with two underscores (__) that means strongly private identifier. Examples: __name
  4. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name. Example __inti__.

In the next blog we discuss more about underscore and it’s importance.

Indentation in Python:

Many of the programming languages is used curly braces to denotes that the block of code but in python braces are eliminated and follow only indentation. All the python IDE designed with the same pattern otherwise interpreter throws an error. So the IDE must configure with python. Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

In the indentation, the space is varied but maintain the same indentation within the block otherwise we got an error.

 


if True:
print ("HI")
else:
print ("Bye")

Indentation Demo In Python
Indentation Demo In Python

It gives the output as “HI”.
In the same way if I change the indentation it throws an error.

Indentation Demo - Error
Indentation Demo – Error

See in the above picture, it is showing an error, because I change the indentation, so it throws an error.Python uses uniform indentation to denote a block of statements. When a block is to be started, type the exclamation symbol (:) and press Enter. Any Python-aware editor (like IDLE) goes to the next line leaving an additional whitespace (called indent). Subsequent statements in the block follow the same level of indent. In order to signal the end of a block, the whitespace is de-dented by pressing the backspace key. If your editor is not configured for Python, you may have to ensure that the statements in a block have the same indentation level by pressing the spacebar or Tab key. The Python interpreter will throw an error if the indentation level in the block is not same.

Multiple lined statements:

If any programmer has a scenario with line continuation, python provides line continuation character ( \ ). It allows line continuation.

Print “Hi” \

“Welcome to the”\

“Python tutorial”

Python Multi lined statements Demo
Python Multi lined statements Demo

 

See it allows line continuation. Normally when a line is typed, and press enter the shell executes here in this it allows the programmer to continue the line.

Note: If the statement contains brackets like {},(),[] are don’t need line continuation


Fruits = ['Gua', 'Apple', 'Orange',
'Lemon', 'Watermelon']

Continuation In Python
Continuation In Python

See it doesn’t throw any error.

Statements in Python:

In any programming language the line ends with semi colon but in python the line terminator in new line when type a line of statement and hit enter key the command will execute, and it treated as a statement.

Examples:

College= “IGNOU”

Class=12

Name=”Santosh”

Here all are independent statements. In the same way of we want differentiate these multiple statements in a single line of statement use semicolon ;.

College="IGNOU";Class=12;name="Santosh"

Here semicolon indicates the interpreter new line starts. See the below picture.

Semicolon - Python
Semicolon – Python

Comments in Python:

To increase the program readability programmers, use comments. Not only readability for better and effective maintenance purpose comments are used. 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.In a Python script, the symbol # indicates the start of a comment line.

Unlike Java, C, other programming languages python don’t have block commenting, if you want to add comment to a block use IDE shortcuts but in python IDLE use alt +3 for commenting a block.

# this is a comment

print ("Hello World")

print ("Welcome to Python Tutorial") #this is also a comment but after a statement.

Note: Blank lines in python

If a line containing white spaces only that means that was started with # or blank line python is ignore that line. In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.

Suites in Python:

Python has Suites, A suite in python is a group of individual statements which is formed a single code block called suites. Mostly these decision control statement has header line and suites.

These are begins with keyword and followed by an expression and terminate with : (Colon).

if expression :

suite

elif expression :

suite

else :

suite

How python takes input from the user.

Python takes input from the user when the user include the function “input()” in the statement. The input() function is available in core library of standard python distribution. It reads keystrokes as sting object.

Input - Python
Input – Python

The input() function always reads the input as a string, even if comprises of digits. The type() function used earlier confirms this behavior.

 

About santosh G

Hi, i am Santosh Gadagamma, a tutor in Software Engineering and an enthusiast for sharing knowledge in Computer Science and other domains. I developed this site to share knowledge to all the aspirants of technologies like, Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts and all levels of software project design, development, deployment, and maintenance. As a programmer I believe that, “The world now needs computers to function.” Hope, this site guides you as a learning tool towards greater heights. I believe that Education has no end points and i wish to learn more in the process of teaching you….,

Check Also

Python Installation and Environment Setup

Python Installation Checking: As python is most widely used language so before installing python in …