Basic Syntax of Python

In this article, you will learn about Python’s basic Syntax.  It is very simple just like the common English language. There is no need to end a Python statement by using the semicolon “;”.

We can use the command line to run Python code.

Python Print Statement

Usually, when we start learning any new programming language we begin with learning to print the “Hello world” on the screen.

Syntax

print("Hello World")

Output

Hello World

Python Variables

Variables are used to store any value. In other programming languages like C, C++, and Java a variable is specified by a data type. But in Python Variables are automatically assigned by a data type based on the value that you are given

Syntax

a=5
print(a)

Output

5

Python Comments

Comments are the pace of code that is never executed. Python comments begin with the “#” symbol. All the codes after that are ignored.

Syntax

# This is a single line comment
print("Code after comment")

Output

Code after comment

Python supports multiline comments that are discussed in the Python Comments section.

Python User Input

Sometimes users want to interact with the program by entering any value. The bellow function is used in Python to take any value from the user.

Input (Prompt) Prompt is the display message on the screen while taking input from the user. It can be anything like “Enter a number” etc.

Syntax

a= input("Enter a number")
print(a)

Output

5

For more information about input and output in Python, you are referred to the Python input/output tutorial.

Python Print Multiple Statements in a Single Line

Below is the code to print multiple statements in a single line.

a=10; b=20; c=20; print (a,b,c)

Output

10 20 20

Python multiple lines

In Python backslash “\” is known as a continuation character that is used to indicate the continuation of a line.  It is used when we break down the long string into multiple lines.

Syntax

message ="You are reading this article on:" \
"www.logictoprogram.com"
print(message)

Output

You are reading this article on:www.logictoprogram.com

Python Indention

Indention or indent basically is the spaces within the code. In other programming languages, you use braces to represent a single block of code. But in Python, we use left indent to specify a block.

Syntax

if 20 > 10:
  print("20 is greater then 10.")

Output

20 is greater then 10.

Scroll to Top