You are on page 1of 8

EXPERIMENT 1

TITLE : Introduction to basic Python programming


OBJECTIVE : To understand basic Python programming

1. Write Python code to assign various numbers to variables and perform


different mathematical operation.
a = int(input("Enter first number : "))
b = int(input("Enter second number : "))
print("Summation is :", a+b)
print("Subtraction is :", a-b)
print("Multiplication is :", a*b)
print("Division is :", a/b)
print("Floor division is :", a//b)
print("Exponent is :", a^b)

Enter first number : 9


Enter second number : 3
Summation is : 12
Subtraction is : 6
Multiplication is : 27
Division is : 3.0
Floor division is : 3
Exponent is : 10

2. Write Python code to perform concatenation operation on two strings.


a = input("Enter first string : ")
b = input("Enter second string : ")

print("Concatenated string is :", a+b)

Enter first string : Hello


Enter second string : World
Concatenated string is : Hello World

3. Take user defined 10 numbers. Make a list out of this. Arrange them in
ascending and descending order.
lst1 = []

print("Enter 10 numbers : ")


for i in range(10) :
  lst1.append(int(input()))
print(lst1)

#sorting
for i in range(len(lst1)) :
  for j in range(i+1, len(lst1)) :
    if lst1[i] > lst1[j] :
      lst1[i], lst1[j] = lst1[j], lst1[i]

print("Ascending Order :", lst1)


print("Descending Order :", list(reversed(lst1)))

Enter 10 numbers :
10
0
4
7
21
23
67
54
34
2
[10, 0, 4, 7, 21, 23, 67, 54, 34, 2]
Ascending Order : [0, 2, 4, 7, 10, 21, 23, 34, 54, 67]
Descending Order : [67, 54, 34, 23, 21, 10, 7, 4, 2, 0]

4. Take another list of 5 numbers and insert the numbers to the previous list at
their proper position. (Insertion sort)
lst1 = []
lst2 = []

print("Enter 10 numbers : ")


for i in range(10) :
  lst1.append(int(input()))
print(lst1)

#sorting
for i in range(len(lst1)) :
  for j in range(i+1, len(lst1)) :
    if lst1[i] > lst1[j] :
      lst1[i], lst1[j] = lst1[j], lst1[i]

#insertion
print("Enter more 5 numbers : ")
for i in range(5) :
    lst2.append(int(input()))
print(lst2)

for i in range(len(lst2)) :
    for j in range(len(lst1)) :
        if lst2[i] <= lst1[j] and lst2[i] > lst1[j-1] :
            lst1.insert(j, lst2[i])
print(lst1)

Enter 10 numbers :
10
0
21
24
31
6
3
9
12
16
[10, 0, 21, 24, 31, 6, 3, 9, 12, 16]
Enter more 5 numbers :
7
8
15
2
1
[7, 8, 15, 2, 1]
[0, 1, 2, 3, 6, 7, 8, 9, 10, 12, 15, 16, 21, 24, 31]

5. Write a python code to implement Selection Sort, Quick Sort and Merge Sort.
Selection Sort :-
a = list(map(int, input("Enter the numbers : ").split()))

for i in range(len(a)) :
    temp = a[i]
    temp_index = i
    for j in range(i+1, len(a)) :
        if temp > a[j] :
            temp = a[j]
            temp_index = j
    a[i],a[temp_index] = temp, a[i]

print(a)

Enter the numbers : 10 9 2 5 13 2 6 7


[2, 2, 5, 6, 7, 9, 10, 13]
Quick Sort :-
def partition(lst, low, high):
  pivot = lst[high]
  i = low - 1
  for j in range(low, high):
    if lst[j] <= pivot:
      i = i + 1
      lst[i], lst[j] = lst[j], lst[i]

  lst[i + 1], lst[high] = lst[high], lst[i + 1]

  return i + 1

def quickSort(lst, low, high):
  if low < high:
    pi = partition(lst, low, high)
    quickSort(lst, low, pi - 1)
    quickSort(lst, pi + 1, high)

data = list(map(int, input("Enter the numbers : ").split()))
print(data)
quickSort(data, 0, len(data) - 1)

print("Sorted Array in Ascending Order : ")
print(data)

Enter the numbers : 5 1 13 0 9 45 21


[5, 1, 13, 0, 9, 45, 21]
Sorted Array in Ascending Order :
[0, 1, 5, 9, 13, 21, 45]

Merge Sort :-
def merge(lst, l, m, r):
  n1 = m - l + 1
  n2 = r - m

  L = [0] * (n1)
  R = [0] * (n2)

  for i in range(0, n1):
    L[i] = lst[l + i]

  for j in range(0, n2):
    R[j] = lst[m + 1 + j]

  i = 0  
  j = 0 
  k = l 
  while i < n1 and j < n2:
    if L[i] <= R[j]:
      lst[k] = L[i]
      i += 1
    else:
      lst[k] = R[j]
      j += 1
    k += 1

  while i < n1:
    lst[k] = L[i]
    i += 1
    k += 1

  while j < n2:
    lst[k] = R[j]
    j += 1
    k += 1

def mergeSort(lst, l, r):
  if l < r:
    m = l+(r-l)//2

    mergeSort(lst, l, m)
    mergeSort(lst, m+1, r)
    merge(lst, l, m, r)

lst = list(map(int, input("Enter the numbers : ").split()))
n = len(lst)
print("Given List is")
print(lst)

mergeSort(lst, 0, n-1)
print("\n\nSorted List is")
print(lst)

Enter the numbers : 3 17 2 0 23 5 6


Given List is
[3, 17, 2, 0, 23, 5, 6]

Sorted List is
[0, 2, 3, 5, 6, 17, 23]

6. Write a Python program to find out GCD.


a = int(input("Enter first number : "))
b = int(input("Enter second number : "))

while b:
    a, b = b, a % b

print(a)

Enter first number : 24


Enter second number : 12
12
7. i) Take two Binary String and do bitwise and, or, xor operations.
a = list(map(int, input("Enter first binary string : ").split(
)))
b = list(map(int, input("Enter first binary string : ").split(
)))
c = []

