Wednesday 24 April 2013

Python Programming Tutorial 1 - Numbers and Math

This tutorials gives overview of the Python numbers and Math functions.

Using the Python as Calculator


Let's try some python command line options.  The python Interpreter act as an Calculator, we can do simple mathematical function in the command line.

Below given video, demonstrates the some of the python number and mathematical functions.

Note: Content was Scrapped from youtube.



A values can be assigned to several variable simultaneously.

[code]>>> x = y = z = 0 # Zero x, y and z
>>> x
0
>>> y
0
>>> z
0[/code]

Complex numbers are also supported; imaginary numbers are written with a suffix of j or J. Complex numbers with a nonzero real component are written as (real+imagj), or can be created with the complex(real, imag) function.

[code]>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0,1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> a=(1+2j)/(1+1j)
>>> a
(1.5+0.5j)
>>> a.real
1.5
>>>a.imag
0.5
>>> abs(a) # sqrt(a.real**2 + a.imag**2)
5.0[/code]

In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:

[code]>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06[/code]

Python Programming Tutorial 2 - Strings

Subscribe for next update. Thanks you.

No comments:

Post a Comment