Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python

 Python

Mike Yumatov

October 12, 2012
Tweet

More Decks by Mike Yumatov

Other Decks in Programming

Transcript

  1. public class QuickSort { public static void quicksort(int array[], int

    left, int right) { int i = left, j = right; int pivot = array[(left + right) / 2]; do { while (array[i] < pivot) { ++i; } while (array[j] > pivot) { --j; } if (i <= j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; i++; j--; } } while (i <= j); if (left < j) { quicksort(array, left, j); } if (i < right) { quicksort(array, i, right); } } }
  2. def quicksort(array): if array: left = quicksort([x for x in

    array[1:] if x < array[0]]) right = quicksort([x for x in array[1:] if x >= array[0]]) return left + array[:1] + right return []
  3. Популярность языков на GitHub JavaScript (21%) Ruby (14%) Python (8%)

    Java (8%) Shell (8%) PHP (7%) C (6%) C++ (4%) 0 5 10 15 20 25
  4. Beautiful is better than ugly. Explicit is better than implicit.

    Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! The Zen of Python
  5. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users))
  6. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users)) Значащие отступы
  7. def users(request): if request.user.is_anonymous() or not request.user.is_active: raise Http404 users

    = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users)) Именованные аргументы
  8. manage_file( '/etc/vim/vimrc', 'salt://vim/vimrc', 'root', 'root', 0644, 'jinja', ) manage_file( name='/etc/vim/vimrc',

    source='salt://vim/vimrc', user='root', group='root', mode=0644, template='jinja', )
  9. manage_file( '/etc/vim/vimrc', 'salt://vim/vimrc', 'root', 'root', 0644, 'jinja', ) manage_file( name='/etc/vim/vimrc',

    source='salt://vim/vimrc', user='root', group='root', mode=0644, template='jinja', ) Так понятнее
  10. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users)) Логические операторы — слова (and, or, not)
  11. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users)) Динамическая типизация
  12. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users)) Строка документации
  13. if 0 <= x < 10: print('x has one digit.')

    elif 10 <= x < 100: print('x has two digits.') else: print('x has many digits.') Условный оператор
  14. False 0 '' () [] {} set() — пустая строка

    — пустой кортеж — пустой список — пустой словарь — пустое множество
  15. Циклы for x in range(10): print(x) x = 0 while

    x < 1000: x += x - 3 x = 0 while True: x += x - 3 if x >= 1000: break
  16. Стандартные типы данных — bool — list, tuple, range —

    str — bytes, bytearray, memoryview — set, frozenset — dict
  17. def users(request): """Show a list of active users.""" if request.user.is_anonymous()

    or not request.user.is_active: raise Http404 users = User.objects.filter(is_active=True) return render(request, 'users.html', dict(users=users))
  18. len(a) str(a) a[2] b in a a + b a.__len__()

    a.__str__() a.__getitem__(2) a.__contains__(b) a.__add__(b) Python
  19. from flask import Flask app = Flask(__name__) @app.route("/hello/") def hello():

    return "Hello World!" if __name__ == "__main__": app.run() Веб-разработка
  20. twitter.com — mitsuhiko — kennethreitz — alex_gaynor — zzzeek —

    raymondh — tarek_ziade — voidspace — zeeg — jessenoller — nedbat
  21. codeforces.ru — C — C++ — C# — Pascal —

    Java — Ruby — Python — PHP — Haskell — F# — OCaml — Scala
  22. spoj.pl — C — C++ — C# — Pascal —

    Java — Ruby — Python — PHP — Haskell — F# — OCaml — Scala — Lua — Go — Erlang — Clojure — JavaScript — Perl