Skip to content

Commit cda0021

Browse files
Practice Question
1 parent e986954 commit cda0021

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1272
-0
lines changed

Diff for: 10_alphabets.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# printing first 10 aplhabets in list form
2+
x = []
3+
for i in range(97,107): # alphabets can be printed using ascii numbers
4+
i = chr(i)
5+
x.append(i)
6+
print(x)

Diff for: abbrevations.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# example The Jungle Book should be displaye as TJB
2+
def abs(str_1):
3+
str_2 = str_1.split(" ") # .split(" ") seprates the words at blank
4+
for i in str_2:
5+
print(i[0].upper(),end="")
6+
7+
8+
string_1 =input("enter the string: ")
9+
abs(string_1)

Diff for: add and sub fraction.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# making a class to add and subtract a fraction
2+
class Fraction:
3+
def __init__(self, a, b, c, d):
4+
self._a = a
5+
self._b = b
6+
self._c = c
7+
self._d = d
8+
9+
def sum(self):
10+
add = (self._a / self._b) + (self._c / self._d)
11+
print("The sum of numbers is: ", add)
12+
13+
def diff(self):
14+
sub = (self._a / self._b) - (self._c / self._d)
15+
print("the difference of the numbers is: ", sub)
16+
17+
18+
w = float(input("enter the numerator of first number"))
19+
x = float(input("enter the denominator of first number"))
20+
y = float(input("enter the numerator of second number"))
21+
z = float(input("enter the denominator of second number"))
22+
result = Fraction(w, x, y, z)
23+
result.sum()
24+
result.diff()

Diff for: add_ing_at_end.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# adding ing at the end if character is longer then 3, if ing already there then add ly
2+
def string(str_1):
3+
if len(str_1)<3: # len shows the length of the string
4+
return str_1
5+
elif len(str_1)>3:
6+
if str_1[len(str_1)-3:] != "ing":
7+
return str_1 + "ing"
8+
elif str_1[len(str_1)-3:] == "ing":
9+
return str_1 + "ly"
10+
11+
12+
line = input("enter the string: ")
13+
print(string(line))

Diff for: age.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# enter current year and your birthe year and code will display your age
2+
x = int(input("enter the current date, month and year resp \n"))
3+
y = int(input())
4+
z = int(input())
5+
a = int(input("enter the birth date, month and year resp \n"))
6+
b = int(input())
7+
c = int(input())
8+
age = z - c # current year minus birth year
9+
z = str(z)
10+
c = str(c)
11+
print("Date of birth is",str(a) +"/",str(b)+"/",str(c[2]),str(c[3]))
12+
print("Current date is",str(x) +"/",str(y)+"/",str(z[2]),str(z[3]))
13+
print("Age of person is",age)

Diff for: alphabetical.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# sorting the elements of a list
2+
x = []
3+
y = int(input("enter the number of elements: "))
4+
for i in range(y):
5+
z = str(input("enter any element: "))
6+
x.append(z)
7+
x.sort()
8+
print("The sorted order of the list is: ", x)

Diff for: alphbet_or_digit.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# determining whether the character entered is a digit or alphabet
2+
x = input("enter any character")
3+
if x.isdigit(): # checking for digit
4+
print("enter input is digit")
5+
elif x.isalpha(): # checking for alphabet
6+
print("input is alphabet")
7+
elif x.isspace(): # checking for space
8+
print("white space")
9+
else:
10+
print("wrong input")

Diff for: area.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Finding the area of a triangle using class
2+
import math
3+
4+
5+
class Triangle:
6+
def __init__(self, side1, side2, side3):
7+
self._side1 = side1
8+
self._side2 = side2
9+
self._side3 = side3
10+
self._semi_perimeter = 0
11+
12+
def semi(self):
13+
self._semi_perimeter = (self._side1 + self._side2 + self._side3)/2
14+
area = math.sqrt((self._semi_perimeter)*(self._semi_perimeter-self._side1)
15+
*(self._semi_perimeter-self._side2)*(self._semi_perimeter-self._side3))
16+
print("The area of the given triangle is", area)
17+
18+
19+
a = int(input("enter the first side: "))
20+
b = int(input("enter the second side: "))
21+
c = int(input("enter the third side: "))
22+
AREA = Triangle(a, b, c)
23+
AREA.semi()
24+

Diff for: area_triangle.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# program to find the area of a triangle
2+
import math # importing math module
3+
def area(x,y,z):
4+
s = (x + y + z)/2
5+
ar = math.sqrt(s*(s-x)*(s-y)*(s-z)) # this is the formula to find area
6+
return ar
7+
8+
9+
a = int(input("Enter a side: "))
10+
b = int(input("Enter a side: "))
11+
c = int(input("Enter a side: "))
12+
print(area(a,b,c))

