Sitemap
thirukolluru.haritha haritha

thirukolluru.haritha haritha

Python Syntax

--

Python syntax refers to the set of rules that define how to write and organize code so that the Python interpreter can understand and execute it correctly. These rules ensure that your code is structured, formatted, and error-free.

Indentation in Python

Indentation in Python is crucial as it defines the blocks of code. Python uses indentation. This improves the readability of Python code but can also lead to indentation errors if not used correctly.

Example: print(“This is true!”)

print(“I am tab indentation”)

print (“I have no indentation”)

Statements :

A computer program is a list of “instructions” to be “executed” by a python engine. In a programming language, these programming instructions are called statements. Any instruction the python engine executes with error or without error it is called as a statement.

Example: print (1+1)

print (“hi”)

print (2–2)

There are 3 types of statements

  1. Single statements in Single Line.

Example: print (“hello world”).

2. Multiple Statement in Single Line.

Example: print (1+1) print (2+2) print(hi)

3.Single Statements in Multiple Line.

Example: 1+2–3+5

(1\

+\

2\

-\

3\

+\

5)

In above example slash (\) is used to break the statement. We can use Slash (\) wherever we want to break the statement. Parenthesis is used at the starting and ending of the statement. At the end of the statement slash cannot be used at the end of the statement. When it is executed, it searches for next statement and compilation error may occur.

Get thirukolluru.haritha haritha’s stories in your inbox

Join Medium for free to get updates from this writer.

4.Comments: Need of comments in programming language is to make the code more “readable”, which helps to debug the error easily. Single line comment can be used multiple times.

In python everything which is written is consider as statement by default, statements are converted into comments by using “#” hash symbol.

Example : print (1+1) #Add

In above example the python executes the ‘#’ and ignores the remaining comments after the “#”. Python doesn’t supports “Multiline comments”.

Variables

Variables are containers for storing data values. To reuse the data, data need to be stored . The place where the data is stored is called variable.

Variable name: The name given to the stored data , to identify the stored data.

Example: variable name= data.

They are certain rules which is called “Naming Convention”.

Naming Convention: Set of rules to create a variable name which is python syntax.

Rule 1: Variable name should always start with an alphabet

(lowercase ,upper case) or “_” underscore.

example: xyzy or _yz

10xy gives us syntax error.

Rule 2: Variable name should not contain any special symbol/character except underscore (_).

Special symbol: ?, #,$,*,@,!.

Example: x_yz is a variable, x@yz is not a variable.

Rule 3. Variable name can contain numbers, but should not be First character.

Example: x10z is a variable, 1xy is not a variable.

Rule 4: Python is a “case-sensitive” language it treats upper case and lower case as different.

Rule 5: Never use keywords as “variable name”. Keywords have reserved meaning and reserved memory in python. So we cannot use keyword.

Example: True=10

In above example we cannot use True as a variable as it is keyword and have reserved meaning in python.

Variable name Syntax:

Example : x=10

In above example 10 is stored in RAM, Python creates space for 10 in RAM and address it with the variable ‘x’.

Whenever we are calling a variable which is not declared or not defined, Python generates an exception called “Name error” which is a logical error.

--

--