How to install Python ?
For Ubuntu Systems
$ sudo apt-get install python3
$ sudo apt-get install python3-pip
$ sudo apt-get install ipython3
$ sudo apt-get install python3-numpy
For Fedora Systems
$ su # change to root user and run the following commands
# dnf install python3
# dnf install python3-pip
# dnf install python3-numpy
# dnf install python3-ipython
For Windows Systems
users can get the Python3 (Python3.5) from Miniconda (or Anaconda) distribution available at
the url: http://conda.pydata.org/miniconda.html
After installing the distribution, run the following commands in a command window.
> conda update conda
> conda install ipython
> conda install numpy
How to open python prompt after installation?
Step 1: Open Command Prompt
Step 2: Type "python"
Result: It should show python version installed in your system and displays python prompt.
How to close python prompt?
Type exit() to close the python prompt
Python Basics
1.Objects
>>> s="hello"
>>>s.capitalize()
Hello
>>>s.replace("lo","p")
help
2. Basic Types
>>>int(3.2)
3
>>> float(2)
2.0
>>> complex(1)
(1+0j)
>>> (1+2j)*(1-2j) #python can handle complex math
(5+0j)
>>> round(0.8)
1.0
>>> int(round(0.8))
1
3.Calculator
>>> 1+1
2
>>> 8/3
2
>>> 8./3
2.6666666666666665
>>> 8**2 #8 to the power of 2
64
>>> 8**0.5 #Square root of 8
2.8284271247461903
>>> 8 % 3 #modulus operator returns remainder
2
>>> 4 % 3.
1.0
4.Boolean values and comparison operators
>>> 1 > 6
False
>>> 2 <= 2
True
>>> 1 == 2
False
>>> 2 != 5
True
>>> not 2 == 5
True
>>> int(True)
1
>>> 0==False
True
>>> (2 > 1) and (5 < 8)
True
>>> (2 > 1) or (10 < 8)
True
5.Variable Assignment
>>> a = 1
>>> a
1
>>> b = 2
>>> b == a
False
>>> a = 1
>>> a = a + 1
>>> a
2
>>> a += 1
>>> a
3
>>> a -= 3
>>> a
0
6.Strings
>>> s = "molecular simulation"
>>> print s
molecular simulation
>>> "molecular " + 'simulation'
'molecular simulation'
>>> s = "hello"*3
>>> s
'hellohellohello'
>>> len("Scott's class") #To find string length in terms of number of characters
13
>>> "ram" in "Programming is fun."
True
>>> "y" in "facetious"
False
>>> s = "The value of pi is %8.3f." % 3.141591
>>> print s
The value of pi is 3.142
7.Lists
>>> list1= [1,2,3,4,5]
>>> print list1
[1, 2, 3, 4, 5]
>>> [1,2] + [3,4]
[1, 2, 3, 4]
>>> [1,2]*3
[1, 2, 1, 2, 1, 2]
>>> range(4) #range() function lists sequence of numbers. syntax is range(start,stop,step)
[0, 1, 2, 3]
>>> range(1, 4)
[1, 2, 3]
>>> range(0, 8, 2)
[0, 2, 4, 6]
>>> len([1, 3, 5, 7])
4
8. Accessing list elements
>>> l = [1,4,7]
>>> l[0]
1
>>> l[2]
7
>>> l = [1,4,7]
>>> l[0] = 5
>>> l
[5, 4, 7]
>>> l = [1,4,7]
>>> l[-1]
7
>>> l[-3]
1