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.
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.
- Single line comment
- Multi line comments
- 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.
num = 10 print(num)
_amt = 5000 print(_amt)
Ravi_age = 9 print(Ravi_age) |
10 5000 9 |