Showing posts with label Python: Basic syntax. Show all posts
Showing posts with label Python: Basic syntax. Show all posts

Python: Basic Syntax

 Here, We will discuss the basic syntax of Python program.

Python provides us two modes to  run a program:

1. Interactive mode and

2. Script mode

1. Interactive mode: In this mode we type single line program and see the output after each line.

To open interactive mode, 

i. Go to the search box besides the start button and type 'idle' or other other python developer like Pycharm (which one in installed in your system). 

ii. Idle shell opens up. Type any single line program on command prompt, like: print "Hello World". 

iii. Press 'enter key', now see the output below the program.


2. Script mode: Script mode is best suitable to write multi lines of codes.

Using the script mode we can write multi lines code in a file which can be executed later. To do this

i. Open any editor like notepad. Type your multi line codes.



ii. Save it with extension .py

iii. Now go to the IDLE (Python) Shell and click file tab--open option.

iv. Select your file from browser. Open it


v. Click here 'Run' tab -- Run Module option.

vi. Now the codes will be executed in Python Shell.


Advantages and Disadvantages of Script Mode: The script mode has few advantages and disadvantages as well. 

Following are the advantages:

i. We can run multiple lines of code.

ii. Debugging is easy in script mode.

iii. We can save our program for later use.

Following are the disadvantages:

i. It may be tedious when we run a single or a few lines of code.

ii. We have to save the code everytime if we make any change in the code.

Syntax of Python: Basic syntax of Python comparises of nothing but proper indentation and necessary commeants about the codes. Proper indentation and comments make us understand the code very clearly. Indentation defines which statement belongs to which block inside a program. Improper use on indentation will end up "indentationError" in our code.
Indentation is the white space before statement. Without indentation Python doesn't know which statement to be executed to next.
For example:

Def fun ():

       Print (“Hello World”)

       If condition:

                statement

       else:

                statement

statement

 Code of block 1 begins

       Code of block1 continue

       Code of block 2 begins

                 Code of block 3 begins

       Code of block 2 continues

                 Code of block 3 continues

Code of block 1 continues

Codes with indentation

What is understood by Python interpreter











The programming language such as C, C++, Java use the curly braces{} to define the code block. But in Python indentation defines the particular group of codes belongs to the particular block.

For example:

list1 = [1, 2, 3, 4, 5, 6, 7, 8]

for i in list1:

    print i

    if i = = 4:

        break

print ("End of the loop")

Here, it is clearly understood that 

in block 1 Python interpreter understands that list 1 has the values 1, 2, 3, 4, 5, 6, 7, 8 and 'for' loop reads these numbers.

in block 2 Python interpreter print the value of 'for' loop, only if condition matches inside the loop, means will print the number till 4.

in block 3 Python interpreter is instructed to take a break.

Now it revers to block 1 (outside from loop) and finally prints "End of the loop"


The output is


Comments in Python: Comments are essential for defining the codes and help us to understand the codes well. By looking the comments, we can easily understand the purpose of each line, we have written in code.

In Python we can apply comment, using the hash(#) character. The Python interpreter ignores the lines followed by hash (#) character. A good programmar should always use the comments to make code understandable.
Let's see the following example with comments:
p=5000                # Principal is rs. 5000.
r=2                      # Rate is 2%
t=3                      # Time is 3 years
i=p*r*t/100         # i calculates the interest
print(i)
(we can add comment in each line or only in selected lines of code section.)
Types of comments:   
  • Single line comment
  • Multi line comments
Single line comment: It starts with # character followed by text or explanation that terminates in one line only.
for ex. p=5000                # Principal is rs. 5000.
Multi line comments: In multi line comments, We have to write each line with # character.
for ex. p=5000                # Principal is rs. 5000.
                                        # This is the previous amount
Python Identifiers:
Python identifiers refer to a name used as a variable name, function, module, class or other objects.
There are few rules to follow while naming the Python variable. These are:
  • A variable name must start with either an english letter or underscore(_).
  • A variable name cannot start with a number.
  • Special characters are not allowed in the variable name.
  • The variable's name is case sensitive.
For example:

num = 10

print(num)

 

_amt = 5000

print(_amt)

 

Ravi_age = 9

print(Ravi_age)

 


Output

10  

 

5000

 

9

 


Exercise:
1. A python file gets saved with the extension ___.
2. ___________ mode can allow you to type multiple lines of code and save it for future referene.
3. The writing rules of program is called?
4. State the difference between the two modes used to work in Python.
5. What are comments in Python? How can we insert single line and multi line comments in Python?
 


Python: Functions