Python - Variable Declaration and Operator

 

Python

Variable Declaration and Operator



Fill in the blanks

1.   How many operator presents in python  6

2.   Str=’Hello world’.str[2:5]   =  llo

3.   Str =”Mother”. Str[-5]=   o

4.   S=”python”.s*3=  pythonpythonpython

5.   S1=’hai’,s2=’hello’ .Write the output of string concatenation  haihello

6.   What is Concatenation operator  + 

7.   a=”happy morning”. a[-5:11]=   orni

8.   a=5.0 . type(a) = <type ‘float’>

9.   List is Mutable

10.         Tuple is Immutable

11.         List & Tuple index starts from  0  index

12.         s=[3,8,2,1,9,5,7]. a[1:]=  8,2,1,9,5,7

13.         List bracket [square bracket] , Tuple bracket (Open & close bracket)

14.         String is immutable

15.         Collection of character is called String

16.         Set is Collection of unique items

17.         Write a syntax of Dictionary   key:value

18.         A={2,2,2,2,5,6,5,7,7,8,8,8,9}.what is the output of above set = set([2,5,6,7,8,90])

19.         B={‘school’:’student’,’book’:’maths’}.B[‘book’]=  maths

20.         int(-100.7).what is the output of the type conversion    -100

21.         print(2,3,6,9,sep=’@’,end=’*’).write the output = 2@3@6@9*

22.         print(‘I had visited {1},{0} and {2}’.format(‘chennai’,’theni’,’madurai’)) I had visited theni,chennai and madurai

23.         x=45.89344354.print(‘%3.2f’%x) =   45.89

24.         a=2,b=3; a**b =  8

25.         a=5;b=2;a//b  =    2  

26.         a=5;b=2;a/b  =    2.5  

27.         x=6;y=6; print(x is y)=  True

28.         c={1:56,3:7}.print(7 in c) = False

29.         x=’welcome’.print(‘m’ not in x) =  False

30.         a=[4,5,6].b=[4,5,6].print(a is not b)= False

 

Answer the following Question

                                                                             (4 marks)

1.write arithmetic operator program

x = 15

y = 4

print('x + y = ',x+y)

print('x - y = ',x-y)

print('x * y =',x*y)

print('x / y =',x/y)

