Skip to main content

Simple Programs to Begin With

use IDLE in linux/windows for the program development.Open a file and type the program .Save the file with extension '.py' eg: test.py. From the run menu click run module (F5) to run the program.If you are running the program from command prompt type $python test.py

*************************************************************************
#Python Program to find Area and Circumference of a Circle
#Standard formula to calculate the Area of a circle is: a=π r². Circumference c=2 π r.
r=input(“Enter radius”)
a=3.14159 * r * r
c=2* 3.14159 * r
print “Area of the circle”,a
print “Circumference of the circle”,c

**************************************************************************
#Input a time in seconds and print the time in HH:MM:SS format ( university question)
time=input("Enter time in seconds")
timeinmin=time/60
timeinsec=time%60
timeinhr=timeinmin/60
timeinmin=timeinmin%60
print "HH:MM::SS----",timeinhr,":",timeinmin,":",timeinsec

***************************************************************************
Now you can try the following simple programs.

1) area of the circle given the diameter.
2)area of the circle given the circumference.(circumference=2*pi*r)
3)area of the circle given center point (x1,y1) and one point on the perimeter (x2,y2).
(Hint: input the two points and compute the distance between them as radius)
4)area of a triangle given three sides a,b,c.(use Heron's formula)
5)area  and perimeter of a right angled triangle given width(w) and height(h).
(area=1/2.0*w*h .use Pythagoras theorem to find the hypotenuse then add the three sides to find the perimeter)
6)volume and surface area of a sphere given the radius.
(volume=4/3.0*pi*r*r*r  surface area=4*pi*r*r)
7)area of an equilateral triangle given one side (s).( sqrt(3)/4.0*s*s)
8) volume and surface area of the cylinder given radius(r) and height(h).
(volume=pi*r*r*h   surfacearea=2*pi*r*r+2*pi*r*h)
9) volume and surface area of a cube given one side ( s).
(volume=s*s*s surface area=6*s*s)
10)hypotenuse of a right-angled triangle, given the length of the other two sides.( university question) ( use Pythagoras theorem)
11)Enter length and breadth  of a rectangle and find its area and perimeter.
12)Enter name and marks in 6 subjects of a student and find the total, average and percentage.
13)Enter temperature in Celsius and covert it into Fahrenheit.( F=C*(9/5.0) + 32 )
14)Write a program to read P,T, R  and calculate simple interest.(SI=(P*T*R)/100)
15)Write a program to read P,T,R and calculate compound interest.(CI=P(1+R/100)*T)
16)Input a time in seconds and print the time in HH:MM:SS format ( university question)

Comments

Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language"""
This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’
We can also slice a string. Segment of a string is called a slice.
>>>s[1:3] ‘yt’
If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’
>>>s[2:6] ‘thon’
If the last index is not mentioned, the slicing will go till the end o…

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…