Search This Blog

Tuesday 21 February 2017

python basics

Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica)
which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.


features of python:

It is a general purpose programming language which can be used for both
scientific and non scientific programming.

 It is a platform independent programming language.

It is excellent for beginners as the language is interpreted,
 hence gives immediate
results.

 The programs written in Python are easily readable and understandable.

 It is suitable as an extension language for customizable applications.

 It is easy to learn and use


python usage and applications:


---->In operations of Google search engine, youtube, etc.
----> Bit Torrent peer to peer file sharing is written using Python
---->Intel, Cisco, HP, IBM, etc use Python for hardware testing.
---->Maya provides a Python scripting API
---->i–Robot uses Python to develop commercial Robot.
---->NASA and others use Python for their scientific programming task.



python IDE:

To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment

downloaded from: www.python.org


^D (Ctrl+D) or quit () is used to leave the interpreter.
^F6 will restart the shell.

Python prompt& shell:
>>> This is a primary prompt indicating that the interpreter is
expecting a python command.

>>>print “How are you?”


without quotes it will be treated as variable


i)print 5+7
ii) 5+7
iii) 6*250/9
iv) print 5-7


sequence of instructions:

>>>a=1
>>>b=2
>>>c=a+b
>>>print c

single and mutiple variables:

>>> a,b=2,3
>>> a
2
>>> b
3
>>> a,b
(2, 3)

>>> a,b,c=1,2.3,'ab'
>>> a,b,c
(1, 2.3, 'ab')

python  script:

To create and run a Python script, we will use following steps in IDLE,
 if the script
mode is not made available by default with IDLE environment.

interactive mode:

1. File>Open OR File>New Window (for creating a new script file)
2. Write the Python code as function i.e. script
3. Save it (^S)
4. Execute it in interactive mode- by using RUN option (Alt+F5)

Otherwise (if script mode is available) start from Step 2


script mode:

Step 1: File> New Window
Step 2:
def test():
x=2
y=6
z = x+y
print z
Step 3:
Use File > Save or File > Save As - option for saving the file
(By convention all Python program files have names which end with .py)
Step 4:
For execution, press ^F5, and we will go to Python prompt (in other window)
>>> test()


IN LINUX ENVIRONMENT:


[training@localhost ~]$ vi program.py
a=1
b=2
c=a+b
print c
~
~
~
[training@localhost ~]$ python program.py
3

Variables and Types::

When we create a program, we often like to store values
so that it can be used later. We use objects to capture data


1. Number
Number data type stores Numerical Values. This data type is immutable i.e. value
of its object cannot be changed.
a) Integer & Long
b) Float/floating point
c) Complex

-2147483648 to 2147483647


>>> import sys
>>> print sys.maxint
2147483647


>>> a=123
>>> type(a)
<type 'int'>
>>> b=34.098
>>> type(b)
<type 'float'>
>>> c=2435637L
>>> type(c)
<type 'long'>
>>> d='c'
>>> type(d)
<type 'str'>



>>> y=20+10j
>>> print y.real
20.0
>>> print y.imag
10.0
>>>


2.Sequence
A sequence is an ordered collection of items, indexed by positive integers. It is
combination of mutable and non mutable data types. Three types of sequence data
type available in Python are Strings, Lists & Tuples.

String: is an ordered sequence of letters/characters. They are enclosed in
single quotes („ ?) or double („? “)

>>> str='abc'
>>> type(str)
<type 'str'>
>>> str1='12'
>>> type(str1)
<type 'str'>
>>> str2='12.34'
>>> type(str2)
<type 'str'>

str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string

-Type casting:

For explicit type casting, we use functions (constructors):
int ()
float ()
str ()
bool ()



Lists are the most versatile of Python's compound data types.
 A list contains items separated by commas and enclosed within square brackets ([])
. To some extent, lists are similar to arrays in C.
One difference between them is that all the items belonging to
 a list can be of different data type.


list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists



Tuples: Tuples are a sequence of values of any type, and are indexed by
integers. They are immutable. Tuples are enclosed in (). We have already seen
a tuple, in Example 2 (4, 2).

 Sets:
Set is an unordered collection of values, of any type, with no duplicate entry. Sets
are immutable.
Example

s = set ([1,2,34])


Dictionaries: Can store any number of python objects. What they store is a
key – value pairs, which are accessed using key. Dictionary is enclosed in
curly brackets.
Example
d = {1:'a',2:'b',3:'c'}


A partial list of keywords in Python 2.7 is
and del from not
while as elif global
or with assert else
if pass yield break
except import print class
exec in raise continue
finally is return def
for lambda try



>>> print "%s is clever" %("ramu")
ramu is clever

>>>a=1
>>> print "%d is integer" % a
1 is integer


>>> print "%s is string " % "welcome"
welcome is string

>>> f=1.345
>>> print "%f is float" %f
1.345000 is float




s = set ([1,2,34])









No comments:

Post a Comment

Hadoop Analytics

NewolympicData

  Alison Bartosik 21 United States 2004 08-29-04 Synchronized Swimming 0 0 2 2 Anastasiya Davydova 21 Russia 2004 0...