Basics of Python: Part I


Bikash Santra and Avisek Gupta

Indian Statistical Institute, Kolkata


1. Printing and Commenting

In [1]:
print('Hey There!')
Hey There!
In [2]:
# This is a comment

'''
This is 
a multi-line 
comment
'''
Out[2]:
'\nThis is \na multi-line \ncomment\n'

2. Data types in python

a) Strings
b) Integers
c) Floats
d) Complex Numbers
e) Boolean

In [3]:
# 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))
This is a string
This is a string too
This is a string three
This is a str'ing
<class 'str'>
In [4]:
# 2. Integers

i = 5
print(i)
print(type(i))
5
<class 'int'>
In [5]:
# 3. Floats

f = 4.3
print(f)
print(type(f))
4.3
<class 'float'>
In [6]:
# 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)
(2+3j)
(4+5j)
<class 'complex'> <class 'complex'>


2.0
3.0
In [7]:
# 5. Boolean

b = True
c = False
print(b)
print(c)
print(type(b))
print(type(c))
True
False
<class 'bool'>
<class 'bool'>

3. Input from a user

In [8]:
# input() for Python3 
# raw_input() for Python2

ip = input('Enter an integer:')
print(type(ip))
Enter an integer:5
<class 'str'>

4. Data type conversion

In [9]:
# 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))
5 <class 'int'>
5.0 <class 'float'>
Enter an integer:10
10 <class 'int'>
In [10]:
# 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)))
10 <class 'int'>
10 <class 'str'>
0.5 <class 'float'>
0.5 <class 'str'>
(100+2j) <class 'complex'>
In [11]:
# Implicit type-conversions

b1 = True
b2 = False
print(b1 + b2 + b1 + b1)
3
In [12]:
# Zero is False, non-zeros values are True

print(bool(0))
print(bool(100))
print(bool(-100))
False
True
True
In [13]:
# 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))
This 5 is a 6
This 5 is a 6
This 5 is a 6

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.

In [14]:
i = int(input('Enter an integer:'))
f = float(input('Enter a float:'))

s = i + f

print(s, type(s))
Enter an integer:20
Enter a float:4.5
24.5 <class 'float'>

5. Operators in python

a) Arithmetic operators
b) Assignments operators
c) Relational operators
d) Logical operators
e) Bitwise operators
f) Membership operators
g) Identity operators

5.1 Arithmetic operators

In [15]:
# 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)
Add 7.5
Subtract 1.5
Multiply 13.5
Division 1.5
Remainder (Division) 1
Floor Division 1.0


Exponentiation 20.25
Exponentiation 2.1213203435596424
Exponentiation 0.04938271604938271

5.2 Assignment operators

In [16]:
# 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)
16
10
60
10.0
4.0
1
1

5.3 Relational operators

In [17]:
# Relational Operators

n1 = 10
n2 = 5

print(n1 == n2)
print(n1 != n2)
print(n1 > n2)
print(n1 < n2)
print(n1 >= n2)
print(n1 <= n2)
False
True
True
False
True
False

5.4 Logical operators

In [18]:
# 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)
False
False
True
0
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

5.5 Bitwise operators

In [19]:
# 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

5.6 Membership operators

In [20]:
# Membership Operators

print('str' in 'string')
print('not' in 'convenient')
print('not' not in 'convenient')
True
False
True

5.7 Identity operators

In [21]:
# 7. Identity Operators

string1 = 'abcd'
string2 = 'efgh'
print(string1 is string2)
False