def bit_calc(a,b,operation) :
  if operation == "and" :
    for i in range(max(len(a), len(b))) :
      if a[i]==b[i] and a[i]==1 :
        c.append(int(1))
      else :
        c.append(int(0))
    print("And operation is :", c)
  
  elif operation == "or" :
      for i in range(max(len(a), len(b))) :
        if a[i]==b[i] and a[i]==0 :
          c.append(int(0))
        else :
          c.append(int(1))
      print("Or operation is :", c)

  elif operation == "xor" :
      for i in range(max(len(a), len(b))) :
        if a[i]==b[i] :
          c.append(int(0))
        else :
          c.append(int(1))
      print("Xor operation is :", c)

print("List a is :",a)
print("List b is :",b)
oper = input("Enter operation to perform : ")
bit_calc(a,b,oper)

Enter first binary string : 1 0 0 1 0


Enter first binary string : 1 1 0 0 1
List a is : [1, 0, 0, 1, 0]
List b is : [1, 1, 0, 0, 1]
Enter operation to perform : or
Or operation is : [1, 1, 0, 1, 1]

ii) Do binary addition, subtraction, multiplication, division operation.


a = list(map(int, input("Enter first binary string : ").split(
)))
b = list(map(int, input("Enter first binary string : ").split(
)))
c = []

def add_op(a,b) :
  carry = 0
  for i in range(max(len(a),len(b))-1, -1, -1) :
    sum = a[i] + b[i] + carry
    if sum>1 :
      sum = 0
      carry = 1
    c.insert(0,sum)  
  print("Addition is :", c)

def sub_op(a,b) :
  borrow = 0
  for i in range(max(len(a),len(b))-1, -1, -1) :
    sub = a[i] + borrow - b[i]
    if sub<0 :
      sub  += 2
      borrrow = 1
    c.insert(0,sub)  
  print("Subtraction is :", c)

def mul_op(a,b) :
  if len(a)>len(b) :
    b = (len(a)-len(b)) + b
  else:
    a = (len(b)-len(a)) + a

  ans = len(a)
  for i in range(len(a)-1,-1,-1):
    if b[i]==0:
      temp = len(a) + (len(a)-1-i)
      temp_ans = ans
      ans = add_op(temp,temp_ans)
    else:
      temp = a + (len(a)-1-i)
      temp_ans = ans
      ans = add_op(temp,temp_ans)

  print("Multiplication is :", ans)

def div_op(dividend, divisor) :
  def binary_compare(a, b):
    """
    Compares two binary numbers a and b and returns:
    1 if a > b
    0 if a == b
    -1 if a < b
    """
    diff = len(a) - len(b)
    if diff > 0:
        b = diff + b
    elif diff < 0:
        a = (-diff) + a

    for i in range(len(a)):
        if a[i] > b[i]:
            return 1
        elif a[i] < b[i]:
            return -1
    return 0
  quotient = []
  remainder = []
  for i in range(len(dividend)):
    remainder += dividend[i]
    if binary_compare(remainder, divisor) >= 0:
        remainder = sub_op(remainder, divisor)
        quotient.append(1)
    else:
        quotient.append(0)
  #quotient = quotient.lstrip('0')
  if quotient == []:
      quotient.append(0)
print(“Division quotient is :”,quotient,”and remainder
is :”, remainder)

print("List a is :",a)
print("List b is :",b)
add_op(a,b)
sub_op(a,b)
mul_op(a,b)
div_op(a,b)

Enter first binary string : 1 0 0 1 0


Enter first binary string : 1 0 1 0
List a is : [1, 0, 0, 1, 0]
List b is : [1, 0, 1, 0]
Addition is : [1 ,1, 1, 0, 0]
Subtraction is : [0, 1, 0, 0, 0]
Multiplication is : [1, 0, 1, 1, 0, 1, 0, 0]
Division quotient is : [1] and remainder is : [1, 0, 0]

You might also like