© 2013 NUS. The contents contained in this document may not be reproduced in any form or by any means, without the written...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 2What is Python?Sm...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 3Python FeaturesDy...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 4Python Language F...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 5Python Sampledef ...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 6Python Sampledef ...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 7Android phone/tab...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 8Android phone/tab...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 9Django for the We...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 10Django ModelMode...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 11URL matchingimpo...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 12Viewsfrom mysite...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 13productlisting.h...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 14product.html tem...
© 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 15SummaryPython la...
Upcoming SlideShare
Loading in …5
×

Python for the Mobile and Web

1,062 views
869 views

Published on

Introduction and Why use Python presented at KMD, Myanmar.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,062
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python for the Mobile and Web

  1. 1. © 2013 NUS. The contents contained in this document may not be reproduced in any form or by any means, without the written permission of ISS, NUS other than for the purpose for which it has been supplied.Software DevelopmentPython for Mobile and WebTotal: 15 pagesATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0Derek Kiongdkiong@nus.edu.sg
  2. 2. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 2What is Python?Small and consistent programming languageSupports multiple programming paradigms –object-oriented, imperative and functionalDesign philosophy emphasizes codereadability – has clear syntaxhas comprehensive standard librarylatent (dynamic) type systemrun-time model similar to Scheme/Lisp
  3. 3. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 3Python FeaturesDynamic typing – does not requiredeclarationContrast C and Java which have strong typing(implemented through compile-time checks)Dynamic objects with built-in memorymanagementSimilar with Java and C#Contrast C and C++Even function definition is dynamic
  4. 4. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 4Python Language Featuresint, float, complex, bool, str, byteslist, tuple, set, dictcontrol flow statements, iterator, generator,exception handlingfunctions, parametersclass, object, lambdaextensive function/class librarieskivy and django are Python modules
  5. 5. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 5Python Sampledef factorial(n):if n > 1:return n*factorial(n-1)else:return 1def list(n):for i in range(n):if i%2 == 0:print "{0} is even".format(i)else:print "{0} is odd".format(i)
  6. 6. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 6Python Sampledef factorial2(n):result = 1for i in range(n,0,-1):result = result*ireturn resultdef myadd(a,b):"""Adds a and b using + operator"""return a+b
  7. 7. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 7Android phone/tabletapp = androidhelper.Android()app.dialogCreateSpinnerProgress("Coffee", "checking...")app.dialogShow()pg = urlopen("http://www.beans-r-us.biz/prices.html")text = pg.read().decode("utf8")m = re.search(r>$([.d]+)<,text)if m:price = float(m.group(1))else:price = 0app.dialogDismiss()app.vibrate()
  8. 8. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 8Android phone/tabletapp.dialogCreateAlert("Current price of coffee beans:")app.dialogSetItems([price])app.dialogSetPositiveButtonText(OK)app.dialogShow()resp = app.dialogGetResponse().resultapp.makeToast("Bye!")
  9. 9. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 9Django for the WebModel-View-Controller (MVC) WebframeworkObject-relational mappingGenerated admin pagesURL driven siteTemplating system
  10. 10. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 10Django ModelModel specified in Pythonfrom django.db import modelsclass Product(models.Model):code = models.CharField(max_length=8)description = models.CharField(max_length=22)price = models.IntegerField()def __str__(self):return self.descriptionclass Admin: pass
  11. 11. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 11URL matchingimport mysite.inventory.viewsurlpatterns = patterns(,# Examples:# url(r^$, mysite.views.home, name=home),# url(r^mysite/, include(mysite.foo.urls)),url(r^product/?$, mysite.inventory.views.productlisting),url(r^product/(.+)$, mysite.inventory.views.product),)
  12. 12. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 12Viewsfrom mysite.inventory.models import Productdef productlisting(request):list = Product.objects.all()t = get_template(productlisting.html)html = t.render(Context({products:list}))return HttpResponse(html)def product(request, pcode):p = Product.objects.get(code=pcode)t = get_template(product.html)html = t.render(Context({product:p}))return HttpResponse(html)
  13. 13. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 13productlisting.html template<html><body><h1>Products</h1><ul>{% for p in products %}<li><a href="/product/{{ p.code }}">{{ p.code }}</a></li>{% endfor %}</ul></body></html>
  14. 14. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 14product.html template<html><body><h1>Product Information</h1><table border="1" style="solid"><tr><th>Code</th><th>Description</th><th>Price</th></tr><tr><td> {{ product.code }} </td><td> {{ product.description }} </td><td> {{ product.price }} </td></tr></table></body></html>
  15. 15. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 15SummaryPython late binding provides much flexibilityhttp://docs.python.org/2/reference/QPython in Androidhttp://qpython.com/Django for the Webhttps://docs.djangoproject.com/en/1.5/

×