Wednesday, 7 August 2019

Example programs on Arithmetic Operators

#Example program on All Arithmetic Operators

a=10 # value 10 is stored in variable a
b=2 # value 2 is stored in variable b

print('a+b=',a+b) #Addition operator

print('a-b=',a-b)#Subtraction operator

print('a*b=',a*b)#Multiplication

print('a/b=',a/b)#Division operator

print('a//b=',a//b)#Floor Division (integer Division)

print('a%b=',a%b)#Modulus operator Which gives remainder

print('a**b=',a**b)#Exponenet operator

"""output
a+b= 12
a-b= 8
a*b= 20
a/b= 5.0
a//b= 5
a%b= 0
a**b= 100
"""
#Ex2:Arithmetic OPerators
a=10.5
b=2
print(a+b)#12.5
print(a-b)#8.5
print(a*b)#21.0
print(a/b)#5.25
print(a//b)#5.0
print(a%b)#0.5
print(a**b)#110.25
"""output
12.5
8.5
21.0
5.25
5.0
0.5
110.25
"""
Note:

 (Devision)/ operator always performs floating point arithmetic so it will always returns float value. But floor division or integer division (//) can perform both floating point and integral arithmetic.
If values are integers then output is integer. If atleast once value is float then result is float.

If we want to use + operator for strings, then both parameters should be strings only otherwise we will get error.

Example:
>>>'rajendra'+15 #Error
>>> 'rajendra'+'15' #ok
'rajendra15'
>>> 

Ex2: If we use * operator for strings then we must use one parameter should be integer and other parameter should be string type

>>> "rajendra"*10
'rajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendra'
>>> 10*"rajendra"
'rajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendra'

We can’t multiply sequence by non-integer type ‘float’
Example:
>>> 2.5*"rajendra" #Error
>>> 

Note:
We can not perform zero division, If we perform zero division it leads to error.

Example:
>>> a=5
>>> a/0 #Error Division by Zero

Evaluate the following expression:
d=(x+y)*z**a//b+c
where x=1;y=2;z=3;a=2;b=2;c=3
>>> d=(1+2)*3**2//2+3
d=3*3**2//2+3 #parenthesis evaluated
d=3*9//2+3 #Exponential operator is evaluated
d=27//2+3 #Multiplication, division, and floor divisions are at equal priority
d=13+3 #addition and subtraction are afterwards
d=16 #finally assignment is performed, the value is 16 now stored in variable ‘d’
>>> print(d)
16
>>> 


Previous Topic: Arithmetic Operators
Next Topic: Relational Operator

Sunday, 4 August 2019

Assessment 5

Assessment 5


1. What is the syntax of the following piece of code?
>>> a=frozenset(set([5,6,7]))
>>> a
a) {5,6,7}
b) frozenset({5,6,7})
c) frozen ({5,6,7})
d) frozenset(set{5,6,7})
Ans: B

2. What is the output of the following code?
>>> a={1,2,3}
>>> b=frozenset([3,4,5])
>>> a-b
a) {1,2}
b) {1,2,4,5}
c) {4,5}
d) {12}
Ans: A

3. Create 10 phone numbers with names and display it.
Ans:
>>> d={8980:"raj",43424:"ravi"}
>>> d
{8980: 'raj', 43424: 'ravi'}

4. Write a Python script to merge two Python dictionaries.
d1 = {'a': 100, 'b': 200}
d2 = {'x': 300, 'y': 200}

Ans:
d1 = {'a': 100, 'b': 200}
d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)

5. Evaluate the following expression
>>> b=4+5-2*5/2
>>>
Ans: 4.0

6. Write a Python program to add a key to a dictionary ex:(2:30).
d = {0:10, 1:20}
Ans:
d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)

7. Which of these about a frozenset is not true?
a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type
Ans: A

8. Write a logic to Display the following output:
              2
              6
             10
             14
             18

Ans:
>>> a=range(2,20,4)
>>> for i in a:print(i)