print('x // y =',x//y)

print('x ** y =',x**y)

2.write bitwise operator program

x =2

y = 3

print('x & y = ',x&y)

print('x | y = ',x|y)

print('x ^ y =',x^y)

print('~x  =',~x)

print('x <<2 =',x<<2)

print('x >> y =',x>>y)

3.write Comparison operator program

x=10

y=12

Print('x>y is', x>y)

Print('x<y is', x<y)

Print('x==y is', x==y)

Print('x!=y is' ,x!=y)

Print('x>=y is', x>=y)

Print('x<=y is', x<=y)

4.write logical operator program

Operator

Meaning

Example

And

True if both the operands are true

X and Y

Or

True if either of the operands is true

X or Y

Not

True if operands is falce(complements the operand)

not X

EXAMPLE:

X=True

y=False

print('x and y is', x and y)

print('x or y is', x or y )

print('not x is', not x)


5.write assignment operator program

Operator

Example

Equivalent to

+=

X+=5

X=X+5

-=

X-=5

X=X-5

*=

X*=5

X=X*5

/=

X/=5

X=X/5

 

>>> a=6

>>> a+=3

>>> a

9

>>> a-=2

>>> a

7

>>> a*=3

>>> a

21

>>> a/=3

>>> a

7.0


 

Answer the following Question

                                                                             (10 marks)

1.what is operator ?. Write 6 operator Program.

 Python Operator

6 Type Of Operators In Python 

¨    Arithmetic Operator

¨    Comparison (Relational) Operator

¨    Logical Operator

¨    Bitwise Operator

¨    Assignment Operator

¨    Special Operator

Arithmetic Operator

arithmetic operator are used to perform mathematical operation like addition, subtraction, multiplication etc.

Operator

Meaning

Example

+

Add two operands

X+y+2

-

Subtract right operand from the left

x-y-2

*

Multiply two operands

X*y

/

Divide left operand by the right one

x/y

%

Modulus-remainder of the division of left operand to the right

X%y

//

Floor divison-division that result into whole number adjusted to the left in the number line

x//y

**

Exponent-left operand raised to the power of right

X**y

 

Example:

x = 15

y = 4

print('x + y = ',x+y)

print('x - y = ',x-y)

print('x * y =',x*y)

print('x / y =',x/y)

print('x // y =',x//y)

print('x ** y =',x**y)

 

output :

x + y = 19

x - y = 11

x * y = 60

x / y = 3.75

x // y = 3

x ** y = 50625

Comparison Operator:

comparison operators are used to compare values. it either returns true or false according to the condition.

 

Operator

Meaning

example

Greater than-true if left operand is greater than the right

X > y

Less than - true if left operand is less than the right

X < y

==

Equal to - true if both operands are equal

X == y

!=

Not equal to-true if operands are not equal

X!=Y

 

>=

Greater than or equal to-true if left operand is greater than or equal to the right

X>=Y

<=

Less than or equal to-true if left operand is less than or equal to the right

X <= y

 

 

 

 

 

Example:

X=10

Y=12

Print('x>y is', x>y)

Print('x<y is', x<y)

Print('x==y is', x==y)

Print('x!=y is' ,x!=y)

Print('x>=y is', x>=y)

Print('x<=y is', x<=y)

OUTPUT

X>Y is false

x<y is true

x==y is false

x!=y is true

x>=y is false

LOGICAL OPERATORS

Operator

Meaning

Example

and

True if both the operands are true

X and Y

or

True if either of the operands is true

X or Y

not

True if operands is falce(complements the operand)

not X

EXAMPLE:

X=True

y=False

print('x and y is', x and y)

print('x or y is', x or y )

print('not x is', not x)

OUTPUT

x and y is false

x or y is true

not x is false

 

 

BITWISE OPERATORS

Operator

Meaning

Example

&

Bitwise AND

X&Y=0(0000 0000)

|

Bitwise Or

X/Y=14 (0000 0000)

~

Bitwise NOT

~X=-11 (1111 0101)

^

Bitwise XOR

X^ y= 14 (0000 1110)

>> 

Bitwise right shift

X>>2=2(0000 0010)

<< 

Bitwise left side

X<<2= 40 (0010 1000)

Example:

x =2

y = 3

print('x & y = ',x&y)

print('x | y = ',x|y)

print('x ^ y =',x^y)

print('~x  =',~x)

print('x <<2 =',x<<2)

print('x >> y =',x>>y)

output

x & y =  2

x | y =  3

x ^ y = 1

~x  = -3

x <<2 = 8

x >> y = 0

 

ASSIGNMENT OPERATORS:

Operator

Example

Equivalent to

+=

X+=5

X=X+5

-=

X-=5

X=X-5

*=

X*=5

X=X*5

/=

X/=5

X=X/5

 

>>> a=6

>>> a+=3

>>> a

9

>>> a-=2

>>> a

7

>>> a*=3

>>> a

21

>>> a/=3

>>> a

7.0

 

SPECIAL OPERATORS

Identity Operators

OPERATOR

MEANING

EXAMPLE

is

True if the operands are identical

X is true

is not

True if the operands are  not  identical

X is not true

 

example

x1=5

y1=5

x2='Hello'

y2='Hello'

x3=[1,2,3]

y3=[1,2,3]

print(x1 is not y1)

print(x2 is y2)

print(x3 is y3)

output

False

True

False

 

Membership Operator

OPERATOR

MEANING

EXAMPLE

in

True if value/variable is found in the sequence

5 in x

not in

True if value/variable is not found in the sequence

5 not in x

 

x="Hello world"

y={1:'a',2:'b'}

print('H' in x)

print('hello' not in x)

print(1 in y)

print('a' in y)

 

output

True

True

True

False

 

 

 

No comments:

Post a Comment