subjects

Dropdown Menu

Monday 22 February 2021

Python Statements

 

A statement is an instruction that a Python interpreter can execute. In python we use various statements to write the code. Some of the statements available in Python are assignment statement, print statement, control flow and the import statement.

Assignment Statement:

  Assignment statements are used to set value to a variable

Ex: a = 10

in this statement value 10 is  stored into variable a.

we can use assignment statement in evaluating an expression

     f  = c *9.0/5.0 + 32

This expression converts Celsius into Fahrenheit

 

We can assign several variables at once

  a = b = c =10

 

Python permits number of variables to assign with different values with comma separated values

                   x , y = 10,20

is same as

                   x = 10

y = 20

 

Input and Output Statements

 

Input from the user: We use input function to read data from user,

>>>input(“Enter a number”)

…Enter a number 10

'10'  Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

>>> int(10)

10

>>>float(10)

10.0

Output:

print statement: In python  we use  print statement to display output in console

>>> print(“Hello world”)

will display Hello world as  output

formatting output

The print statement produces a more readable output, by omitting the enclosing quotes and by printing escape sequences, comma separator and special characters:

 

 \'     display   '                            \"   display        "\t  tab space                \n new line

Monday 15 February 2021

Operators

 An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.

Ex:

          c = a + b

In this statement      +, = are operators and a, b, c are operands (variables).in this instruction the sum of two variables a, b is carried out and the result is stored in variable c.

 

Operators in Python

 

1. Arithmetic Operators

2. Relational Operators (Comparison Operators)

3. Logical Operators

4. Bitwise operators

5. Assignment Operators

                  6. Special Operators   

Arithmetic Operators:  All the arithmetic operators will perform basic calculations. The operators +,-,*, /, //, % all work the same way as in other languages.

Python Program Demonstrating the use of arithmetic operators                       

a = int (input ("Enter First Value"))

b = int (input ("Enter Second Value"))

print("sum", a+b)

print("difference", a-b)

print("product", a*b)

print("division", a/b)

print("modulus", a%b)

