Python - Script 1 PDF
Python - Script 1 PDF
• In early 1990’s
• @ National Research Institute for Mathematics and Computer Science in
Netherlands.
Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
• Easy-to-use
o Interactive programming experience
• Open source
o Free to use and distribute
• Portable
o Supports a wide variety of hardware platforms
• Extendable
o Support for adding low-level modules
• Object-Oriented
o An object-oriented language, from the ground up with support for advanced
notions as well
• Databases
o Provision to connect with all major databases
Python is pre-installed in Linux and Mac OS machines, while installers for all the
operating systems are available at http://python.org/download/.
For windows : installer python-XYZ.msi file where XYZ is the version you are
going to install.
The actual process involves compiling the source code into a platform-
independent byte code for speed optimization at first. Then the byte code is
executed by the runtime engine called Python Virtual Machine.
Enter python and start coding right away in the interactive interpreter by starting
it from the command line.
You can do this from Unix, DOS or any other system, which provides you a
command-line interpreter or shell window.
$ python # Unix/Linux
C:> python # Windows/DOS
You can run Python from a graphical user interface (GUI) environment as well.
Unix: IDLE is the very first Unix IDE for Python.
Windows: PythonWin is the first Windows interface for Python and is an
IDE with a GUI. IDLE comes along with Python itself.
Macintosh: The Macintosh version of Python along with the IDLE IDE is
available from the main website, downloadable as either MacBinary or
BinHex'd files.
PyDev, a plugin for Eclipse that turns Eclipse into a full-fledged Python IDE . Both
Eclipse and PyDev are cross-platform and open source.
Naming Rules
A comment may appear at the start of a line or following whitespace or code, but
not within a string literal.
>>> print('hello‘)
hello
>>> print('hello', 'there‘)
hello there
>>> (50-5*6)/4
5.0
>>> 7//-3
-3
>>>7%3
1
>>> a=b=c=1
>>> a,b,c=1,2,’John’
The number of spaces in the indentation is variable, but all statements within the
block must be indented by the same amount.
>>>if True:
print("True“)
else:
print("False“)
Statements contained within the [], {} or () brackets do not need to use the line
continuation character.
For example:
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
>>> var1 = 1
>>> var2 = 10
>>> 1j * complex(0, 1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5
Usually the single quote for a word, double quote for a line and triple quote for a
paragraph.
>>>word = 'word'
>>>sentence = "This is a sentence."
>>>paragraph = “““This is a \
paragraph \
across \
multiple lines.”””
>>> ’doesn\’t’
"doesn’t“
>>> "doesn’t"
"doesn’t“
>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name
>>>s='aaa,bbb,ccc,dd’
>>>s.split(“,") # Split the string into parts using ‘,’ as delimiter
['aaa','bbb','ccc','dd']
>>> ", ".join(names) # Join the list elements into a string using ‘,’
‘Ben, Hen, Pen’
>>> "Br" in “Brother” # ‘in’ and ‘not in’ operators to check the existence
True
>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[1][0]
2
>>>a=a+[10,20]
This will create a second list in memory which can (temporarily) consume a lot of
memory when you’re dealing with large lists
append takes a single argument(any datatype) and adds to the end of list
>>>a=[10,20,30]
>>>a.append(‘new’)
>>> a
[10,20,30,’new’]
>>> a.append([1,2,3])
>>>a
[10,20,30,’new’,[1,2,3]]
Copyright © 2013 Tech Mahindra. All rights reserved. 43
Adding to Lists
Method 3 : Using extend
extend takes a single argument(list),and adds each of the items to the list
>>>a=[10,20,30]
>>>a.extend([1,2,3])
>>>a
[10,20,30,1,2,3]
>>>a=[10,20,30]
>>>a.insert(0,‘new’)
>>> a
[’new’,10,20,30]
>>>a.insert(100,‘python’)
>>> a
[’new’,10,20,30,’python’]
Copyright © 2013 Tech Mahindra. All rights reserved. 44
Deletion in Lists
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>>del a
>>>a
Name Error : name ‘a’ is not defined
>>> a.index(333) # Returns the index of the given value in the list
1
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> names
['ben', 'chen', 'yaqin']
They work like associative arrays or hashes found in Perl and consist of key-
value pairs.
A dictionary key can be almost any Python type, but are usually numbers or
strings.
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and
accessed using square braces ( [] ).
>>> D
{'age': 40, 'job': 'dev', 'name': 'Bob'}
>>> D[‘name']
Bob
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string.
~: python input.py
What's your name?
> Michael
What year were you born?
>1980
Hi Michael! You are 35 years old!
Statement Description
An if statement consists of a boolean expression followed by one or
if statements
more statements.
An if statement can be followed by an optional else statement,
if...else statements
which executes when the boolean expression is false.
You can use one if or else if statement inside another if or else if
nested if statements
statement(s).
var = 100
if ( var == 100 ) :
print("Value of expression is 100“)
print("Good bye!“)
var2 = 0
if var2:
print("2 - Got a true expression value“)
print(var2)
else:
print("2 - Got a false expression value“)
print(var2)
count = 0
while count < 5:
print(count, " is less than 5“)
count = count + 1
else:
print(count, " is not less than 5“)
apple red
mango yellow
0 tic
1 tac
2 toe
9
7
5
3
1
apple
banana
orange
pear
The pass is also useful in places where your code will eventually go, but has
not been written yet (e.g., in stubs for example):