
Bikash Santra and Avisek Gupta
Indian Statistical Institute, Kolkata
print('Hey There!')
# This is a comment
'''
This is
a multi-line
comment
'''
a) Strings
b) Integers
c) Floats
d) Complex Numbers
e) Boolean
# 1. Strings:
print('''This is a string''')
print('This is a string too')
print("This is a string three")
s = "This is a str'ing"
print(s)
print(type(s))
# 2. Integers
i = 5
print(i)
print(type(i))
# 3. Floats
f = 4.3
print(f)
print(type(f))
# 4. Complex Numbers
c1 = 2 + 3j
c2 = complex(4,5)
print(c1)
print(c2)
print(type(c1), type(c2))
print('\n')
print(c1.real)
print(c1.imag)
# 5. Boolean
b = True
c = False
print(b)
print(c)
print(type(b))
print(type(c))
# input() for Python3
# raw_input() for Python2
ip = input('Enter an integer:')
print(type(ip))
# converting to integers
print(int(ip), type(int(ip)))
# converting to floats
print(float(ip), type(float(ip)))
# type conversion and input in one line
ip2 = int(input('Enter an integer:'))
print(ip2, type(ip2))
# conversions to strings
i = 10
print(i, type(i))
print(str(i), type(str(i)))
f = 0.5
print(f, type(f))
print(str(f), type(str(f)))
s3 = '100+2j'
print(complex(s3), type(complex(s3)))
# Implicit type-conversions
b1 = True
b2 = False
print(b1 + b2 + b1 + b1)
# Zero is False, non-zeros values are True
print(bool(0))
print(bool(100))
print(bool(-100))
# Some ways to print
a = 5
b = 6
print('This',a,'is a',b)
print('This {} is a {}'.format(a , b))
print('This %d is a %d' %(a,b))
Exercise 1: Take as input an integer and a float. Print the integer and the float. Also print their sum along with the type of the sum.
i = int(input('Enter an integer:'))
f = float(input('Enter a float:'))
s = i + f
print(s, type(s))
a) Arithmetic operators
b) Assignments operators
c) Relational operators
d) Logical operators
e) Bitwise operators
f) Membership operators
g) Identity operators
# Arithmetic Operators
n1 = 4.5
n2 = 3
print('Add', n1 + n2)
print('Subtract',n1 - n2)
print('Multiply',n1 * n2)
print('Division',n1 / n2)
print('Remainder (Division)',4 % n2)
print('Floor Division',n1 // n2)
print('\n')
print('Exponentiation',n1 ** 2)
print('Exponentiation',n1 ** 0.5)
print('Exponentiation',n1 ** -2)
# Assignment Operators
n3 = 10
n4 = 6
n3, n4 = 10, 6
n3 += n4 # n3 = n3 + n4
print(n3)
n3 -= n4 # n3 = n3 - n4
print(n3)
n3 *= n4 # n3 = n3 * n4
print(n3)
n3 /= n4 # n3 = n3 / n4
print(n3)
n3 %= n4 # n3 = n3 % n4
print(n3)
n3 = 10
n3 //= n4 # n3 = n3 // n4
print(n3)
n3 **= n4 # n3 = n3 ** n4
print(n3)
# Relational Operators
n1 = 10
n2 = 5
print(n1 == n2)
print(n1 != n2)
print(n1 > n2)
print(n1 < n2)
print(n1 >= n2)
print(n1 <= n2)
# Logical Operators
b1 = True
b2 = False
print(not b1)
print(b1 and b2)
print(b1 or b2)
print(0 and 1)
print(0 or 1)
Exercise 2: Take four numbers as input. Display whether the sum of the first two numbers is greater than or equal to the sum of the latter two numbers
# Bitwise Operators
i1 = 15 # 1111
i2 = 6 # 0110
print(i1 & i2) # and
print(i1 | i2) # or
print(i1 ^ i2) # xor
print(~i1) # one's complement
print(i1 << 2) # left shift
print(i1 >> 2) # right shift
# Membership Operators
print('str' in 'string')
print('not' in 'convenient')
print('not' not in 'convenient')
# 7. Identity Operators
string1 = 'abcd'
string2 = 'efgh'
print(string1 is string2)