py
py
types
Python is available on a wide variety of platforms including Linux and Mac OS X. Let's
understand how to set up our Python environment.
Local Environment Setup
Open a terminal window and type "python" to find out if it is already installed and which
version is installed.
• Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
• Win 9x/NT/2000
• Macintosh (Intel, PPC, 68K)
• OS/2
• DOS (multiple versions)
• PalmOS
• Nokia mobile phones
• Windows CE
• Acorn/RISC OS
• BeOS
• Amiga
• VMS/OpenVMS
• QNX
• VxWorks
• Psion
• Python has also been ported to the Java and .NET virtual machines
Getting Python
The most up-to-date and current source code, binaries, documentation, news, etc., is available
on the official website of Python https://www.python.org/
You can download Python documentation from https://www.python.org/doc/. The
documentation is available in HTML, PDF, and PostScript formats.
Installing Python
Python distribution is available for a wide variety of platforms. You need to download only
the binary code applicable for your platform and install Python.
If the binary code for your platform is not available, you need a C compiler to compile the
source code manually. Compiling the source code offers more flexibility in terms of choice of
features that you require in your installation.
Here is a quick overview of installing Python on various platforms −
Unix and Linux Installation
Here are the simple steps to install Python on Unix/Linux machine.
• Open a Web browser and go to https://www.python.org/downloads/.
• Follow the link to download zipped source code available for Unix/Linux.
• Download and extract files.
• Editing the Modules/Setup file if you want to customize some options.
• run ./configure script
• make
• make install
This installs Python at standard location /usr/local/bin and its libraries at
/usr/local/lib/pythonXX where XX is the version of Python.
Windows Installation
Here are the steps to install Python on Windows machine.
•Open a Web browser and go to https://www.python.org/downloads/.
•Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version
you need to install.
•To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer
2.0. Save the installer file to your local machine and then run it to find out if your machine
supports MSI.
•Run the downloaded file. This brings up the Python install wizard, which is really easy to
use. Just accept the default settings, wait until the install is finished, and you are done.
Installation of Python
Programs on Data types
1.Numbers
a) Integers
>>> a=5
>>> b=c=5
>>> a
5
>>> b
5
>>> c
5
>>> a+b
10
>>> a+b+c
15
>>> type(a)
<class 'int'>
>>> a/c
1.0
>>> a//c
1
>>> a*b
25
b) Floating-Point Numbers
>>>a= 4.2
>>>a
4.2
>>> type(a)
<class 'float'>
>>> b=4.
>>>b
4.0
>>> a+b
8.2
c) Complex Numbers
>>> d=2+4j
>>> type(d)
<class 'complex'>
>>> a=2
>>> b=3j
>>> c=a+b
>>> c
(2+3j)
d)
2. Strings
>>> str="cvsr"
>>> str
'cvsr'
>>> type(str)
<class 'str'>
>>> str1="agi"
>>> str+str1
'cvsragi'
>>> print('a\
... b\
... c')
a... b... c
>>> print('foo\\bar')
foo\bar
>>> print('foo\tbar')
foo bar
>>> print("a\tb")
a b
>>> print('\u2192 \N{rightwards arrow}')
→→
>>> print('foo\nbar')
foo
bar
>>> print(r'foo\nbar')
foo\nbar
>>> print(R'foo\\bar')
foo\\bar
>>> print('''This string has a single (') and a double (") quote.''')
This string has a single (') and a double (") quote.
>>> print("This string contains a double quote (\") character.")
This string contains a double quote (") character.
>>> print('This string contains a single quote (\') character.')
This string contains a single quote (') character.
3.LIST
>>> a=[]
>>> a
[]
>>> a=[1,'b',5.5,'c']
>>> a
[1, 'b', 5.5, 'c']
>>> type(a)
<class 'list'>
>>> a[1]
'b'
>>> a[-1]
'c'
>>> a[1]=10
>>> a
[1, 10, 5.5, 'c']
4. Tuple
>>> a=( )
>>> a
()
>>> a=(5,'b',10,5.8)
>>> a
(5, 'b', 10, 5.8)
>>> type(a)
<class 'tuple'>
>>> a[1]
'b'
>>> a[2]
10
>>> a[-1]
5.8
5.Dictionary
>>> d={}
>>> d
{}
>>> type(d)
<class 'dict'>
>>> dict={"name":"cvsr","number":10,"loc":"hyderabad"}
>>> dict
{'name': 'cvsr', 'number': 10, 'loc': 'hyderabad'}
>>> type(dict)
<class 'dict'>
>>> dict["name"]
'cvsr'
>>> dict["number"]=88
>>> dict
{'name': 'cvsr', 'number': 88, 'loc': 'hyderabad'}