Python Revision Class 9

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Al LAB

computer and observe the output.


Try the following Python programs on
1: Program to convert the age in seconds.
into seconds
# this program converts your age HaPyhon 3J2 Shel
Window He
n=input('Enter your Name: ") File Edik Shel Debug Ogtions
Python 3.7.2 (tags/v3.7.2:9a3ffc0492. Dec 23
TMSC v.1916 32 bit (Intel)] on win32 2018,22:20:52)
a=input('Enter your Age in years:) Tvpe "help","copyright, "credts or license()" for more informe
tion.
s=int (a) >>>
RESTART: CAUsers/BRAINWAVEIAppData/L.ocaUProgramsp
ython/Python37-32/P1.py
asec= ((((s*365) *24) *60)*60) Enter your Name: Sam
Enter your Age in years: 15
print ('Hello',n, ) Hello Sam
Your Age in years= 15
Sam you are 473040000 seconds old
print ('Your Age in years =',a) >>>

print (n, 'you are' ,asec, 'seconds old')


2: Program to find the square root.
# Python Program to calculate the square root
num = float (input('Enter a number: '))
num sqrt = num ** 0.5 athon 372Sel
File Edk Shel Debug Opbons Window Help
print(The square root of ', num, "=", num sgrt) Python 3.7.2 (tagsv3.7.2:9a3fc0492, Dec 23 2018, 22:20:52)
[MSC v.1916 32 bit (Inte)] on win32
Type "help", "copyright, "credits or "license( for more infom
ation.
>>>

RESTART: CAUsers/BRAINWAVEJAppData/LocaVPrograms/P
In this program, we ask the user for a number and find the square ython/Python37-32/P1.py
Enter a umber: 4
root using the $* exponent operator. This program works for all The square root of 4.0 = 2.0
>»>|
positive real numbers.

3: Program tocalculate the area of triangle using base and height. Hint: The base is one side of the triangle and
the height is the measure of the tallest point on atriangle. Area=1/2(basexheight)
# To calculate the area of triangle using base and height
b=float (input( 'Enter the base in centime ters '))
h=float (input( 'Enter the height in centimeters '))
area=( (b*h) )/2
print( 'The base value of the triangle in centimeters= , b)
print ( 'The height value of the triangle in centimeters ', h)
print( The area of the triangle in centimeters = ,area)
# Python program to
swap two
xinput(' Enter
input( Enter
value of x: ) variablea provided by the user
value of y: ')
# areate a
temp x
temporary varlable and awap the
values t t hat bsbu

ython 372 (tagsv17.20a3fo0402. Deo 23 2018, 22.20:02)


(MRC v 1016 32 bt (ntel))on win32
Type "hel", "sopykght "eredits" or "hense(y' fos more intorrna
y temp RESTART CNsers/RAINWAVEJApoData/L06al/Piograms/
print ('The ython/Python37-32IP1.py
value of x after fnter vatue of : 10
Cnter value of y 15

value of y after swapping ,x)


The value of after
print ('The ' The value of y fter swpping
15
swapping 10

In this program, we use


swapping Y)
the
and latertemp in y. In this way,temp variable to temporarily hold the value of x. We
the values get exchanged. then put the value of yin x
5: Program toconvert Kilometers to Miles.
Program to convert kilometers into
miles
Tnput is provided by the user in Pe te Shelt Oeug Cyin ndw Mey
Python 3.7.2 (tagsiV3.7.2:9a3fc0492, Dec 23 2018, 22:20:52)

kilometers = kilometers (MSC v.1916 32 bit (Intel)) on win32


|Type "help", "copyrighr, "eredits or ticense( for more infome
float (input ('How many tion.

# conversion factor kilometers?:))RES RESTART: CNsers/BRAINVWAVEJAppData/localPrograms/P


ythonPython37-32/P1.py
How many kilometers?:15
15.0 kilometers is equal to 9.320585 miles
conv fac = 0. 621371 |>>>|

# calculate miles
miles= kilometers * conv fac
print( kilometers, ' kilometers is equal to', '=
, miles, 'miles')
6: Program to convert Celsius to Fahrenheit.
# Python Program to convert temperature in
celsius to fahrenheit
# Input is provided by the user in degree celsius
celsius = float (input('Enter in degree Celsius: '))
# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32


print (celsius, degree celsius =' ,fahrenheit, ' fahrenheit')
In this program, we ask the user for temperature in degree Celsius File Edt Sheil Debug Ogbens elndow Hei
and convert it into degree Fahrenheit. They are related by the Python 3.7.2 (tagsv3.7.2-9a3ffc0492. Dec 23 2018, 22-20:32))
MSC v.1916 32 bit (inte) on win32
formula celsius * 1.8 =fahrenheit -32. With a simple modification Type "help", "copyrighr,"credis or "icense( fo more informa
tion.

in this program, we can convert Fahrenheit into Celsius. We ask RESTART: CANsers BRAINWAVEJAppDataiLocalProgramsP
ythonvPython37-32/P1.py
the user for temperature in Fahrenheit and use the following Enter in degree Cesius: 28
28.0 degree celsius = 82.4 fahrenheit
formula to convert it into Celsius.

ARTI
electricitybill.
7: Programnto calculate the electricity bil
calculates the
program
# this reading: ")
ohon 12Shelt
Options Window Helo
Cr=input('Enter the
the last meter
current meter reading: ) File Edit Shei Debug
Python 3.7.2 (tags/v3.7.2:9a3fc0492 Dec 23 2018 ,22:20:52):
lr=input('Enter
(MSC v.1916 32 bit (Intel)] on win32
"help", "copyright", "credits" or "icense() for more
Type
unit:')) tion. infoma
(input('Enter the rate per RESTART: C:Users/BRAINWAVEIAppData/LocalPrograms/P
rate=eval ython/Python37-32IP1.py
Enter the last meter reading: 120380
uc=int (cr) -int (lr) Enter the current meter
reading:
unit:5.5
Enter the rate per
bill=uc*rate Total bill consumption is 260
1430.0
Total bill amount
consumption is',uc)
print ('Total bill
amount=',bill) Cot

print(Totalbill
and average of three subject marks.
8: Program tocalculate the total
student: ')
n=input('Enter name of the
the marks in English: '))
ereval (input ('Enter
'))
the marks in Math:
m=eval (input('Enter
marks in Science:'))
L Pythan372 Shell
indow Heto

S=eval (input('Enter the


FileEdt helDebug Options
Dec 23 2018, 22:20:52)
Python 3.7.2 (tagsV3.7.2:9a3fc0492,
on win32
[MSC v.1916 32 bit (Intel)] icense()" for more infoma
total= (etm+s) Type "hep", "copyright", "credits" or
tion.
avg-total//3 RESTART: C:Nsers/BRAINWAVEIAppData/LocalPrograms/P
i>>>

ython/Python37-32/P1.py
student',n) student:SAM
print('Name of the Enter name of the
English:80
Enter the marks in Math:85
Marks=',total) Enter the marks in
print(Total Enter the marks in
Science:75
Name of the student SAM
print ('Average Marks=',avg) Total Marks= 240
|Average Marks= 80
fe12 Ck

interest.
9: Program to calculate the simple
Python program to find simple interest
#
("Enter the principal amount : "))
p= float (input
interest : "))
("Enter the rate of
r= float (input
time in years: "))
t= float (input ("Enter the LPthon372Shel
interest Help 22:20:52)
Options Window
# calculating simple (tagsv3.7.2:9a3fic0492, Dec 23 2018,
File Edit Shel Debug
Python 3.7.2 on win32
MSC v.1916 32 bit (Intel)]
"credits" or icense() for
more infoma
si = (p*r*t)/100 Type "help", "copyright,
tion.
# printing the values RESTART: C:Nsers/BRAINWAVEJAppDataLoca/ProgramsiP
ython/Python37-32P1.py
print ("principal amount: ", p) Enter theprincipal amount:
15000
Enter the rate of interest :12
print ("Interest rate: " ,r) Enter the time in years: 2
principal amount: 15000.0
Interest rate: 12.0
print ("Time in years : ",t) Time in years: 2.0
Simple Interest: 3600.0
print ("Simple Interest : ", si) >>>
LR12 Cat4

ARTIFICIAL INTELLIGENCE-IX
298
ACTIVITY4.14;: Al Quiz Time
A. Readthe following statements and select ) the correct option.
1. One off the following is not true for Python.
lal Itis high levelprogramming language. (b) It can be run on MS
Icl It is only available after paying license fee for Windows, Linux and Mac OS.
using it.
Which of the following data types is not supported in python?
(a) Alphanumeric (b) Integers (c) Strings (d) Floating Numbers
3, One of them developed the Python Language.
(a) Bill Gates (b) Guido van Rossum (c) Charles Babbage
A This is the prompt of the interactive shell of the
Python window.
(a) <<< (b) >>>
(c)
E Oneof the following statement is not valid for
variable names.
(a) Special character like &,@,?$are not allowed
(b) It should begin with an alphabet in
lowercase (c) Spaces are not allowed
(d) Letters, numbers and the underscore )character are allowed
6. The operator used in assignment statements.
(a) Equals to(-) (b)
Hash sign (#) (c) Asterisk(*)
7. Programming statements used to decide which instructions are to be executed under a given conditions.
(a)Order statements (b) Loop statements (c) Control statements
8. In Python all flow control statements are ended with one of the following
character.
(a) Semicolon(:) (b) Hash sign(#) (C) Colon(:)
D. Tne process wherein a set of instructions are repeated in a sequence either specified number of times or
untila condition is met.
(a)
Looping (b) Iteration (c) Any of them
0 nis type of error occurs when a program does not conform to the grammar of a programming language.
(a)Logical error (b) Syntax error (c) Grammatical error
11. Select the correct output of following python code.
a= "Hello"
b= "Python"
c=a-b
print (c)
(a) hello-world (b) helloworld (c) Error
12. What is
the output of this expression, print(12.4//2)
(a) 6
(b) 6.0 (c) 0.4

You might also like