Tuesday, November 19, 2019

Basic terms of a Python Programs.


  1. Blocks and Indentation
  2. Statements
  3. Expressions
  4. Comments

1. Blocks and Indentation:

  • Python provides no braces to indicate blocks of code for class and function definition or flow control.
  • Maximum line length should be maximum 79 characters.
  • Blocks of code are denoted by line indentation, which is rigidly enforced.
  • The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.

for example –
if True:
print(“True”)
else:
print(“False”)
2. Statements

  • A line which has the instructions or expressions.

3. Expressions:

  • A legal combination of symbols and values that produce a result. Generally it produces a value.

4. Comments: 

  • Comments are not executed. 
  • Comments explain a program and make a program understandable and readable. 
  • All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.
There are two types of comments in python:

  • Single line comment.
  • Multi-line comment.

Single line comment: 

  • This type of comments start in a line and when a line ends, it is automatically ends. Single line comment starts with # symbol.
  • Example: if a>b: # Relational operator compare two values.

Multi-Line comment: 

  • Multi line comments can be written in more than one lines. 
  • Triple quoted ‘ ’ ’ or “ ” ”) multi-line comments may be used in python. 
  • It is also known as docstring.

Example:
‘’’ This program will calculate the average of 10 values.
First find the sum of 10 values
and divide the sum by number of values
‘’’
Multiple Statements on a Single Line:
  • The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.
  • Example:-

          x=5; print(“Value =” x)

No comments:
Write comments