subjects

Dropdown Menu

Friday 2 April 2021

Conditional Statements(If Statements)

 If statement: If statement evaluates the test condition and will execute statement-block only if the condition is True. If the condition is False, the statement-block is not executed and control is transferred to next statements. The group of statements which has to be executed when the condition is true are written in statements-block. Statement-block  may be a single statement or group of statements  with proper indent.


   
   Flow chart : 



Ex: Python program to check whether the given number is positive or not

num = float(input("Enter a number: "))                

if num > 0:

print("Positive number")

print("Simple if")

Output:

Enter a number 5

Positive number

Simple if


 If –else:

The if-else is an extension of the simple if statement. The if –else statement evaluates the condition, whether the condition is true then the If block will be executed otherwise the else block   will be executed, not both. The general form is


Syntax

                                   


                                    if  test condition:

                                                If block

                                    else:

                                                else block

                                    statement-x


num = float(input("Enter a number: "))

if num > 0:

    print("Positive Number ")

else:

    print("Negative Number ")


Output:

    Enter a number 5

    Positive Number


Monday 29 March 2021

Control statements

 

Control statements are used to change the order of sequence of execution based on some conditions. Various control statements in python are if statement, for statement,           while statement, break, continue and pass.

Decision making and Branching / Conditional / if statements

If statement is a powerful decision-making statement and used to control the flow of execution of statements. Basically it is a two way decision statement and is used with a keyword if and test condition. It allows computer to evaluate the condition first and then, depending on the condition transfers the control to a particular statement. Python supports the following statements in order to achieve decision making capabilities

1.            If statement

2.            If –else

3.            Nested If

4.                 elif




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: