Question Bank For Midterm - Jupyter Notebook
Question Bank For Midterm - Jupyter Notebook
Write a Python program which accepts a sequence of comma-separated numbers from user and
generate a list and a tuple with those numbers.
In [ ]:
In [ ]:
Write a Python program to get the difference between a given number and 17, if the number is greater
than 17 return double the absolute difference.
In [ ]:
def difference(n):
if n <= 17:
return 17 - n
else:
return (n - 17) * 2
print(difference(2))
print(difference(19))
Write a Python program to test whether a number is within 100 of 1000 or 2000.
In [ ]:
def near_thousand(n):
return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))
print(near_thousand(1200))
print(near_thousand(900))
print(near_thousand(800))
print(near_thousand(2200))
Calculate the sum of three given numbers, if the values are equal then return thrice of their sum
In [ ]:
sum = x + y + z
if x == y == z:
sum = sum * 3
return sum
print(sum_thrice(1, 2, 3))
print(sum_thrice(3, 3, 3))
Get a new string from a given string where 'Is' has been added to the front. If the given string already
begins with 'Is' then return the string unchanged
In [ ]:
def new_string(str):
if len(str) >= 2 and str[:2] == "Is":
return str
return "Is" + str
print(new_string("Test"))
print(new_string("IsNotTest"))
Write a Python program to find whether a given number (accept from the user) is even or odd, print out
an appropriate message to the user.
In [ ]:
Write a Python program to concatenate all elements in a list into a string and return it.
In [ ]:
def concatenate_list_data(list):
result= ''
for element in list:
result += str(element)
return result
print(concatenate_list_data([5, 6, 7, 8]))
Write a Python program to compute the greatest common divisor (GCD) of two positive integers.
In [ ]:
if x % y == 0:
return y
print(gcd(10, 12))
print(gcd(100, 100))
Write a Python program to get the least common multiple (LCM) of two positive integers.
In [ ]:
Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will
return 20.
In [ ]:
print(sum(11, 9))
print(sum(12, 2))
print(sum(18, 12))
Write a Python program which will return true if the two given integer values are equal or their sum or
difference is 5.
In [ ]:
print(test_number5(7, 2))
print(test_number5(9, 2))
print(test_number5(2, 2))
Write a Python program to take the user information for n students details like name, age, address and
print them on different lines.
In [ ]:
def personal_details():
name, age = "Alok", 25
address = "Mumbai, Maharashtra, India"
print("Name: {}\nAge: {}\nAddress: {}".format(name, age, address))
personal_details()
Write a Python program to find the sum of array (hint take a list as array).
In [ ]:
def array_summer(arr):
total = 0
for item in arr:
# shorthand of total = total + item
total += item
return total
# Test input
print(array_summer([1, 2, 3, 3, 7]))
Write a Python program to imitate login activity of a user also do its validation. (hint:-If user enters the
wrong userid or password it should provide a message)
In [ ]:
import re
password = input("Input Password")
# password = "R@m@_f0rtu9e$"
flag = 0
while True:
if (len(password)<8):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[_@$]", password):
flag = -1
break
elif re.search("\s", password):
flag = -1
break
else:
flag = 0
print("Valid Password")
break
if flag ==-1:
print("Not a Valid Password")
Write a Python program to imitate a shopping transaction where user purchase 5 notebooks of 20 rs
each and 2 pens of 5 rs each. Show the total amount to be payable to the shopkeeper.
In [ ]:
cost_of_notebook = 20
cost_of_pen = 5
Accept the age of the person and print an appropriate message as per the table given alongside:
Age Message
In [ ]:
Accept the purchases made by a customer and calculate and print the discount payable by him. You are
given that a discount of 10% is given on purchases greater than Rs. 3000/- and no discount is given for
purchases below that.
In [ ]:
if amt<3000:
print("No Discount Applicable")
elif amt>=3000:
print("Amount payable after 10% discount is", amt-amt*0.1)
print("Total discount applied =", amt*0.1)
Accept the gross salary of an employee. Calculate and print the tax based on the given criteria.
2,00,001-5,00,000 10%
In [ ]:
elif sal>500000:
print("Salary after 20% tax =", sal-sal*0.2, "Total Tax Deducted =", sal*0.2)
>=90 5000
>=80 1000
Otherwise Nil
In [ ]:
Accept the quantity of an item purchased and its price. Calculate the amount of purchase. If the amount
exceeds Rs.5000, a discount of 20% is given otherwise the discount rate is 10%. Print the input values,
the discount rate and amount, net amount to be paid by the customer.
In [ ]:
if (qty*prc)>=5000:
print("Quantity Purchased =", qty, "Amount Paid =", prc, "Discount = 20%", "Net Amount
elif (qty*prc)<5000:
print("Quantity Purchased =", qty, "Amount Paid =", prc, "Discount = 10%", "Net Amount
Accept the marks obtained by the 5 students for subjects of Python, Statistics, Machine Learning, Deep
Learning, Big-Data also all marks are out of 100. Find the sum and percentage of the for all students
and display the name and marks of the first rank holder.
In [ ]:
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle
elements.
In [ ]:
Given an array of ints, return a new array length 2 containing the first and last elements from the
original array. The original array will be length 1 or more.
In [ ]:
def make_ends(nums):
return [nums[0], nums[-1]]
In [ ]:
make_ends(([1, 2, 3]))
Write a Python program to find the Mean, Median and Mode of three user entered values
In [47]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-47-198c3a2719b5> in <module>
6 mean = get_sum / len(n)
7
----> 8 print("Mean / Average is: " + str(mean))
7###### Write a Python program to check the validity of password input by users. Go to the editor Validation:
In [ ]:
In [8]:
result_str="";
for row in range(0,7):
for column in range(0,7):
if (((column == 1 or column == 5) and row != 0) or ((row == 0 or row == 3) and (col
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
***
* *
* *
*****
* *
* *
* *
In [ ]:
In [13]:
def print_pattern(n):
# Outer for loop for number of lines(rows)
for i in range(n):
print()
if num > 7:
print_pattern(num)
else:
print("Enter a size minimum of 8")
In [9]:
*****
*
*
*
*
*
*****
In [7]:
rslt=""
for row in range (7):
for col in range(7):
if (col==1 or ((row==0 or row==6) and (col>1 and col<5)) or (col==5 and row!= 0 and
rslt= rslt+"*"
else:
rslt=rslt+" "
rslt = rslt+"\n"
print(rslt)
****
* *
* *
* *
* *
* *
****
In [11]:
result_str="";
for row in range(0,7):
for column in range(0,7):
if (column == 1 or ((row == 0 or row == 6) and (column > 1 and column < 6)) or (row
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
*****
*
*
****
*
*
*****
In [12]:
str="";
for Row in range(0,7):
for Col in range(0,7):
if (Col == 1 or (Row == 0 and Col > 1 and Col < 6) or (Row == 3 and Col > 1 and Col
str=str+"*"
else:
str=str+" "
str=str+"\n"
print(str)
*****
*
*
****
*
*
*
In [15]:
result_str="";
for row in range(0,7):
for column in range(0,7):
if ((column == 1 and row != 0 and row != 6) or ((row == 0 or row == 6) and column >
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str);
***
* *
*
* ***
* *
* *
***
In [16]:
str="";
for Row in range(0,7):
for Col in range(0,7):
if ((Col == 1 or Col == 5) or (Row == 3 and Col > 1 and Col < 6)):
str=str+"*"
else:
str=str+" "
str=str+"\n"
print(str)
* *
* *
* *
*****
* *
* *
* *
In [43]:
*****
*
*
*
*
*
*****
In [20]:
*****
*
*
*
*
*
***
In [24]:
str=""
j = 5
i = 0
for Row in range(0,7):
for Col in range(0,7):
if (Col == 1 or ((Row == Col + 1) and Col != 0)):
str=str+"*"
elif (Row == i and Col == j):
str=str+"*"
i=i+1
j=j-1
else:
str=str+" "
str=str+"\n"
print(str)
* *
* *
* *
**
* *
* *
* *
In [23]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (column == 1 or (row == 6 and column != 0 and column < 6)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
*
*
*
*
*
*
*****
In [25]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (column == 1 or column == 5 or (row == 2 and (column == 2 or column == 4)) or (r
result_str=result_str+"* "
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
* *
* *
* * * *
* * *
* *
* *
* *
In [26]:
* *
** *
* * *
* * *
* **
* *
In [27]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (((column == 1 or column == 5) and row != 0 and row != 6) or ((row == 0 or row =
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
***
* *
* *
* *
* *
* *
***
In [28]:
result_str="";
for row in range(0,7):
for column in range(0,7):
if (column == 1 or ((row == 0 or row == 3) and column > 0 and column < 5) or ((colu
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
****
* *
* *
****
*
*
*
In [30]:
str=""
for Row in range(0,7):
for Col in range(0,7):
if ((Col == 1 and Row != 0 and Row != 6) or (Row == 0 and Col > 1 and Col < 5) or (
str=str+"*"
else:
str=str+" "
str=str+"\n"
print(str)
***
* *
* *
* *
* * *
* *
** *
In [31]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (column == 1 or ((row == 0 or row == 3) and column > 1 and column < 5) or (colum
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
****
* *
* *
****
* *
* *
* *
In [33]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (((row == 0 or row == 3 or row == 6) and column > 1 and column < 5) or (column =
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
****
*
*
***
*
*
****
In [34]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (column == 3 or (row == 0 and column > 0 and column <6)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
*****
*
*
*
*
*
*
In [35]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (((column == 1 or column == 5) and row != 6) or (row == 6 and column > 1 and col
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
* *
* *
* *
* *
* *
* *
***
In [36]:
str=""
for Row in range(0,7):
for Col in range(0,7):
if (((Col == 1 or Col == 5) and Row < 5) or (Row == 6 and Col == 3) or (Row == 5 an
str=str+"*"
else:
str=str+" "
str=str+"\n"
print(str)
* *
* *
* *
* *
* *
* *
*
In [37]:
i=0
j=3
for row in range (4):
for col in range (7):
if col==0 or col==6 or (col==5 and row==2) or (col==4 and row==1):
print("*", end="")
elif row==i and col==j:
print("*", end="")
i = i+1
j = j-1
else:
print(end=" ")
print()
* * *
* * * *
** **
* *
In [40]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (((column == 1 or column == 5) and (row > 4 or row < 2)) or row == column and co
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
* *
* *
* *
*
* *
* *
* *
In [41]:
str=""
for Row in range(0,7):
for Col in range(0,7):
if (((Col == 1 or Col == 5) and Row < 2) or Row == Col and Col > 0 and Col < 4 or (
str=str+"*"
else:
str=str+" "
str=str+"\n"
print(str)
* *
* *
* *
*
*
*
*
In [42]:
result_str=""
for row in range(0,7):
for column in range(0,7):
if (((row == 0 or row == 6) and column >= 0 and column <= 6) or row+column==6):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
*******
*
*
*
*
*
*******
In [49]:
In [ ]: