MediumPython Syntax. Syntax refers to a set of rules that… | by Greeshmagunda | Nov, 2026 | Medium
Sitemap

Python Syntax

3 min read1 day ago

--

Press enter or click to view image in full size

Syntax refers to a set of rules that need to be followed while writing the Python code. An error that occurs when Python programmer does not follow the Python rules is called syntax error.

print(1+1) — no error

print(1+2 — syntax error

Statements in Python

Statement: Statement is an instruction that Python engine can execute. The statement goes through the parts of the Python engine ( syntax checker, compiler, and interpreter) and performs the particular task, and displays it on the screen. The executed instruction is called statement.

Writing of Statement

There are types of writing of statements

1.Single Statement in Single line

Single statement in single line and multiple statements in multiple lines gives the same meaning. Writing a single instruction that has to be executed in a single line is a single statement in single line.

ex: print (2+2)

In multiple lines

ex:

  • print(2+3)
  • print(4+2)
  • print(6+2)

2. Multiple statements in single line

Writing Multiple statelents in a single line. To write multiple statements in single line use “;” after every statement.

ex: print(1+1); print(2+2); print(3+3)

3.Single statement in multiple lines

Writing a single statement in multiple lines. To write a single statement in multiple lines symbol \ backslash or ( ) parenthesis is used. To avoid using \ backslash n number of times use ( )

parenthesis.

ex: using \ backslash

1+\

2+2–5\

+2\

-4

If we add \ backslash after the last value, it shows an error (incomplete input error).

ex: by using ( ) parenthesis

(1+2

Get Greeshmagunda’s stories in your inbox

Join Medium for free to get updates from this writer.

+3

+5–6

+10)

Comments

Comments are also instructions that are not executed by the python engine. Comments are used to understand the code easily, debug the code easily.

By using the symbol hash # before an instruction, it is considered as a comment.

  • ex: #2+3+4

Keywords

Keywords are the reserved words in the Python engine.

Press enter or click to view image in full size
Keywords in Python

It has special meaning in the Python engine. There are 35 keywords in Python.

Variable

Variable is a temporary name given to a memory location. We store the Python data in memory. To reuse the data again and again, we store data in the memory. By using assignment operator “=” the data is stored.

Press enter or click to view image in full size

syntax — variable name = dataex: x=10

There are some that have to be followed while creating the variable names called Naming Convention.

Naming convention

Naming convention is a set of Python rules that have to be followed by the programmer to create the variable names.

a. Variable name should always start with an alphabet or underscore(_)

xyz=30

_xy=30

b. Variable name shouldn’t contain any special symbols except underscore.

x_yz=20

x@yz=20 — shows error

c. Variable name can contain numerics , but not as the first character.

var1=30

d. Python is a case-sensitive language.

  • abc=20
  • Abc=20

both are 2 different variables

e. Never use keywords as variable names

True=10 — error

--

--