6 Dec 2020

Operator precedence

           When an expression contains more than one operator, the order of evaluation depends on the order of operations.

        

  The acronym PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) is a useful way to remember the rules:

            * Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1)is 4, and (1+1)**(5-2) is 8.You can also use parentheses to make an expression easier to read, as in (minute* 100) / 60, even if it doesn’t change the result.
            * Exponentiation has the next highest precedence, so 1 + 2**3 is 9, not 27, and 2 *3**2 is 18, not 36.
            * Multiplication and Division have higher precedence than Addition and Subtraction. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
            *Operators with the same precedence are evaluated from left to right (except exponentiation).

Boolean values


                                           Boolean data type have two values. They are 0 and 1. 0 represents False, 1 represents True. True and False are keyword . Boolean expressions is an expression that is either true or false. It use the operator == to compare two operands which produces true if the both operands are equal ,false  otherwise.

Program:

>>> 3==5
 False
>>> 6==6 
True
>>> True+True
2
>>> False+True
1
>>> False*True
0

5 Dec 2020

Identity Operators

            They are used to check if two values (or variables) are located on the same part of the memory.

                         


Example:
x = 5 
y = 5 
print(x is not y)
print(x is y)

Output:
false
true


Membership Operators

                            Evaluates to find a value or a variable is in the specified sequence of string, list, tuple, dictionary or not.To check particular item in list or not, in and not in operators are used.
 
                         

Example :
x=[5,3,6,4,1]
>>> 5 in x True
>>> 5 not in x False

Bitwise operators

               A bitwise operation operates on one or more bit patterns at the level of individual bits.

                    

Example :
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print ("Line 1 - Value of c is ", c)
c = a | b; # 61 = 0011 1101
print ("Line 2 - Value of c is ", c) 
c = a ^ b;  # 49 = 0011 0001
print ("Line 3 - Value of c is ", c)
c = ~a; # -61 = 1100 0011
print ("Line 4 - Value of c is ", c)
c = a << 2; # 240 = 1111 0000
print ("Line 5 - Value of c is ", c)
c = a >> 2; # 15 = 0000 1111
print ("Line 6 - Value of c is ", c)

Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4- Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15


Logical Operators

           Logical operators are the and, or, not operators.

             


 Example :   

a = True
b = False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)

Output:
x and y is false
x or y is true
not x is false

Assignment Operators

                  Assignment operators are used in Python to assign values to variables.

                       

Example :
a = 21
b = 10
c = a + b
print("Line 1 - Value of c is ", c)c += a
print("Line 2 - Value of c is ", c)c *= a
print("Line 3 - Value of c is ", c)c /= a
print("Line 4 - Value of c is ", c)c = 2 c %= a
print("Line 5 - Value of c is ", c) c **= a
print("Line 6 - Value of c is ", c) c //= a
print("Line 7 - Value of c is ", c)

output:
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152 
Line 7 - Value of c is 99864

Comparison(Relation) operators

 Comparison operators are used to compare values. It either returns True or False according to the condition.

                  

Example :

a=10
b=5
print("a>b=>",a>b)
print("a>b=>",a<b)
print("a==b=>",a==b)
print("a!=b=>",a!=b)
print("a>=b=>",a<=b)
print("a>=b=>",a>=b)

Output:
true
false
false
true
false
true

Arithmetic operators

                      Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. . . 

                 


      

