8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
Python Exercise for beginner and itermediate ( Interview
Quesion Answer )
****************************************************************************
Exercise 1: Write a Program to extract each digit from an
integer in the reverse order. For example, If the given int is
7536, the output shall be “6 3 5 7“, with a space separating
the digits.
In [1]:
1 k=""
2 n=input("enter the number: ")
3 y=n[::-1]
4 Data=k.join(y)
5 print(Data)
enter the number: 2009
9002
Excercise 2: Write a program that will tell whether the given
number is divisible by 3 & 6.
In [2]:
1 num = int(input("enter the number"))
2
3 if num%3==0 and num%6==0:
4 print("the number is divisible by 3 and 6")
5 else:
6 print("the number is not divisible by 3 and 6")
7
8
enter the number85
the number is not divisible by 3 and 6
Exercise 3: Display float number with 2 decimal places using
print()
In [3]:
1 num=78.87107
2 num=float(round(num,2))
3 print(num)
4
78.87
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 1/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
Excercise 4:Print all factors of a given number provided by
the user.
In [4]:
1 n=int(input("enter the number: "))
2 for i in range(1,n+1):
3 if n%i==0:
4 print(i,end=" ")
enter the number: 98
1 2 7 14 49 98
Exercise 5: Accept a list of 5 float numbers as an input from
the user
In [5]:
1 lst=[]
2 while len(lst)<5:
3
4 n=float(input("enter the number: "))
5 lst.append(n)
6 print(lst)
enter the number: 525
[525.0]
enter the number: 25
[525.0, 25.0]
enter the number: 854
[525.0, 25.0, 854.0]
enter the number: 259
[525.0, 25.0, 854.0, 259.0]
enter the number: 5001
[525.0, 25.0, 854.0, 259.0, 5001.0]
Exercise 6: Accept any three string from one input() call
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 2/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
In [6]:
1 df = input("write three string: ")
2
3 df_split = df.split()
4
5 print(df_split)
6
7 A=df_split[0]
8 B=df_split[1]
9 C=df_split[2]
10
11 print(A)
12 print(B)
13 print(C)
14
write three string: Amar Akbar Anthony
['Amar', 'Akbar', 'Anthony']
Amar
Akbar
Anthony
Exercise 7:
In [7]:
1 totalMoney = 1000
2 quantity = 3
3 price = 450
4
5 Expected_Output = print("i have {} dollars so i can buy {} football for {} dollars".format(tota
i have 1000 dollars so i can buy 3 football for 450 dollars
Excercise 8:Write a program to find the simple interest when
the value of principle,rate of interest and time period is
given.
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 3/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
In [8]:
1 Principal_amount =float(input("principle amount: "))
2
3 Rate_of_interest =float(input("Rate of interest: "))
4
5 #time period in months
6 Time_period_months =float(input("for the time period: "))
7
8 simple_interest = (Principal_amount * Rate_of_interest * Time_period_months)/100
9
10
11 print("Ineterst need to be paid :", simple_interest)
12
13
principle amount: 2500
Rate of interest: 5
for the time period: 10
Ineterst need to be paid : 1250.0
Excercise 9: Write a program to find the volume of the
cylinder. Also find the total cost when the cost of 1litre milk
is 40Rs.
In [9]:
1 #Mathematical formula to calculate volume of cylinder = pie r**2*h
2
3 # # pie = 3.142
4 # r**2 = Radius
5 # h = height
6
7 #convert volumn of cylinder into litre
8
9 #litre = volumn of cylinder/1000 #this much milk can carry a cylider.
10
11
12 radius=float(input("enter the radius of cylinder in cm "))
13 height=float(input("enter the height of cylinder in cm "))
14
15 volume_of_cylinder = 3.142 *(radius**2)*height
16
17 litre_in_cylinder = volume_of_cylinder/1000
18
19 total_cost = 40*litre_in_cylinder
20
21
22 print("Volume of cylinder will be ",volume_of_cylinder)
23 print("How much milk we can carry in this cylinder ",litre_in_cylinder, 'ltr')
24 print("this cost of that milk will be",total_cost)
25
26
27
28
29
enter the radius of cylinder in cm 150
enter the height of cylinder in cm 50
Volume of cylinder will be 3534750.0
How much milk we can carry in this cylinder 3534.75 ltr
this cost of that milk will be 141390.0
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 4/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
Exercise 10: Check file is empty or not,Write a program to
check if the given file is empty or not
In [10]:
1 import os
2 size=os.stat(r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\test file.txt
3 if size==0:
4
5 print("file is empty")
6 else:
7 print("file is not empty")
8
9
file is not empty
Exercise 11: Read content from the file
In [11]:
1 df=r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\Saurabh.txt"
2 with open(df) as df_object:
3 content=df_object.read()
4 print(content)
I am Saurabh
I am from New Delhi
Exercise 12: Read the file line by line
In [12]:
1 df=r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\Saurabh.txt"
2 with open (df) as df_object:
3
4 for line in df_object:
5
6 print(line.rstrip()) # rstrip used to remove as trailing characters
I am Saurabh
I am from New Delhi
Exercise 13 : take a blank file and write a line "I live in
india"
In [13]:
1 df=r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\test file.txt"
2 with open(df,'w') as f:
3 f.write("i live in India")
4
5 #this code will take a saved blank sheet named "test file" and write a line "i live in India"
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 5/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
Exercise 14 : Write all content of a given file into a new file
by skipping line number 5
1 # Original test file2
2
3 i live in India
4 Ram
5 shyam
6 Rohit
7 Prem
8 Amar
9 Neena
10
11 output required
12
13 i live in India
14 Ram
15 shyam
16 Rohit
17 Amar
18 Neena
In [14]:
1 #read test file2
2
3 file_name = r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\test file2.txt
4 with open (file_name,'r') as fp:
5 lines=fp.readlines() #read all the lines from the file
6
7 #open new file in write mode
8 with open ("test file new.txt","w") as fp:
9 count=0
10 for line in lines:
11 if count==4:
12 count=+1
13 continue
14 else:
15 fp.write(line)
16 count+1
17
18
19
Output as below
Exercise 15 : Read line number 4 from the following file
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 6/7
8/18/23, 11:36 PM Python Exercise for beginner and intermediate - Jupyter Notebook
1 # Original test file2
2
3 i live in India
4 Ram
5 shyam
6 Rohit
7 Prem
8 Amar
9 Neena
In [15]:
1 df=r"C:\Users\bhavi\Desktop\Data Science\Interview help\Python Linkedin\test file2.txt"
2 with open(df,"r") as fp:
3 lines=fp.readlines()
4 print(lines[3])
Rohit
In [ ]:
In [ ]:
In [ ]:
In [ ]:
localhost:8888/notebooks/Data scientist/Stat and ML/Python Exercise - Linkedin and Youtube Practice/Python Exercise for beginner and intermed… 7/7