Python Basic Syntax With Example

In this section, we will discuss Python basic syntax with an example, What are rules and regulations need to follow in Python programming. Let’s see the below details to get knowledge about Python Basic syntax.

Indentation in Python

Python does not support 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. Indentation refers to the spaces at the beginning of a code line.

The number of spaces in the indentation is variable but all statements within the block must be indented the same amount. The indentation in Python is very important but other languages follow the indention for better readability.

Let’s see the below example.

if True:
    print("True")
else:
    print("False")

Output-

True

If skip the indentation then line gives the error by python

if True:
print("True")
else:
print("False")

Output-

  File "<ipython-input-3-1c0550ff8db5>", line 2
    print("True")
    ^
IndentationError: expected an indented block
Python basic

Please read the related topics Python Introduction and Python Get Started

Quotation in Python

Python accepts single(‘), double(“) and triple code (”’ or “””) quotes to depend on a string literal 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.

word = 'word'
sentence = "This is a sentence"
paragraph = """This a paragraph
        support multi line"""
print(word)
print(sentence)
print(paragraph)

Output-

word
This is a sentence
This a paragraph
        support multi line

Comments in Python

A hash sign or # is used to make comment line python. # sign must be beginning of the line otherwise it will throw an error. Comments start with a #, and Python will ignore them.

Comment can provide idea python code and the code more readable. In the future, we can use these comments for better understanding.

#First Comment in Python

There will be no output come because it’s a comment line.

First # Comment in Python

The above line of code will throw the below error.
Output-

Python basic

Multiline Comment in Python

Python does not really have a syntax for multi-line comments but Triple quoted string is also ignored by the python interpreter and can be used as a multiline comment. If you did not assign the triple quoted string to any variable then Python will read the code, but then ignore it, and you have made a multiline comment.

"""
This multi line
comments
"""
print("Hello World")

Output-

Hello World

Using Blank Line

A line containing only whitespace, possibly with a comment, is known as a blank line and python totally ignores it.


#The above line is complete and Python will ignore this line at execution time.
print("Hello World")

Output-

Hello World

Multiple Line Statements

Python will not allow writing multiple lines for a single statement but we can write with the help of backslash \ or parentheses ().

sum = 10 + 23 + 9 \
 + 53

print(sum)

Output-

95
sum = (10 + 23 + 9 
 + 53)

print(sum)

Output-

95

Multiple statements on a single line

Python is also capable to write multiple statements on a single line. The semicolon(;) allows multiple statements on the single line given that neither statement start a new code block.

print("Hello Lipsa"); print("Welcome to InfoSyntax");

Output-

Hello Lipsa
Welcome to InfoSyntax

Python Variables

In Python, variables are created when you assign a value to it. Python has no command for declaring a variable.

# variables are created when you assign a value to it. Python has no command for declaring a variable.
a=10
name="Lipsa"
print(a)
print(name)

Output-

10
Lipsa

Python User Input

Python User Input means users can enter the data into our application through the python program. The built-in input function requests and obtains user inputs.

name = input("What is your name ?")
Print(name)

Output-

What is your name ?Lipsa
My name is  Lipsa

Python Basic Syntax Warm UP

This session we have learned the basic syntax of Python such as

Indentation in Python
Python does not support braces to indicate blocks of code for class and function definitions or flow control.
The number of spaces in the indentation is variable but all statements within the block must be indented the same amount.

Quotation
Python accepts single(‘), double(“) and triple code (”’ or “”””) quotes to depend on string literal as long as the same type of quote starts and ends the string.

Comments
Hash # sign must be beginning of the line otherwise it will throw an error. Comments start with a #, and Python will ignore them.

Multiline Comment
Python does not really have a syntax for multiline comments but Triple quoted string is also ignored by the python interpreter and can be used as a multiline comment.

Using Blank Line
A line containing only whitespace, possibly with a comment, is known as a blank line and python totally ignores it.

Multiple statements on a single line
The semicolon(;) allows multiple statements on the single line given that neither statement start a new code block.

Variables
Variables are created when you assign a value to it.

All the above details are considered under Python Basic Syntax

Please download the sample program of Python Basic Syntax Click Here.

Leave a Comment

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