print("floor division", a//b)

print("exponent", a**2)

 

                                                                  Arith.py

 



Relational Operators:  Relational Operators used in comparing the quantities and to formulate some conditions. The value of a relational expression is either true or false.

  Ex:   10 < 100 is true

          100 < 10 is false

Operator

Meaning

     <

     <=

     >

     >=

     ==

     !=

     Is less than

     Is less than or equal to

     Is greater than

     Is greater than or equal to

     Is equal to

     Is not equal to


                      Table: Relational Operators


Python Program Demonstrating the use of Relational operators                        

a = int (input ("Enter First Value"))

b = int (input ("Enter Second Value"))

print("less than: ",a<b)

print("less than or equal to: ",a<=b)

print("greater than: ",a>b)

print("greater than or equal to: ",a>=b)

print("equal to:" ,a==b)

 

 

                Relational.py

output



Logical Operators: In order to evaluate more than one condition we use logical operators.in python we use and, or, not as logical operators

Operator

                 Meaning

       and

       or

       not

 

             logical And

             logical Or

             logical Not

                                            Table: Logical Operators

Python Program Demonstrating the use of Logical operators                       

a = True

b = False

print(a and b)

print(a or b)

print(not a)

                                                Logical.py

Output:



Bitwise operators: we use bitwise operators to manipulate data at bit level.

 Operator

    Meaning

     &

      |

      ^

     <<

     >>

      ~

    Bitwise AND

    Bitwise Or

    Bitwise Exclusive Or

    Shift left

    Shift Right

    Bitwise Inverse

                                      Table: Bitwise operators

Python Program Demonstrating the use of Bitwise operators                       

a = int(input ("Enter First Value"))

b = int(input ("Enter Second Value"))

print("Bitwise AND :",a&b)

print("Bitwise Or :",a|b)

print("Bitwise Exclusive Or :",a^b)

print("Bitwise Inverse :",~a)

print("Shift left :",a<<b)

print("Shift Right :",a>>b)

                                         Bitwise.py

 Output:



ASSIGNMENT Operators: Assignment operator used to store some value        (right side)into   variable(left side). Compound-assignment operators provide a        shorter syntax for assigning the result of an arithmetic or bitwise operator. They    perform the operation on the two operands before assigning the result to the first   operand.

Simple assignment statement

Short Hand Operator

      a = a+1

      a = a-1

      a = a* 1

      a = a / 1

      a = a % b

      a = a&b

      a = a | b

      a = a ^ b

      a = a << b

      a = a >> b

 

       a += 1

       a -=1

       a *=1

       a /=1

       a %= b

       a &= b

       a |= b

       a ^=  b

       a <<= b

       a >>= b

                                                 Table: Assignment Operators

Python Program Demonstrating the use of Assignment operators                       

a = int(input ("Enter First Value"))

b = int(input ("Enter First Value"))

a += b

print("+=",a)

a -= b

print("-=",a)

a *= b

print("*=",a)

a /= b

print("/=",a)

a %= b

print("%=",a)

a //=b

print("//=",a)

a **=b

print("**=",a)

a &= b

print("&=",a)

a |= b

print("|=",a)

a ^=  b

print("^=",a)

a <<= b

print("<<=",a)

a >>= b

print(">>=",a)

 

                                                     Assignment.py

 

Output:



Special Operators:

Membership Operators: in and not in are membership operators in Python

 

            Operator

         Meaning

               in

               not in     

         Member of sequence

    Not a member of sequence

Table:Membership Operators

Python Program Demonstrating the use of membership operators                

list1 = [1,2,3,'Ram','Rani',3.2]

print(1 in list1)

print(1 not in list1)

print('Ram' in list1)

print(5.5 not in list1)


Output:

                           t

Identity operators: “is” and “is not” are the identity operators in Python. They are used to               check if two values (or variables) are located on the same part of the memory. Two                                    variables      that      are equal does not imply that they are identical.

              Operator

            Meaning

               is

               is not  

 Same memory location

Not same memory location



                                                                   Table: Identity Operators                                                                                                                           

Python Program Demonstrating the use of Identity operators                

#for integer values

a = int(input ("Enter First Value"))

b = int(input ("Enter Second Value"))

print("IS",a is b)

print("iS NOT :",a is not b)

#for string values

s1 = 'Hello'

s2 = 'Hello'

print(s1 is s2)

#for list

L1 = [1,2,3]

L2 = [1,2,3]

Print(L1 is L2)

 Output:



Tokens

 Tokens  :  A token is smallest individual unit in a program. 
1. Keywords

2. Identifiers

3. Literals

4. Operators

5. Special Symbols

1.    Keywords: Keywords are the reserved words. We cannot use a keyword as variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.

Python    has  the following  keywords

False           class              from                  or
None           continue        global                pass
True            def                 if                       raise
and              del                 import               return
as                elif                 in                      try
assert          else                is                       while
async          except            lambda             with
await          finally             nonlocal            yield                             
break          for                   not                       

       2.    Identifiers: Identifier is the name given to entities like class, functions, variables etc.     In Python, It helps in differentiating one entity from another.

        Rules for writing identifiers in Python:

  1. 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 (_).                                                                          Names like student, sno, marks, stu_mno, all are valid example.
  1. An identifier cannot start with a digit.                                                                                   1variable is invalid,  but variable1 is perfectly fine.   
  2. Keywords cannot be used as identifiers.
  3. We cannot use special symbols like! , @, #, $, % etc. in our identifier.
  4. Identifier can be of any length.
3. Literals :  Literals are constant values

Ex:   subject = “Computer Science”

         Pi = 3.14

         “\n” - newline

         “\t” -   tab space

 



4.  Operators: Symbol that used to perform arithmetic and logical operations

Ex : + - < and in << >>



5. Special Symbols: ‘ “ # . \ * + ? [ ] ( ) { } etc.