Diff for: assertion error.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The numberes should be entered as asked unless display to correct mistake
2+
x = int(input("enter the larger number: "))
3+
y = int(input("enter the smaller number: "))
4+
try:
5+
if x > y:
6+
print("you entered the right numbers")
7+
else:
8+
raise AssertionError
9+
except AssertionError:
10+
print("Sorry you didn't entered the numbers correctly")

Diff for: average.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# finding the average of the numbers entered
2+
def sum(args, count):
3+
print(args/count)
4+
5+
6+
x = int(input("how many numbers you want?"))
7+
add = 0
8+
nums = 0
9+
for i in range(1, x+1):
10+
num = int(input("enter any number: "))
11+
nums += 1
12+
add += num
13+
sum(add,nums)

Diff for: average_odd_even.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# checking the aveage of sum of odd and even numbers
2+
even = 0
3+
odd = 0
4+
count_even = 0
5+
count_odd = 0
6+
while True:
7+
x = int(input("enter any number(press -1 to exit): "))
8+
if x > 0:
9+
if x%2==0: # includes all even numbers
10+
even += x
11+
count_even +=1
12+
else: # includes all odd numbers
13+
odd += x
14+
count_odd +=1
15+
16+
elif x==-1:
17+
print("The average of even numbers is: ",even/count_even)
18+
print("the average of odd numbers is: ", odd/count_odd)
19+
break

Diff for: binary.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# converting a decimal number into a binary number
2+
while True:
3+
decimal_number = int(input("enter any number(press 999 to exit): "))
4+
if decimal_number<0:
5+
continue
6+
elif decimal_number>0 and decimal_number!=999:
7+
i = 0
8+
binary_num = 0
9+
while (decimal_number!=0):
10+
remainder = decimal_number%2
11+
binary_num = binary_num + remainder*(10**i)
12+
decimal_number = int(decimal_number/2)
13+
i += 1
14+
print("binary number of is: ", binary_num)
15+
elif decimal_number==999:
16+
break
17+

Diff for: book.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# adding and displaying details of a book using class
2+
class Book:
3+
def __init__(self):
4+
self.title = ""
5+
self.author = ""
6+
self.publisher = ""
7+
self.ISBN = 0
8+
9+
def read(self):
10+
self.title = input("enter the title: ")
11+
self.author = input("Enter the author: ")
12+
self.publisher = input("Enter the publisher: ")
13+
self.ISBN = int(input("Enter the number: "))
14+
15+
def display(self):
16+
print("The title of the books is: ", self.title)
17+
print("The author of the book is: ", self.author)
18+
print("The publisher of the book is: ", self.publisher)
19+
print("The ISBN number of the book is: ", self.ISBN)
20+
21+
22+
z = Book()
23+
x = []
24+
while True:
25+
y = input("Do you want to enter in the details?(y/n): ")
26+
if y == "y":
27+
z.read()
28+
x.append(z)
29+
elif y == "n":
30+
for j in x:
31+
j.display()
32+
break

Diff for: cars.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Displaying details of a car
2+
class Cars:
3+
def __init__(self, colour, type, price):
4+
self._colour = colour
5+
self._type = type
6+
self._price = price
7+
8+
def display(self):
9+
print("The ", self._colour, " car is ", self._type, " type and costs Rs ", self._price)
10+
11+
12+
car1 = Cars("red", "convertible", "10 lakh")
13+
car2 = Cars("blue", "sedan", "6 lakh")
14+
car1.display()
15+
car2.display()

Diff for: complex.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# adding and subtracting 2 complex numbers using class
2+
class Complex:
3+
def __init__(self, real1, img1, real2, img2):
4+
self._real1 = real1
5+
self._img1 = img1
6+
self._real2 = real2
7+
self._img2 = img2
8+
9+
def sum(self):
10+
add_real = self._real1 + self._real2
11+
add_img = self._img1 + self._img2
12+
print("The sum of complex numbers is: ", add_real," + (",add_img, "i)")
13+
14+
def sub(self):
15+
sub_real = self._real1 - self._real2
16+
sub_img = self._img1 - self._img2
17+
print("The difference of complex numbers is: ", sub_real, " + (", sub_img, "i)")
18+
19+
20+
a = float(input("enter the real part of first number: "))
21+
b = float(input("enter the imaginary part of first number: "))
22+
c = float(input("enter the real part of second number: "))
23+
d = float(input("enter the imaginary part of second number: "))
24+
result = Complex(a, b, c, d)
25+
result.sum()
26+
result.sub()
27+

