Thursday, 17 May 2018

Chaudhari Karan

Python basic syntax

    2 comments:


Python Basic Syntax

Hello friends, In this lecture, we will discuss python basic syntax.

The Python language is very similarities to C, Perl, and Java. But some definite differences between the languages.

Programming  mode:


There are two types of programming mode in python.
  • ·         Interactive programming mode
  • ·         Script programming mode

Interactive programming:

In interactive programming mode invoking the interpreter without passing parameter script file and following python prompt will brings up.

Python basic syntax

Type following text at python prompt and press the Enter. Following result shows.
But if you using older version python like python 2.4 something, so then use print keyword then show the result.

Python basic syntax

Script programming:

In script programming mode invoking the interpreter with parameter script file. Python has .py extension.
First, create the new file and save the file with  .py extension. Then run the program. It would be run in the python shell.

Python Identifier:

Python identifier is the name. For identify any class, method, function, variable, module or objects python identifier used.
The identifier is starting from A to Z, or a to z, underscore and digits o to 9.
Punctuation character like @,$, and % does not allow in python within the identifier.
Python language is case sensitive programming language. So if pass identifier like Car and car are two different identifiers.

Naming conventional for python identifier:

  • ·         The class name always starts with an uppercase letter. And all other identifiers start with lowercase letter.
  • ·         Identifier starting with the single underscore it is indicated identifier is private.
  • ·         Identifier starting with the double underscore it is indicated identifier is strongly private.
  • ·         The identifier also ending with two trailing underscore.

Reserved Words:

Here is the list of the keywords in python. These keywords are reserved words. Keywords cannot use as constant, variable, and any other identifier name. All words are in the lowercase letter.
and
assert
break
class
continue
def
del
elif
else
except
Exec
finally
for
from
global
If
import
in
Is
lambda
Not
or
pass
print
raise
Return
try
while
with
yield

Lines and Indentation:

Python does not provide braces to indicate blocks of code for the class and define the function. So in python block are denoted by lines indentation, it is rigidly enforced.
Example:

Python basic syntax

 Multi-line Statement:

In python statement end with the new line. Python allows the use of line continuation character(\).
Example :
Python basic syntax

Multi-line can contain with [] or {}.
Example:
Python basic syntax

Comments in Python:

In python, comments are denoted by hash sign #.
Example :
Python basic syntax


You can comment on the same line after the statement.
Example:
Python basic syntax


You can comment multi-line comment as shown in the example.
Example:
Python basic syntax

Quotation in python:

Python support single(‘ '), double("  " ), or triple(‘''  ‘'')("""  """) quotes to denote String. Quotes start and end with same quote.
Example:
Python basic syntax

 Blank lines:

Lines contain whitespace, possible with the comment. It is known as blank lines. Python is ignoring the blank lines.

Multiple statements on a Single Line:

In single line for allowing multiple statements use the semicolon ;
Example:
Python basic syntax

Multiple statements Groups as suites:

A group of individual statements, which make a single code block is called suites in Python. The compound or complex statements, such as if, while, def, and class requires a header line and a suite.
Header lines start the statement with the keyword and terminate with a colon: and are followed by one or more lines which make up the suite.
 Example:
Python basic syntax


Next: In the next lecture we will discuss python variable types.


 




Subscribe to this Blog via Email :

2 comments

Write comments