Example :
a=10
b=5
print("a+b=",a+b)
print("a-b=",a-b)
print("a*b=",a*b)
print("a/b=",a/b)
print("a%b=",a%b)
print("a//b=",a//b)
print("a**b=",a**b)

output:
15
5
50
2.0
0
2
100000

4 Dec 2020

Operators

                       

          Operators are the constructs which can manipulate the value of operands.Consider the expression 4 + 5 = 9 Here, 4 and 5 are called operands and + is called operator.                                                  

Types of Operators:

* Arithmetic Operators
* Comparison (Relational) Operators
* Assignment Operators
* Logical Operators
* Bitwise Operators
* Membership Operators
* Identity Operators


Tuple assingment

* An assignment to all of the elements in a tuple using a single assignment statement. 

* Python has a very powerful tuple assignment feature that allows a tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment. The left side is a tuple of variables; the right side is a tuple of values.

* Each value is assigned to its respective variable.

* All the expressions on the right side are evaluated before any of the assignments.

* This feature makes tuple assignment quite versatile. 

* Naturally, the number of variables on the left and the number of values on the right have to be the same. 

Example : swap of two variables


Tuple packing & Tuple unpacking :

  * In tuple packing, the values on the left side are packed together in a tuple. In tuple unpacking, the values on the right are unpacked in the variables/names.

Example :
 >>b = ("Ben",18,"30000") #tuple packing
>>(name,age,salary) = b #tuple unpacking
>>name
"Ben"
>>age
18
>>salary
"30000"

    




Intendation in python

 * Most of the programming languages like C, C++, Java use braces { } to define a block of code. But, python uses indentation.

Indentation is a very important concept of Python because without proper indenting the Python code, you will end up seeing intendation error and the code will not get compiled.In simple words intendation is the white space in front of a statement .


Example 

def f(n):
   if n==1:
      return 1
   else:
       return f(n-1)
print f(4)





3 Dec 2020

Comments and DOCSTRING

COMMENTS:
   
           * A hash sign (#) is the beginning of a comment.
           * Anything written after # in a line is ignored by interpreter.    
        Eg: percentage = (minute * 100) / 60 # calculating percentage of an hour 
            * Python does not have multiple-line commenting feature. 

     Eg : # This is a comment. 
            # This is a comment, too.
           # I said that already.



DOCSTRING:
       
           Docstring is short for documentation string. It is a string that occurs as the first statement in a module, function, class, or  
method definition. We must write what a function/class does in the docstring. Triple quotes are used while writing docstrings. 

Syntax:

Fuctionname__doc.__ 

Example :

def double(num):
"""Function to double the value"""
return 2*num
>>> print(double.__doc__)
Function to double the value

Statements and Expressions

Statements:

              *Instructions that a Python interpreter can executes are called statements.
           *  A statement is a unit of code like creating a variable or displaying a value. 

>>> n = 17
>>> print(n)

Here, The first line is an assignment statement that gives a value to n. 
The second line is a print statement that displays the value of n.



Expressions: 

             * An expression is a combination of values, variables, and operators.
          
              * A value all by itself is considered an expression, and also a variable. So the following are all legal expressions: 

>>> 42
42
>>> a=2
>>> a+3+2
7
>>> z=("hi"+"friend")
>>> print(z)
hifriend

2 Dec 2020

Variables, keywords and identifiers

Variables :

         * A variable allows us to store a value by assigning it to a name, which can be used later.
         * Programmers generally choose names for their variables that are meaningful.
         * Named memory locations to store values.
          * We don't need to declare a variable before using it. 
           
           It can be of any length. No space is allowed.

Keywords :

          * Keywords are the reserved words in Python.
          * We cannot use a keyword as variable name, function name or any other
identifier.
          * They are used to define the syntax. 

  

Identifiers :

                    Identifier is the name given to entities  like class, functions, variables etc. in Python.
            
       * Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A  to Z) or digits (0 to 9) or an underscore ( _ ). 

       * An identifier cannot start with a digit. 
     
       *Keywords can't be used as identifiers
 
        *Cannot use special symbols like !, @, #, $, % etc. in our identifier.

         *Identifier can be of any length.



Values and data types

Values :
   
         * Value can be any letter, number or stings. For example ; 2 , 4.56 , "Hello" (these values are belongs to different data types)

Data types :
     
           * Every value in python has data type

            * It is a set of values, and the allowable operations on these values. 
  
             * Python has four standard data types they are :

            

Numbers :

         * This data type stores numeric values and it is immutable ( values/items cannot be changed).

          * There are three different types in numbers :
                     • Integer (int)
           
                     • floating point (float)

                     • Complex number

Integer :

           Integers or int are the whole number. They can be positive or negative but they must be without decimal values .

             Eg : 0 , 28 ,-30 etc. . .

Float :

           A float or floating point number contains only decimal values.

              Eg : 3.14 , - 4.5 etc....

Complex :

          A complex number consist of two parts real and imaginary. The imaginary part written with 'j' suffix.

                Eg : 3+ 5j

Dictionary :
               
             A dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair and they are written within Curley braces { } . We Separate key and values with :

                   Eg : {"year" : 2000}
                               Key.              Value

Boolean :

           Data with one of two build-in values
True or False. 

Note : 'T' (True) and 'F'(False) are must be uppercase letters. 

Sequence :

       A sequence is an ordered collection of similar data types. Python has the following built-in sequence data types.

String :

       A string value is a collection of one or more characters put in single (' '), double(" ")or  triple quotes( "' "').Strings are immutable i.e. the contents of the string cannot be changed after it is created.

          Eg : * 'Good morning'
         
                  * "Hello"
  
                  * "' world is awesome"'

Tuple :

      A tuple object is an ordered collection of one or more data items they are put into the parentheses ( ). A tuple is an immutable list. i.e. once a tuple has been created, you can't add elements to a tuple or remove elements from the tuple. Tuples are faster than lists. If the user wants to protect the data from accidental changes, tuple can be used. Tuples can be used as keys in dictionaries.

              Eg : ( " this is awesome")

List :

           List is an ordered sequence of items. Values in the list are called elements / items. It can be written as a list of comma-separated items (values) between square brackets[ ]. Items in the lists can be of different data types. 

Operations on sequence

              * Indexing
      
              * Slicing

              * Concatenation

              * Repetitions

               * Updation, Insertion, Deletion.

Indexing :
   
             Accessing the position of the items.

                        

                 
           Positive indexing helps in accessing the string from the beginning and Negative subscript helps in accessing the string from the end. Subscript 0 or negative n (where n is length of the string) displays the first element.Example: A[0] or A[-5] will display “H”.

Slicing :

          Slicing means taking elements from one given index to another one. we pass slice instead of index [start:end]. we can also define as[start:end:step].

Concatenation :

               Merge one string into another one. for example, a = 'hello' and b = 'world' c = a+b.
 
                                           

Repetition:
      
                          Repetition is used to repeat the string in the program for certain lengths, It is denoted by the symbol  ' * ' .

Example :
a = " happy forever"
print (a*2);

output:
     "happy forever happy forever" 

Updation : Updating the list using index value.

Insertion : Insert an elements in needed position.

Deletion : Remove an element which is not needed.                                                          

1 Dec 2020

Python interpreter

             To execute a program in a high-level language by translating it one line at a time.
                       
                            


            

Modes of python interpreter:
    
               Python interpreter is a program that reads and executes python code, it uses two modes of execution,

                    *Interactive mode
       
                    *Script mode

Interactive mode:

               *Interactive mode as the name suggests, allow us to interact with operating system. 
  
                *When we type python statement, interpreter displays the results immediately.

                *Python in interactive mode is good enough to learn, experiment or explore.

                 *Working in interactive mode is convenient for beginners and for testing small pieces of code.

                  * In interactive mode you type python programs and the interpreter displays the result : 
                            >>>1+1
                                   2

The Chevron>>>, is the prompt the interpreter uses to indicate that it is ready for you to enter code. If you type 1+1,the interpreter replies 2.
>>>Print ('Hello,world!')
Hello,world!

Script mode:

           *In script we type python program in a file and then use interpreter to execute the content to the file.
   
            *Script can be saved to disk for future use. Python scripts have the extension  .py, meaning that the file names ends with .py
   
            * Save the code with filename .py and run the interpreter in script mode to execute the script.
 
Program:

        Print(1)
          x = 2
          Print(x)

Output:
>>> 1
       2
       
Integrated Development Learning Environment (IDLE):

         *It is graphical user interface which is completely written in python.
 
         *It is bundled with the default implementation of the python language and also comes with optional part of the python packaging.

             
Features of IDLE :

             * multi-window text editor with syntax highlighting.
      
              * Auto completion with smart intendation.

               * Python shell to display output with syntax highlighting.

               

     
              
                  

Introduction to python

               A program is a set of instructions that a computer follows in order to perform a specific task. There are different types of programming languages such as BASIC, Pascal, C, C++, Java, Ruby, Python etc.... In this site we will see about python.   
  
                                         


           Python is a general-purpose interpreted, interactive, object-oriented and high level programming language. It was created by Guido van Rossum during 1985-1990.Python got it's name from "monty  
 python's flying circus" . It was released in the year of 2000.

* Python is interpreted : python is processed at runtime by the interpreter. You don't need to compile your program before executing it.

*Python is interactive : you can actually sit at a python prompt and interact with the interpreter directly to write your programs.

*Python is object-oriented : python is great language for the beginner level programmer's and support the development of a wide range application.

*Python is high-level programming language : when writting a program's programmer's concentrate on solution's of the current problem, no need to worry about the low level details.

Operator precedence

           When an expression contains more than one operator, the order of evaluation depends on the order of operations.            The ac...