Diff for: concatenate.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# concatenating strings without directly adding two string
2+
def conc(str_1, str_2):
3+
x = " "
4+
for i in range(0,len(str_1)):
5+
x += str_1[i] # x becomes equal to str_1 after finishing loop
6+
for j in range(0,len(str_2)):
7+
x += str_2[j] # x further adds on str_2
8+
return x
9+
10+
11+
strn_1 = input("enter any first string: ")
12+
strn_2 = input("enter any second string: ")
13+
print(conc(strn_1,strn_2))

Diff for: consonants.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# print all the consonants present in all the words of the list
2+
x = []
3+
y = int(input("enters the number of terms: "))
4+
for i in range(y):
5+
z = input("enter the element: ")
6+
x.append(z)
7+
print("the entered list is: ", x)
8+
9+
for b in x:
10+
print()
11+
for c in range(0,len(b)):
12+
if b[c] not in ("aeiou"):
13+
print(b[c],end=",")
14+

Diff for: copying_from_list.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# there is a predefined list and a tuple, return the common elements in form of list
2+
x = ["1", "2", "abc", "def", "ghi", "4", "5"]
3+
y = ("abc", "2", "5", "def", 3)
4+
z = []
5+
for i in y:
6+
if i in x:
7+
z.append(i)
8+
print(z)

Diff for: count_a_value.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# counting number of times a value is present inside the list
2+
x = []
3+
y = int(input("enter the number of terms: "))
4+
for i in range(y):
5+
z = input("enter the element: ")
6+
x.append(z)
7+
y = set(x) # so that each item is checked once
8+
for i in y:
9+
if i in x:
10+
b = x.count(i)
11+
print(i,"is present ",b,"number of times")

Diff for: counting.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# counting the number of the uppercase and lowercase letters, press & to exit
2+
upper = 0
3+
lower = 0
4+
digit = 0
5+
while True:
6+
x = input("enter any character(press & to exit)")
7+
if x.isupper():
8+
upper = upper+1
9+
elif x.islower():
10+
lower = lower+1
11+
elif x.isdigit():
12+
digit += 1
13+
elif x=='&':
14+
print("lower case: ",lower)
15+
print("upper case: ",upper)
16+
print("digits: ",digit)
17+
break
18+

Diff for: delete_str_from_another.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# deleting a specified thing for the first thing
2+
def delete(str_1,str_2): # str_1 is the main string; str_2 is what need to be removed
3+
result=" "
4+
for i in str_1:
5+
if i in str_2:
6+
continue
7+
else:
8+
result += i
9+
return result
10+
11+
12+
string_1 = input("enter the string: ")
13+
string_2 = input("enter what you want to remove: ")
14+
print(delete(string_1,string_2))

Diff for: deleting_numbers.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# make a list from 1-20 and then delete numbers which are divisible by 5
2+
x = []
3+
for i in range(1,21):
4+
x.append(i)
5+
print("The numbers are: ",x)
6+
y = []
7+
for j in x:
8+
if int(j)%5==0:
9+
continue
10+
else:
11+
y.append(j)
12+
print("The new list is: ",y)

Diff for: dictionary(bill).py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# making a food bill for all the items in a dictionary
2+
price = {"apple": 50, "mango": 25, "banana": 30, "tomato": 40}
3+
sum = 0
4+
for i,j in price.items():
5+
print("price of " ,i, "is Rupees ",j)
6+
sum += int(j)
7+
print("The total price to be paid is Rupees ",sum)

Diff for: dictionary(birthday).py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# adding or verifying the birth date of a person using a dictionary
2+
birth = {"raju": "Jan 1, 2000", "baju":"Jan 4, 2000", "kaju":"Jan7, 2000"}
3+
x = str(input("enter the name you are looking for: "))
4+
5+
if x in birth.keys():
6+
print("The birth date of",x, "is: ",birth[x])
7+
8+
9+
elif x!= birth.keys():
10+
print("your searched name is not mentioned here, please provide the birth date")
11+
z = input("enter the date: ")
12+
birth[x] = z
13+
print("the new dict is: ",birth)
14+

Diff for: dictionary(menu).py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# show the price of the ordered dish (using dictionary)
2+
menu = {"burger":50, "noodles":80, "fries":30,"cake":100, "pizza":100}
3+
while True:
4+
x = str(input("enter what you want to eat: "))
5+
6+
if x in menu.keys():
7+
print("Your order of ",x,"will cost you Rupees ",menu[x])
8+
break
9+
else:
10+
print("SORRY THIS ITEM IS UNAVAILABLE, PLEASE RE-ENTER THE ORDER")
11+
continue

0 commit comments

Comments
 (0)