Python Basic. Download python Go to and download Python 2.7.8.
Beginning Python
-
Upload
agiliq-info-solutions-india-pvt-ltd -
Category
Technology
-
view
12 -
download
1
description
Transcript of Beginning Python
![Page 1: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/1.jpg)
Shabda Raaj
![Page 2: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/2.jpg)
This is a workshop, not a talk. You are expected to code along. So pull out your laptops. Install python, ipython and komodo edit. (Or
editor of your choice.)
![Page 3: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/3.jpg)
Assume that you know programming in any language.
So we don’t spend a lot of time explaining basics.
BUT, stop and ask if something doesn't make sense.
AND Definitely Stop me if I am going too slow, too fast, or making no sense.
![Page 4: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/4.jpg)
Dynamically but strongly typed. Very object oriented - everything is an object. But pragmatic - Objects aren't everthing. Allows various paradigms of programming -
OO, procedural, functional. Shallow learning curve, but powerful powerful
capabilities available, when you need them. import this We will come back to this slide.
![Page 5: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/5.jpg)
Hello world >>> print "Hello World"
![Page 6: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/6.jpg)
for, while, if, else, break, continue
-Yeah they are available, surprised?
We will use them in a moment, but after we see the data structures available.
![Page 7: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/7.jpg)
List - Like ArrayList in Java Tuple - Like List, but immutable Dict - Like Hashmaps in Java
![Page 8: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/8.jpg)
For Loop
for el in iterable:
[block statement]
the classic for loop
for (int i; i < n; i++){}
for i in range(n):
#Work with I
While condition:
[block]
break, continue. Normal operation - break out of current loop.
![Page 9: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/9.jpg)
If: elif: else:
if condition: [block] else: [block]
![Page 10: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/10.jpg)
You have enough information now to write a solution
Problem statement: Write a program that prints the numbers from
1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
![Page 11: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/11.jpg)
def function_name(argument_list):
[block]
Functions can have default value. def fizzbuzz(till=100, fizz='fizz', buzz='buzz'):
#fi zzbuzz code
Functions can have variable length values. ex multiply all values passed to a function.
Functions are first class - They are objects too. They can be passed to other
functions, assigned to variables etc.
![Page 12: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/12.jpg)
class ClassName(base_classes):
[block]
Classes are first class too They can be passed to function, and assigned
to variables.
![Page 13: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/13.jpg)
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000
![Page 14: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/14.jpg)
The last solution was needlessly verbose List comprehension: Take a list and transform it. Standard list comprehension syntax - [expr(i) for i
in iterable if condition] List of all squares: [i*i for i in range(1,11)] List of all squares of even numbers: [i*i for i in
range(1,11) if i%2 == 0] So solution to last problem is just sum([i*i for i in range(1,1001) if i%3 == 0 or
i%5==0])
![Page 15: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/15.jpg)
List comprehensions are python way to do functional programming constructs
[function(i) for i in iterable if condition] is filter(func2, map(func1, iter))
Lets see how this list comprehension maps to functional concepts
Get the list of squares of even numbers
![Page 16: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/16.jpg)
Open a file with - open('location') or file('location')
or give a mode - open('location', 'rw') iterate as
for line in open_file.readlines():
print line #Or whatever
or string = open_file.read()
![Page 17: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/17.jpg)
Find the most commonly used word in the Alice in wonderland text.
![Page 18: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/18.jpg)
Batteries included math
datetime
string
re
random
os
pickle Do a dir() and see for yourself. And a lot, lot more: http://docs.python.org/library/
![Page 19: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/19.jpg)
Dynamically but strongly typed. Very object oriented - everything is an object. But pragmatic - Objects aren't everything. Allows various paradigms of programming -
OO, procedural, functional. Shallow learning curve, but powerful powerful
capabilities available, when you need them. import this
![Page 20: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/20.jpg)
Syntacting sugar for
foo_func =docorator_func(foo_func)
![Page 21: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/21.jpg)
Many useful frameworks
Tornado,
Pylons,
Turbogears
Django
GAE
Django: Most actively developed and largest community participation
![Page 22: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/22.jpg)
PIL Mechanize Beautiful Soup Element Tree
![Page 23: Beginning Python](https://reader034.fdocuments.in/reader034/viewer/2022051608/5454a772af7959a1608b8ad0/html5/thumbnails/23.jpg)
python.org diveintopython.org uswaretech.com/blog