Bikash Santra
Indian Statistical Institute, Kolkata
# This is my first line of code
print('Hey There!')
Hey There!
# This is a comment
a) Strings
b) Integers
c) Floats
d) Complex Numbers
e) Boolean
# 1. Strings:
x = "'I am in ISI'"
y = 'is a string.'
print(x)
print(type(x))
'I am in ISI' <class 'str'>
print(x, ''' This is a string ''')
'I am in ISI' 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))
This is a string too This is a string three This is a str'ing <class 'str'>
# 2. Integers
i = -0.0
print(i)
print(type(i))
-0.0 <class 'float'>
# 3. Floats
f = 4.3
print(f)
print(type(f))
4.3 <class 'float'>
# 4. Complex Numbers
c1 = 2 + 3j
print(c1, type(c1))
(2+3j) <class 'complex'>
c2 = complex(4,5)
print(c2, type(c2))
(4+5j) <class 'complex'>
print(c1)
print(c2)
print(type(c1), type(c2))
(2+3j) (4+5j) <class 'complex'> <class 'complex'>
print('\n')
print(c1.real + c2.real)
print(c1.imag + c2.imag)
6.0 8.0
# 5. Boolean
Yes = True
No = False
b = Yes
c = No
print(b)
print(c)
print(type(b))
print(type(c))
print(b + c, type(b+c), type(b or c))
True False <class 'bool'> <class 'bool'> 1 <class 'int'> <class 'bool'>
# input() for Python3
# raw_input() for Python2
ip = input('Enter an integer:')
Enter an integer:12
Question: Take an input from the user and check it whether the input is an integer. If the input is not an integer, please prompt the user to write an integer. Prompt again and again until user writes the integer.
print(ip, type(ip))
12 <class 'str'>
# converting to integers
print(int(ip), type(int(ip)))
# converting to floats
print(float(ip), type(float(ip)))
12 <class 'int'> 12.0 <class 'float'>
# type conversion and input in one line
ip2 = int(input('Enter an integer:'))
print(ip2, type(ip2))
Enter an integer:12 12 <class 'int'>
# conversions to strings
i = 10
print(i, type(i))
print(str(i), type(str(i)))
10 <class 'int'> 10 <class 'str'>
f = 0.5
print(f, type(f))
print(str(f), type(str(f)))
0.5 <class 'float'> 0.5 <class 'str'>
s3 = '100+2j'
print(s3, type(s3), complex(s3), type(complex(s3)))
100+2j <class 'str'> (100+2j) <class 'complex'>
# Implicit type-conversions
b1 = True
b2 = False
print(b1 + b2 + b1 + b1)
3
# Zero is False, non-zeros values are True
print(bool(0))
print(bool(100))
print(bool(-100))
False True True
a= True
b= True
c = False
print(a, b, type(a), type(b), float(a), int(b), float(c), str(a))
True True <class 'bool'> <class 'bool'> 1.0 1 0.0 True
a= True
b= True
x = a + b
print(x,type(x))
2 <class 'int'>
# Some ways to print
a = 5.0
b = 6.0
print(a)
print(b)
print(a,b)
5.0 6.0 5.0 6.0
print('This', a, 'is a', b)
This 5.0 is a 6.0
print('This {} is a {}'.format(a , b))
print('The number {} is greater than the number {}.'.format(b , a))
This 5.0 is a 6.0 The number 6.0 is greater than the number 5.0.
# %d -> integer
# %f -> float
# %s -> string
# %b -> boolean (doubtful)
print('This %.4f is a %.4f' %(a,b))
print('This %f is a %f' %(a,b))
print('This %s is a %s' %(a,b))
This 5.0000 is a 6.0000 This 5.000000 is a 6.000000 This 5.0 is a 6.0
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:'))
Enter an integer:12 Enter a float:14
s = i + f
print(s, type(s))
26.0 <class 'float'>
x, y = input('Enter an integer:').split()
Enter an integer:12 14
print(x,y)
12 14
a) Arithmetic operators
b) Assignments operators
c) Relational operators
d) Logical operators
e) Bitwise operators
f) Membership operators
g) Identity operators
# Arithmetic Operators
n1 = 6.5
n2 = 4
print('Add', n1 + n2)
print('Subtract',n1 - n2)
print('Multiply',n1 * n2)
print('Division',n1 / n2)
Add 10.5 Subtract 2.5 Multiply 26.0 Division 1.625
sum_1 = n1 + n2
subtract = n1 - n2
print('Add', sum_1, subtract)
Add 10.5 2.5
print('Remainder (Division)',n1 % n2) # Remainder
print('Floor Division',n1 // n2) # Quotient
Remainder (Division) 2.5 Floor Division 1.0
print('Exponentiation',n2 ** 2)
print('\n')
print('Exponentiation',n2 ** 0.5)
print('\n')
print('Exponentiation',n2 ** -2)
Exponentiation 16 Exponentiation 2.0 Exponentiation 0.0625
# Assignment Operators
n3 = 10
n4 = 6
print(n3, n4)
10 6
n3, n4, n5, n6, n7, n8 = 6, 10, 4.5, 'ISI', True, 10.9
print(n3, n4, n5, n6, n7)
print(type(n3), type(n7))
6 10 4.5 ISI True <class 'int'> <class 'bool'>
n3 += n4 # n3 = n3 + n4
print(n3)
16
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)
6 60 6.0 6.0
n3 = 10
n3 //= n4 # n3 = n3 // n4
print(n3)
n3 **= n4 # n3 = n3 ** n4
print(n3)
1 1
# Relational Operators
n1 = 10
n2 = 10.5
n3 = complex(2,3)
n3= False
print(type(n1), type(n2))
<class 'int'> <class 'float'>
print(n1 == n3)
False
print(n1 != n3)
True
print(n1 > n3)
print(n1 < n3)
print(n1 >= n3)
print(n1 <= n3)
True False True False
# Logical Operators
b1 = True
b2 = False
print(b1, b2)
True False
b3 = not b2
print(not b1, b3)
False True
print('Input-1', 'Input-2', 'Output')
print('-------------------------')
print(b1, b2, b1 and b2)
print(b1 or b2)
Input-1 Input-2 Output ------------------------- True False False True
Practice: Prepare the truth tables for the OR and AND operations. Then you display the truth values of each operations in python.
print(True + False)
print(3 and 23)
print('\n')
print(0 or 6)
1 23 6
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
ap1 = float(input('Enter the Ist Integer:'))
ap2 = float(input('Enter the IInd Integer:'))
ap3 = float(input('Enter the IIIrd Integer:'))
ap4 = float(input('Enter the IVth Integer:'))
x = ap1 + ap2
y = ap3 + ap4
print(x >= y)
Enter the Ist Integer:12 Enter the IInd Integer:14 Enter the IIIrd Integer:16 Enter the IVth Integer:18 False
# 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
6 15 9 -16 60 3
# Membership Operators
print('str' in 'string')
True
print('not' in 'convenient')
print('not' not in 'convenient')
False True
# 7. Identity Operators
string1 = 'abcd'
string2 = 'abcd'
print(string1 is string2)
True