
Learning different programming languages ,technologies is like a passion for me and same i am doing this with Python as well ,thanks to Tata Consultancy Services for making us dive in
into Python straight away..
As i know except few most of you are from other streams , so considering yourself as Beginners itz better to start with very basics of Python.
The program which we are discussing now covers Arithematic operators and printing the complex calculated results in desired form..
Arithematic Operators we are available with are:
----------------------------------------------------------------
a) add(+)
b) subtract (-)
c) multiply (*)
d) divide (/)
let say the the name of our new python project is "PythonArithematic" , let me repeat how we can create this project in MyEclipse 8.6, hope you are all done with integration of
MyEclipse and PyDev plugin...if not please go throgh previous posts..
Creating PythonArithematic project::
--------------------------------------------------------------------
1. Go to File --> New ---> PyDev Project to start a wizard.
2. In the next window that appears, enter the name of project as "PythonArithemati" and select "python" and 3.0"; as the type. Make sure "create default 'src' folder and add it to the pythonpath?" is selected. Click Finish.
3. Create a new module ::
Select the project "PythonArithemati" which we just created and go to File ---> New ---> PyDev Module. This will launch a new PyDev Module Wizard where we should enter a name for your module as "Arithematic.py" . Leave the Package field blank and select Finish.
4. The file should be opened in the open space in the center of the workspace-the Editor view. (If not, right click on the Arithematic.py icon and select Open.) You will see a tab with the name of your file.
so ..wat next.... you are now availabe with everything ready ...letzz do some arithematic operations..
Arithematic.py
------------------
a=2
b=3
c=a+b
print(c) #addition
c= a-b
print(c) #subtraction
c=a*b
print(c) #multiplication
c= b/a #division
print(c)
#printing the resuls in desired format
print("the sum of %d and %d is %d" %(a,b,a+b))
Output:
---------
5
-1
6
1.5
the sum of 2 and 3 is 5
So we finished up performing addition,subtraction,multiplication and division operation.
>>>print("the sum of %d and %d is %d" %(a,b,a+b))
In this command the format string contains '%' markers within it. The letter 'd' after the % tells Python that a 'decimal number' should be placed there.
The values to fill in the markers are obtained from the values inside the bracketed expression following the % sign on its own.
There are other letters that can be placed after the % markers. Some of these include:
%s - for string
%x - for hexadecimal number
%0.2f - for a real number with a maximum of 2 decimal places
%04d - pad the number out to 4 digits with 0's
The Python documentation will give lots more...I will suggest everyone to please go through Python documentaion .
No comments:
Post a Comment