0% found this document useful (0 votes)
44 views10 pages

Session1 Programs

The document contains code examples demonstrating basic Python concepts like variables, strings, data types, operators, and control flow. It shows how to assign values to variables, perform operations on strings and numbers, use comparison, assignment, bitwise, and other operators, and check conditions using if/else statements. A variety of operator precedence examples are also included to illustrate the order in which operations are performed.

Uploaded by

deepasce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views10 pages

Session1 Programs

The document contains code examples demonstrating basic Python concepts like variables, strings, data types, operators, and control flow. It shows how to assign values to variables, perform operations on strings and numbers, use comparison, assignment, bitwise, and other operators, and check conditions using if/else statements. A variety of operator precedence examples are also included to illustrate the order in which operations are performed.

Uploaded by

deepasce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

https://www.tutorialspoint.com/execute_python_online.

php

Variables
#!/usr/bin/python

counter = 200 # An integer assignment

miles = 1000.0 # A floating point

name = "Jim" # A string

print(counter)

print(miles)

print(name)

Python String variables

#!/usr/bin/python

str = 'Hello World!'

print(str) # Prints complete string

print(str[0]) # Prints first character of the string

print(str[2:5]) # Prints characters starting from 3rd to 5th

print(str[2:]) # Prints string starting from 3rd character

print(str * 2) # Prints string two times

print(str + "TEST") # Prints concatenated string

Data type Conversion


Operators

#!/usr/bin/python

a = 21

b = 10

c=0

c=a+b

print ("Line 1 - Value of c is ", c)

c=a-b

print ("Line 2 - Value of c is ", c )

c=a*b

print ("Line 3 - Value of c is ", c )

c=a/b

print( "Line 4 - Value of c is ", c )

c=a%b

print( "Line 5 - Value of c is ", c)


a=2

b=3

c = a**b

print ("Line 6 - Value of c is ", c)

a = 10

b=5

c = a//b

print ("Line 7 - Value of c is ", c)

Comparison operator
#!/usr/bin/python

a = 21

b = 10

c=0

if ( a == b ):

print ("Line 1 - a is equal to b")

else:

print( "Line 1 - a is not equal to b")

if ( a != b ):

print( "Line 2 - a is not equal to b")

else:

print( "Line 2 - a is equal to b")


if ( a <> b ):

print ("Line 3 - a is not equal to b")

else:

print( "Line 3 - a is equal to b")

if ( a < b ):

print ("Line 4 - a is less than b" )

else:

print( "Line 4 - a is not less than b")

if ( a > b ):

print( "Line 5 - a is greater than b")

else:

print( "Line 5 - a is not greater than b")

a = 5;

b = 20;

if ( a <= b ):

print ( "Line 6 - a is either less than or equal to b")

else:

print ( "Line 6 - a is neither less than nor equal to b")

if ( b >= a ):

print ( "Line 7 - b is either greater than or equal to b")

else:

print( "Line 7 - b is neither greater than nor equal to b")

Assignment Operator
#!/usr/bin/python

a = 21

b = 10

c=0

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)


Bitwise operator

#!/usr/bin/python

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)

Membership opetator
#!/usr/bin/python
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
print( "Line 1 - a is available in the given list")
else:
print( "Line 1 - a is not available in the given list")

if ( b not in list ):
print( "Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")

a=2
if ( a in list ):
print( "Line 3 - a is available in the given list")
else:
print( "Line 3 - a is not available in the given list")

Identity Operator

#!/usr/bin/python

a = 20
b = 20
if ( a is b ):
print ("Line 1 - a and b have same identity")
else:
print ("Line 1 - a and b do not have same identity")

if ( id(a) == id(b) ):
print ("Line 2 - a and b have same identity")
else:
print( "Line 2 - a and b do not have same identity")

b = 30
if ( a is b ):
print( "Line 3 - a and b have same identity")
else:
print( "Line 3 - a and b do not have same identity")

if ( a is not b ):
print( "Line 4 - a and b do not have same identity")
else:
print( "Line 4 - a and b have same identity")

When you execute the above program it produces the following result −
Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity
Operator Precedence

#!/usr/bin/python

a = 20

b = 10

c = 15

d=5

e=0

e = (a + b) * c / d #( 30 * 15 ) / 5

print( "Value of (a + b) * c / d is ", e)

e = ((a + b) * c) / d # (30 * 15 ) / 5

print( "Value of ((a + b) * c) / d is ", e)

e = (a + b) * (c / d); # (30) * (15/5)

print ("Value of (a + b) * (c / d) is ", e)

e = a + (b * c) / d; # 20 + (150/5)

print ("Value of a + (b * c) / d is ", e)

When you execute the above program, it produces the following result −

Value of (a + b) * c / d is 90

Value of ((a + b) * c) / d is 90

Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50

You might also like