Python_Tutorial #Class - 02 | Command line & Syntax |Learn Python


In last tutorial, we have successfully configured Python on our machines, and executed our first Python program.

The topics for today's chapter are
  1. Python Command Line
  2. Python Syntax
  3. Comments in Python

1. Python Command Line

Python provides a Python Shell ( also known as Python Interactive Shell), used to execute only a single line of code to get output.
To dive into Python shell, open cmd( Command Prompt), type python or Python and hit enter. you should see something like this:



or you can open IDLE.

Try executing the following statements and see the output.

>>>print(" Hello, World!")
>>>10 + 20
>>>32 * 85
Hello, World!
30
2720

2. Python Syntax

Syntax of any programming language and also Python is the set of rules which must be followed and there are some fix symbols and terms which we can use to create proper programs. Every programming language has its own syntax.
To better understand Python syntax let us consider the following code:

Neeraj
You can vote!

The above program may seem complicated for you at this moment of time, but as we go along we'll learn everything. For now, have a look at the code above and try to compare with following syntax rules of Python:

  • Comments are marked by #
Comments are part of a program that is not executed by the compiler. These are used to make the code readable. Comments are made by adding # at the start of any line. Spot comments in the code example above.

# This is a comment

  • Termination of Statements
End of Line marks the termination of a statement.

name = "Neeraj"
age = 21

  • Semicolons in Python
Semicolons( ; ) are the most popular symbol in the programming world, but no more in Python. Semicolons mark the end of a statement in any programming language, but in python, it's Optional.
We can use semicolons but they are no more mandatory.

age = 21; nationality = "Indian";

  • Indentation: Respect her Space
Indentation is the space or tabs used at the start of a statement. Unlike other programming languages like C/C++/Java and many more which uses { } to enclose multiple statements within a block, Python requires Indent.
The number of spaces for an indent is variable but same block must use fixed number of spaces. We'll learn this by practice along the course. A simple example is shown below:

>>># notice indent after :
>>>age = 21
>>>if age > 18 :
>>>    print( "You are eligible to vote. " )
You are eligible to vote.
In Python, indented code blocks are always preceded by a colon : on the previous line.

  • Not every space matters
Space before a statement does matter, but space inside a statement doesn't matter. This can be easily understood by following example.

sum=10+20
sum = 10 + 20
sum             =       10            +               20
sum=10                       +                            20

3. Comments in Python

Comments are the part of code that is ignored/skipped by the compiler and is not executed. Comments are intended to make the code easy to read by anyone. They are the description of the code. We can add any lines of code in any programming language, there is no barrier to that.
In Python to add a comment, we just need to add # to the start of the line, and the compiler will treat that particular line as a comment and would not execute it.

There are only Single-Line comments in Python. But we can add any number of Single-Line Comments.

Hello, World!
152
351

The above example makes the code self-explanatory to the reader. This is what comments are used for.

Drop your queries in the comment box or mail me at [email protected]


Post a Comment

0 Comments