LUGers Intro to Python Programming

adventurescoldΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

90 εμφανίσεις

LUGers Intro to Python Programming
Victor Miclovich
MIT EPROM
Coders without Borders
July 13,2010
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 1/17
Outline
1
Introduction
1
Why People use Python
1
Getting started with Python
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 2/17
Intoduction
Python is:
a scripting language
an objected oriented programming language
Python is a simple yet powerful programming language with excellent functionality
for doing lots of simple (and complex) jobs such as linguistic processing,network
tasks,web programming,a
nd mobile programming.
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 3/17
Why People use Python
High quality software is produced because of Simplicity and ease of use
Developer Productivity is Increased
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 4/17
Quick look
for line in open("file.txt"):
for word in line.split():
if word.endswith(’ing’):
print word
This piece of code prints words that end with ’ing’.The words come from a file
named ’file.txt’
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 5/17
Rolling with Python
Python is installed by default in most Linux and Mac distros.
Start Python by typing the word ”python” in your shell
$ python
This Gives You
Python 2.6.5 (r265:79359,Mar 24 2010,01:32:55)
[GCC 4.0.1 (Apple Inc.build 5493)] on darwin
Type"help","copyright","credits"or"license"for more informati
o
>>>
I did this on my Mac,for Linux or Windows,an appropriate message will show up
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 6/17
Python as a calculator
You can do math with Python.
Addition
>>> 1 + 3
4
>>>
Subtraction
>>> 4 - 1
3
>>>
Multiplication
>>> 4 * 3
12
>>>
Try other kinds of math
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 7/17
The Art of the Program
Programs manipulate data,data gets stored,crunched,messed up and refined
How is it stored?
Variables store data in the computer’s memory
Variables are like addresses:They tell us where some piece of data has been stored
Programs are instructions
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 8/17
Data structures
Data structures help to just ”structure” data
They are used to e!ciently store and retrieve data in cases where just one variable
can’t store more than one piece of data
Examples:
suppose you want to store the name,age and sex of a person:You can store
it in the following ways
person = (’victor’,21,’male’)
person = {name="victor",age:21,sex="male"}
storing addresses of a network
mtn_mbale = ("192.157.2.0","255.255.255.255","switch")
mtn_mbale =\{subnet:"192.157.2.0",mask:"255.255.255.255",
dev_type:"switch"\}
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 9/17
Data structures
In Python there are about 3 major data structures and numerous inbuilt/custom
data structures
Lists:
[ ],[1,2,3],[’victor’,’richard’]
Tupples:
(),(2,3)
More Info
http://docs.python.org/tutorial/datastructures.html
http://bit.ly/93NoLn
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 10/17
Operations on Data
When you store data in data structures,you do certain operations on that data
Sorting a list of numbers
Sorting a list of names i.e.alphabetical orders,descending,etc.
>>> x = [3,2,1,5,4]
>>> x.sort()
>>> x
[1,2,3,4,5]
>>>
Resources:
Previous slide listed a couple of web resources you can use
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 11/17
Common Programming concepts
Iteration (repeatition)
Looping
conditionals
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 12/17
Iteration and looping
This is repeating of task.There are many things we do in life that require
repeating.
Wake up
Wash up
Eat up
Work
Sleep
...
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 13/17
Conditionals
Programs are written with ”directions” or ”control” in mind
What do you do when there’s a lot of tra!c hitting your server?Write a script?
Shut the server?etc.
More info:
http://en.wikibooks.org/wiki/Python/ugana
Programming/Conditional
Statements
#example of conditions and functions
def factorial(x):
if x > 0:
return x * factorial(x-1)
else:
return 1
Take a look at the tutorial
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 14/17
Advanced concepts in Python
Object Oriented Programming
Exceptions and Exception handling
Modular programming and using libraries
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 15/17
What you can do with Python
Web Design (with Django,Appengine,Plone,Turbogears)
Natural Language processing (NLTK)
Game Programming (PyGame,OpenGL,Blender API)
Scientific computation (numpy,scipy,numarray)
Animations and graphics
etc.
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 16/17
More resources
http://python.org
http://www.freenet.org.nz/python/
http://oreilly.com/python/
http://www.diveintopython.org
It takes quite some time to learn the language
Good news
:
it takes a shorter time with Python
Victor Miclovich
MIT EPROMCoders without Borders ()
LUGers Intro to Python Programming
July 13,2010 17/17