Python Syntax
Python is also a Programming Language if we want to learn a Python language, first we need to learn all the rules provided by the python those rules are called Python Syntax.
→ Python syntax refers to a set of rules we must follow while writing Python code.
→ Basic Python syntax:
Print() is a built-in function used to display the output.
Hello, World! is a string.
() Parentheses are used to pass the value to the print function.
→ Statement: Any instruction that a python engine can execute during execution it can give with an error (or) without an error if it able to execute that instructions is called a statement.
Example: Without an error
Example: With an error
→ Whenever we are going to write a statement we need to follow three rules:
First Rule
- Single Statement in Single line :- Single Statement in single line: If we write only one statement in a single line it is called single statement in a single line.
→ Don’t write multiple statements in a single line. If we write multiple statements in a single line, then we get an error; the error is called a syntax error.
→ Syntax Error: A syntax error is also called a compile-time error, which means that whenever we don’t follow Python rules, it gives a syntax error.
→ Here, I have written two statements in a single line; if I click the run button, the entire code is given to the Python engine. python engine will gives to the syntax checker and syntax checker stops and says to follow first rule.
→ Always preferred to write in a single statement on a single line.
→ We can correct this error by writing the statement on a new line
2nd Rule
2. Multiple Statements in Single Line:- If we write more than one statement in a single line, it is called as Multiple Statement in Single line.
→ If we want to write multiple statements in a single line, use a symbol called a semicolon ( ; ). In between the statements, use a semicolon.
→ Dont use this rule because it can reduce code readability.
3rd Rule
Single Statement in Multiple Lines:- If we want to write a single statement in multiple lines, it is called a single statement in multiple lines.
→ To follow the 3rd rule use a symbol backslash \ (or) ( ) — Parentheses. Wherever you want to break a line and continue to the next line.
Using backslash \ :-
Example:- 2+2–5+6
Using Parentheses ( ) :-
→ But never use backslash at the end because if we use this python thinks that this line also has something is present.
→ If we use a backslash at the end, we get a syntax error called Incomplete Input Error.
4th Rule
Comments: Comments can be used to explain Python code. Comments start with #, and Python will ignore them.
Use of comments : Comments make the code more readable, which will help us to debug the error very easily.
Statement
Example Of Statement :- This is a statement because python excutes the code.
Example Of Comment:-
→ This is a comment because python ignores it during the execution.
Statement and Comment in Single line:-
→ Before the hash, Python thinks the code as statement.
→ After the hash symbol, Python thinks the code as a comment.
→ Comments are mainly used to explain the code within the program so that the programmer can easily understand.
Types of Comments in Python. There are Two Types:-
1. Single Line Comment
2. Multi-Line Comment
- Single Line Comment :- A comment is written in single line using the hash symbol # is called as single line comment.
5th Rule
Keywords:- Keywords are reserved words which have a special meaning in the Python engine; these are called as keywords. There are 35 keywords.
RAM: RAM is a temporary storage unit where we can store data. It is the hardware present in a machine where we can store data.
→ Data is created and stored in RAM.
→ It stores data temporarily.
→ When we turn off the computer, the data stored in the RAM will be lost.
→ RAM is memory that contains small cells called as memory cells; inside the cell, we store the data.
→ Every memory cell will have its own address
Example : x = 20
→ When we click the Run button, the Python engine creates 20 and stores it in RAM.
Example : var1 = 20
→ When we click the Run button, Python first checks whether the data 20 is there or not. If the data is not there it will create the data and store in that data randomly inside the memory.
→ But the problem is that memory has huge addresses that we can’t access, so we give the address a temporary name.
→ So Python creates a variable name var1 = 20. var1 → goes to check whether any memory has a temporary name called var1.
→ If not there it create a variable called var1 with the help of assignment operator. This var1 will be assigned to this particular location. This is how a variable works.
→If we going to write var2 = 20 → Run → Python creates 20.
→ It creates another variable name called var2, which is a temporary name for any memory location, so it will create a var2, and var2 will be assigned again to the same location.
→ A single location can have n number of temporary name.
→ var1 and var2 are two temporary names pointing to the same memory location
. var3 = 100
Python creates 100 and store 100 randomly in RAM. It creates a variable name called var3. It checks whether any memory has a temp name called var3. If it’s not there, it creates a var3 and assigns 100.
var4 = 100
var1 = 10
Example : var1 = 10
var2 = 20
var3 = 20
var4 = 10
var1 = 20
var4 = 200
var5 = 30
var6 = 200
6th Rule
Variable: In Python, a variable is created when you assign a value to it. We create a variable by assigning a value to it using the = operator. A variable is a Temporary name given to a memory location.
→ Because in any programming language we can’t access the memory at the actual address. So, we require a technique called a variable.
→ Without a variable, we can create and store data, but we can’t access it easily.
Variable -> name
Variable name → name
= → Assignment Operator
Value → ‘Bindu’
Variable Name: A variable is the temporary name given to a memory location. In Python, variables are used to store Python data. In Python, variables are created when you assign a value to them.
→ Variable names usually use snake case.
→ In any Programming language, we can’t access memory by its actual address. So, we require a technique called a variable.
→ Without a variable, we can store data but can’t access.
Example: first_name
Python uses = to assign a value to the variable.
→ Naming Convention: Naming Convention is a set of Python rules that a Python program has to follow whenever they want to create a variable name.
→ If we don’t follow this rule, we get a syntax error.
Get Himabinduvelpula’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax:-
- my_first = ‘Hello world!’ — str
- _Second_Variable = 42 — int
- Valid_variable_name_3 = 3.14
When to use a variable?
Whenever we want to reuse data repeatedly, we use a variable.
Naming Rules :
- A variable name must start with a letter, a number, or an underscore. Can contain alphabets
Example:-
2. Should not contain special characters or spaces.
3. Should not start with numbers.
4. Should not be a reserved keyword in Python.
5. Variable names are case-sensitive; age,Age and AGE are three different variables.
Function: A function is a block of reusable code that performs a specific task whenever we call it.
- id( ): This is a built-in function using which we can get the actual memory address.
→ ( ) — inside parentheses, we can provide a variable name or data value.
#Case 1:
x = 20
y = 20
→ x and y are two different variable names referring to the same integer 20.
→ x and y are pointing to the same memory location.
→ x and y have the same id
# Case 2:
x = 20
x = 30
# Case 3:
x1 = 20
x2 = 30
x3 = 40
x2 = 10
x1 = 30
x5 = 60
x6 = 70
x3 = 60
Datatypes: Datatypes are the different types of data that we can create and use in Python.
→ In python we will be having different types of data where every type will be having their own property/character. This data belongs to a particular type then the type is called data type.
→ Python data types are categorised into different groups
(Non-sequential) It is not a data type. It is a group name where, inside the numeric group, Python has three types of data.
1.Numeric:-
→ Integer ( int )
→ Float ( float )
→ Complex (complex)
2. Boolean (Boolean)
→ Integer: An integer is a Python data type that we can create any real values from -∞ to +∞
→ Float: An integer is a Python data type that we can create numbers with decimal points from -∞ to +∞
→ When creating a float data type, Python is precise up to 16 decimal places. After 16 decimal places, if we create larger float values, Python will do something called approximation. It rounds to the nearest value.
→ Complex: Complex is a Python data type by using which we can create a data called complex. A complex number has two parts:
→ Real Part: an int/float
→ Imaginary
→ Because of the imaginary part we call it as complex.
Rule 6 :
- Whenever you create an imaginary part, it should be created with j or J; otherwise, we get a NameError.
→ If, in front of this j or J, we do not have a number (int or float), if it is not there we will not be called a complex.
These three belong to a group called the Numeric group.
2Sequential Group :
- Text
→ String
2. set
→ set
→ Frozenset
3. Dictionary
→ Dict
4. list
5. Tuple
6. range
Interning:- Interning is a technique used by the Python engine by which the Python engine automatically allocates small memory in RAM, and this small memory is called cache.
→ Not every Python data type supports interning.
→ Some Python data types support interning.
→ Integer Interning:- Whenever we working with integer data internally python automatically allocates memory inside this memory python will store values -5 to -256.
→ Whenever I use an integer, for example, x = 10, Python may reuse an already existing integer.
→ At that time, the integer in RAM is going to occupy a small memory, and inside the memory, it is going to store the value -5 to -256.
→ This is done automatically done by Python. Whenever we working with integer this concept is called as integer interning.
Example:-
x = 20
y = 20
id(x)
id(y)
→ Both will have the same ID because Python reuses the integer.
supports interning
a = 267
b =267
id(a)
id(b)
x1 = -4
x2 = -4
id(x1)
id(x2)
x1 = -5
x2 = -5
id(x1)
id(x2)
2. Floats don't support interning
Example: x1 = 0.0
x2 = 0.0
→ Whenever we create x1 = 0.0, it goes into RAM and checks if 0.0 is there. If not creates new memory, stores 0.0, and creates a variable called x1, and tells x1 your reference to this location.
→ Again, it sees 0.0, but because it knows that 0.0 belongs to float. float doesn't support interning; it will automatically create new memory again
3. Complex: It is impossible to support interning.






