Python

Basic syntax, classes, conventions. Best for programmers who already know Js/Java/Ruby.

8 flashcards · Programming Languages · by Xiang J

  1. What are raw strings mostly used for in Python?

    This syntax variant exists mostly because the syntax of regular expression patterns is heavy with backslashes (but never at the end, so the "except" clause above doesn't matter) and it looks a bit better when you avoid doubling up each of them. # import Python's regular expressions module import re p = re.compile(r'\d+') Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides…

  2. pass vs break vs continue?

    break leaves a loop continue jumps to the next iteration pass does nothing, just to avoid IndentationError: expected an indented block

  3. Explain these underscore naming conventions: Single Leading Underscore: _var Single Trailing Underscore: var_ Double Leading Underscore: __var Double Leading and Trailing Underscore: __var__

    _var - convention: private variables [not entirely a convention, on * imports _vars are not imported] var_ - convention: to avoid naming conflicts __var - enforced: truly private variables, will get mangled (prefixed with ClassName) on inheritance __var__ - convention: not touched by interpreter, but don't use them in your program, python uses them Now if you use a wildcard import to import all names from the module, Python will not import names with a leading underscore Links: https://dbader.o…

  4. What's a raw string in Python?

    A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, \, is taken as meaning "just a backslash" (except when it comes right before a quote that would otherwise terminate the literal) -- no "escape sequences" to represent newlines, tabs, backspaces, form-feeds, and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence. print('hi \n hi') => hi hi print(r'hi \n hi') => hi \n hi

  5. What's literal in Python?

    Something that the parser recognizes as syntax for writing an object directly, succinct and easily visible way to write a value: -3, -2, -1, 0, 1, 2, 3 (int literals) 2+3j, 0+5j, 2j, -3-5j (complex literals) 3.5, -2.7 (float literals) "", "hello" (str literals) u"", u"hello" (unicode literals) [], [1,2] (list literals) (), (1,), (1,2) (tuple literals) {}, {'a': 2} (dict literals) {1,2,3} (set literal introduced in Python 2.7) None (I think this is considered a literal, and not a keyword or simp…

  6. list = ['a', 'b', 'c', 'd']; print "list[1:3]: ", list[1:3] #=> ?

    list[1:3]: ['b', 'c'] Just like js's list.slice(1, 3), it returns a new list (without modifying it) from [1arg, 2arg).

  7. What does __name__ refer to?

    __name__ is either: string 'name of the current module' if module is being run directly - string '__main__'

  8. What does this code achieve? if __name__ == '__main__': celery_instance.start()

    Only execute .start() when we execute the module directly, e.g.: python celery_instance.py