Skip to main content

Sample 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

*************************************************************************************************

Now you can modify this program to compute.

1) area of the circle given the diameter.
2)area of the circle given the circumference.
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.
5)area  and perimeter of a right angled triangle given width(w) and height(h).
6)volume and surface area of a sphere given the radius.
7)area of an equilateral triangle given one side (s).
8) volume and surface area of the cylinder given radius(r) and height(h).
9) volume and surface area of a cube given one side ( s).
10)hypotenuse of a right-angled triangle, given the length of the other two sides.( university question)

Comments

Popular posts from this blog

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…

User Defined Functions in Python

So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s) and are called User Defined Functions. These functions can then be combined to form a module which can then be used in other programs by importing them.
In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition.
To define a function keyword def is used. After the keyword comes an identifier i.e.name of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function. A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while executing. In Python, statements in a block are written with indentation. Usually, a block begins when a li…

Dictionary In Python

Dictionaries are similar to other compound types except that they can use any immutable type as an index. We can refer to a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair. A dictionary is an extremely useful data storage construct for storing and retrieving all key value pairs, where each element is accessed (or indexed) by a unique key. However, dictionary keys are not in sequences.
            Dictionaries are mutable.             Dictionaries are unordered. Items in dictionaries are accessed via keys and not via their position. A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionaries are unordered key-value-pairs.  Dictionaries don't support the sequence operation of the sequence data types like strings, tuples and…