In this session we will talk about basic structure of Python program like:-
- Blocks and indentation
We have already covered fundamentals of python in which we have covered Tokens: Keywords, Identifiers, Operators, Literals, Punctuators so you can also checkout that.
Some of you might be wondering about Tokens/lexical units, So we have already covered that In my previous post you can refer that.
Before we proceed, have a look at the following sample code. Don’t worry if things are not clear to you right now. They’ll become clear when the discussion proceeds.
Now, let’s talk about first component of above sample code i.e, Expressions.
Expressions in Python is any legal combination(which Python evaluates) of symbols that represents a value. Python supports many operators for combining data objects into expressions.
For example:- (a-12), (13>b), 77 all are an example of expressions in Python.
A statement is a programming instruction that Python interpreter can execute and does something i.e., some action takes place.
a=14 (assignment statement)
If a statement executes then it is not necessary that a statement results in a value, it may or may not yield value.
Comments are additional information to clarify the source code, which can be read by programmers but ignored by Python interpreter.
In Python, comments begin with # (Symbol) and generally end with the end of physical lines.
Advantages of Using Comments.
- Comments enhance the overall readability of the program hence your code more understandable.
- t helps us to remember why certain blocks of codes were written.
- At the time of testing of codes, comments are used to ignore some lines of codes from that program.
There are mainly three types of comments:-
- Full line comments.
- Inline comment.
- Multi-line comments.
The examples of full-line comments and inline comments are provided above in sample code you can refer that.
Now let’s talk about multi-line comments,
In Python codes, you can use multiline comments in two ways:-
1. Add # symbol at the beginning of every line.
#Multi line comments #Are useful for detailed #additional information
2. Type comment as a triple quoted multi-line string.
'''Multi line comments are useful for detailed additional information'''
These are also known as docstrings and can be used either with triple-apostrophe (”’) or triple quotes(“””).
A function is a code that has a name and it can be reused (executed again) by specifying its name in the program, where needed.
In the above sample program, there is a function named as table(). The statements that are indented below its def statement are part of the function. The function call statement in above code is table().
Calling a function becomes a statement for e.g. print is a function but when you call print() then it becomes statement.
Blocks and indentation.
Blocks:- A group of statements which are a part of another statement or a function are called block or code-block or suite in Python.
Indentation:- Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suite.
You can have a look at indentation level from above sample code.
Note:- If you unnecessarily indent a statement; Python will raise error for that.
In this session we have discussed all about basics of python programming i.e. Expressions, Statements, Comments, Function, and indentation with an sample code. I hope you have found this article helpful and your all doubts are clear. If you have any question or suggestion then let me know in the comment section trust me your opinions means a lot for us.