Artificial intelligence, robotics, PHP, Javascript, hacking, CSS, Photoshop, deep learning, machine learning, Visual basic

Saturday, 18 August 2018

Python | Python syntax

Python | Python syntax


Python is an interpreted, object-oriented programming language similar to PERL, that has gained popularity because of its clear syntax and readability. Python is said to be relatively easy to learn and portable, meaning its statements can be interpreted in a number of operating systems.

Comment

Comments are used to document and are ignored by the compiler.
In python, comments are of two types. That is,
·        Single line comment
·        Multi-line comments

Single line comments
In python, all comment characters are written after the # sign.

Example

# Python Basic Syntax - Example Program
# We are comments


Multiline Comment in Python
Multiline starting commenting code is ''' and ending with same, that is '''.
Example
''' This
    is
    multiline
    comment
'''
''' Multiline comment
    starts with \'''
    and ends with
    \'''
'''

Printing statement
General form to use print statement in python.
print("any character or string");
 
In the above syntax of print statement, print is the keyword, opening (" and closing ") followed by ; are the syntax to write print statement in python to successfully output any required combination of characters on the output screen.
 
Variables
Variables in Python, are the reserved memory locations to store the values in a Python program. To create a variable and assign some values to it, follow the given syntax or general form in Python.

variable_name = "value to be assign";
 
You can also say that the above statement is called as assignment statement. In the above statement variable_name is the name of the variable, and the value is assine inside “ and “ sign.
 
Operators
Operators in Python, are used to perform mathematical and logical operations. There are the following types of operators available in Python:
  • Arithmetic Operators
  • Logical Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators
Arithmetic operators

Operator
Name
Meaning
+
Addition Operator
Add two values
-
Subtraction Operator
Used for subtraction
*
Multiplication Operator
Used for multiplication
/
Division Operator
Used for division
%
Modulus Operator
Returns remainder after dividing
//
Floor Division Operator
Returns the quotient without any digits after decimal
**
Exponent Operator
Used to perform exponential calculation on operators
 
Comparison operators

Operator
Meaning
==
Returns true if values of the two operands are equal, otherwise returns false if not equal
!=
Returns true if value of the two operands are not equal, otherwise returns false if equal
> 
Returns true if value of the left operand is greater than right one, otherwise false if value of the right operand is greater than left one
< 
Returns true if value of the left operand is less than right one, otherwise false if value of the right operand is less than left one
>=
Returns true if value of the left operand is greater than or equal to the value of the right one, otherwise false if value of the right operand is greater than left one
<=
Returns true if value of the left operand is less than or equal to right one, otherwise false if value of the right operand is less than or equal to left one
 
Assignment operators

Operator
Name
Meaning
=
Simple Assignment Operator
Used for assigning values
+=
Add and Assignment Operator
This operator adds the right operand to the left operand and assigns the result to the left operand
-=
Subtract and Assignment Operator
This operator subtracts the right operand from the left operand and then assigns the result to the left operand
/=
Divide and Assignment Operator
This operator divides the left operand with the right operand and then assigns the result to the left operand
*=
Multiply and Assignment Operator
This operator multiplies the right operand with the left operand and then assigns the result to the left operand
**=
Exponent and Assignment Operator
This operator performs the exponential calculation on operators and then assign the value to the left operand
%=
Modulus and Assignment Operator
This operator takes modulus using the two operands and then assigns the result to the left operand
//=
Floor Division and Assignment Operator
This operator performs the floor division on the operators and then assign the value to the left operand
 
Bitwise operators    
              
Operator
Name
Meaning
&
Binary AND Operator
This operator copies a bit to the result only if its exists in both the operands
^
Binary OR Operator
This operator copies a bit only if it exists in either the operand
|
Binary XOR Operator
This operator copies the bit only if it is the set of one operand but not the both
~
Binary Ones Complement Operator
This is unary operator, has the effect of 'flipping' bits
>> 
Binary Right Shift Operator
The left operand's value is moved right by the number of bits specified by the right operand
<< 
Binary Left Shift Operator
The left operand's value is moved left by the number of bits specified by the right operand
 
 Logical operators

Operator
Name
Meaning
and
Logical AND Operator
If both the operands are true, then the condition becomes true, otherwise false
not
Logical NOT Operator
This operator used to reverse the logical state of its operand
or
Logical OR Operator
If any of the two operands are non-zero, then the condition becomes true, otherwise false
 
Import statement
The import statement is used to import or load modules (modules are files, contains code to be used in other program just by importing that module using the import statement), for example, to load random module,
 General form.
import random
 
Example
import random
val1 = random.randrange(100);
val2 = random.randrange(100);
print("Following are the two generated random numbers \
under 100.");
print("First: ",val1);
print("Second: ",val2);
 
Function
A function in python, is a block of code, used to perform a particular action.
Rules for defining a function
To define a function in python, follow these rules:
  • function blocks in python, begins with the keyword def, followed by the function name and then parentheses
  • the parameters/arguments should be placed inside these parentheses
  • the code block of the function starts with a colon (:)
  • and now the return statement exits a function, passing back an expression to the function caller
  • a return statement with no any arguments is the same as return None
General form

def function_name(parameters):
               "function_doc_string"
               function_suite
               return [expression]
 
Example
def printit(strn):
                    "This will print the passed string into this function"
                    print(strn)
                    return
 
Decision making statement
Decision-making statements in python help in controlling your program accordingly. There are the following decision-making statements available in Python:
  • if statement
General form

if expression:
            statement(s)

  • if-else statement
General form
if expression:
                    statement(s)
else:
                    statement(s)

  • nested if statement
General form
if expression1:
               statement(s)
               if expression2:
                               statement(s)
               elif expression3:
                               statement(s)
               else:
                               statement(s)
else:
               statement(s)

Loops
Loops in Python are used to control your program. Loops are basically used to execute a block of code several numbers of times accordingly. There are the following types of loops available in Python:
  • for loop
general form
for iterating-var in sequence:
                    statement(s)

  • while loop
General form
while expression:
statement(s)

  • nested loop
General form (nested for loop)
for iterating-var in sequence:
               for iterating-var in sequence:
                               statement(s)
               statement(s)

     General form (nested while loop)
while expression:
   while expression:
      statement(s)
   statement(s)


Share:
Location: Delhi, India

0 comments:

Post a Comment

Copyright © Technotalk | Powered by Academy for brilliance