We know that python is an object-oriented and server-side scripting language. Java, C, Perl are also similar to Python. The main difference is in syntaxes.
First, we have to type ‘python’ in the terminal (Linux OS). Then we will get some output like Python 3.8.0 (
#1, Nov 11, 2020, 03:39:03). It means Python is already installed in the OS. Then the terminal
$ will change to
>>>. It means Python is currently active. Now here we can write any code to compile on the terminal under the Python Compiler. If we want to print “Hello World!” then we have to write a print (
"Hello World!"). We have to use this bracket (‘
()‘) for python3. If anyone is using python2 then he or she has to write just print “
Hello World!”. Then the result will be Hello World!
>>> print("Hello, World!") Hello, World!
For this script mode, we have to create a file with a file name and extension of
.py. Then we have to open that file on IDE and write the script. We have to save the file before execution. We assume that the file name is code and that contains print (
“Hello Python3!”). So the executable file will be
code.py. To run the code in Linux OS, we have to write
$ python filename.py. Then it will show the result i.e. Hello Python3.
$ python filename.py Hello Python3!
These identifiers are nothing but a function, variable, module, class, and objects. It starts with both upper and lower case alphabets
(a-z) and an underscore
(_) with any numerical digit (
0-9). Python does not support any special characters like
@, $, and %. Python is also a case sensitive scripting language. In Python, all class names start with the upper case and others will be in lower case.
Python supports various keywords and these are constant. These are used in lowercase and also known as reserved words. Those are and,
exce, not, arrest, finally, or, break, for pass, class, form, print, continue, global, raise, def, if, return, del, import, try, elif, in, while, else, is, with, except, lambda, and yield.
For classes and functions, control statements Python does not support any bracket or braces. Code blocks are strictly enforced and signified by a line indentation. It is necessary to validate that all declarations within the chunk must be scooped the same amount.
You will get an error if you skip the indentation as shown below,
#Without Indent if 4 > 2: print (“Four is greater than two”) #Output File "myfile.py", line 2 print("Five is greater than two!") ^ IndentationError: expected an indented block
If you use proper indentation, then code will execute as shown below,
#With Indent i=2 if i==2: print (“Yes it is Two”) else: print (“Yes it is not Two”) #Output #Yes it is Two
To break one line into two on more lines we have to use the continuation character
(/) in python. Sample code is given below,
Result = Value1 + \ Value2+ \ Value3 Print (Result)
Python understands single
(“), and triple
(‘’’) quotes. Single and double are used for string and triple is used for comment.
For a single line comment, we have to use
# before the line. For multiple line comments, we have to use a triple quote before and after the lines. An example is given below,
# This is a comment print("Hello, Guest!") # Below is a multi-line comment """ Sample Content """