9. What is the output: a=bytes([10,'sita',True,2+3j]

Ans: Error

10. What is the operators? How many types of operators in python?
Ans:

An operator is a symbol which is used to perform an operation on operands (values and variables).Types of operators:
1.  Arithmetic
2.  Assignment
3.  Relational operators
4.  Logical operator
5.  Boolean operator
6.  Bitwise operator
7.  Membership
8.  Identity operators

Youtube channel click here to see this video
Next Topic: Operators
Assessment 6
Previous Assessment: Assessment 4

You may like the following posts:


Monday, 29 July 2019

Nested loops


Ex: wap to display *’s in pyramid style or triangle shape
n=int(input("enter no of rows "))
for i in range(1,n+1):
    print(" "*(n-i),end="")
    print("* "*i)
"""output:
enter no of rows 3
  *
 * *
* * *

Nested Loops:

Loop inside a loop is called Nested Loops.

Nested Loops:
for i in range(2):
    for j in range(2):
        print(i,j)
"""output:
0 0
0 1
1 0
1 1
>>> """
for i in range(1,2):
    for j in range(2):
        print(i,j)
output:
1 0
1 1
>>> 

#find out the output:


n=int(input("enter no of rows:"))
for i in range(1,n+1):
    for j in range(1,n+1):
        print("*",end=" ")
    print()

#find out the output:
n=int(input("enter no of rows:"))
for i in range(1,n+1):
    print("* " *i)
enter no of rows:3
*
* *
* * *
enter no of rows:3
*
* *
* * *



Next Topic: Unconditional statements
Previous Topic: while loop


The else suite


The else suite:
#Find out the output:
for i in range(3):
    print("Hello")
else:
    print("Bye")

"""output:
Hello
Hello
Hello
Bye
>>> """

Example2:
#Find out the output:
for i in range(0):
    print("Hello")
else:
    print("Bye")

"""output:
Bye
>>> """
In python, it is possible to use ‘else’ statement along with loops (for or while), this is called The else suite.

Syntax: for loop

for variable in sequence:
    statement(s)
else:
    statement(s)
syntax: while:
while condition:
    statement(s)
else:
    statement(s)

#searching the data
a=[10,20,30]
n=int(input("enter data to search "))
for i in a:
    if n==i:
        print("Data is found")
        break;
else:
    print("Data is not found")
"""output:
enter data to search 20
Data is found
>>> """


Related Video: https://youtu.be/rUij4z3DVjA

Next Topic: Strings and Characters
Previous Topic: Assessment 8

Bitwise Operators


Bitwise Operators

These operators act on individual bits (0 and 1) of the operand. We can use Bitwise operators directly on binary numbers or on integers also.

Bitwise Operators
Meaning
&
If both bits are 1 then only the result is 1 otherwise result is 0
|
If at least one bit is 1 then the result is 1 otherwise result is 0
^ (Cap operator)
It bits are different then the only result is 1 otherwise result is 0
~ (tilde)
Bitwise complement operator i.e 1 means 0 and 0 means 1
>> 
Bitwise left shift operator
<< 
Bitwise right shift operator

Bitwise AND Operator(&):

This operator performs AND operation on the individual bits of numbers. The symbol for this operator is ‘&’, which is called ampersand.


The AND gate circuit present in the computer, the chip will perform the AND operation.





Bitwise operators are applicable only  for int and Boolean types.
By mistake, if we are trying to apply for any other type then we will get error.
print(4&5)-àValid
print(2.5&6.2)-àError:
print(2.3&2) -àError
>>> print(True&True)
True
>>> print(True&False)
False
>>> print(2&6)
2

2
2

2
1
0
2
0
1

Read the remainders from bottom to up
10 remaining 6 bits filled with 0s
i.e 000000 10

Read the remainders from bottom to up
Binary form of 6 is 110 (remaining 5 bits are filled with 0s) i.e
00000111
A&b is:
A:-à  0 0 0 0 0 0 1 0
B:à   0 0 0 0 0 1 1 1
-----------------
A&b= 0 0 0 0 0 0 1 0
------------------------

Now we need to convert from binary to decimal:
0
0
0
0
0
0
1
0
27
26
25
24
23
22
21
20
128
64
32
16
8
4
2
1
0
0
0
0
0
0
2
0

Add 1’s: 0+0+0+0+0+0+2+0=2
So result is 2

Steps to convert decimal to Binary
1.  Write down the decimal number ex: 6
2.  Divide the decimal number by 2
3.  Write the result underneath (below)

4.  
Write the remainder on the right hand side. This will be 0 or 1

5.  Divide the result of the division by 2 and again write down the remainder
 6.  Continue dividing and writing down remainders until the result of the division is 0

7.  Read the remainders from bottom to up

Steps to convert binary to decimal:
Converting from binary to decimal involves multiplying the value of each digit.
1.  Write down the number
2.  Multiply the digit by value place holder
3.  Continue doing this until you the number
Add the result together (only 1’s)

Related Video:  https://youtu.be/xBNdZ2ytTiU




Next Topic: Bitwise AND Operator (&)
Previous Topic: Membership Operators

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...