daily pastebin goal
47%
SHARE
TWEET

Brief Overview of Python

Kesha Nov 27th, 2013 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 20:29 < onpon4> Alright, it's about time to start.
  2. 20:30 < onpon4> I think normally I'm supposed to get some sort of elevated permissions, but I don't need to use that, so I'll start right now.
  3. 20:31 < onpon4> Python is an interpreted, general-purpose programming language sort of similar to Lisp. It's named after Monty Python, so references to that are appropriate.
  4. 20:32 < onpon4> Python is dynamically typed, and duck typed. You don't need to declare variables or assign a type to them; it's all automatic.
  5. 20:32 < onpon4> So, for example, there's no "int i = 3"; you just do "i = 3".
  6. 20:33 < onpon4> Another interesting thing about Python's variables is pretty much anything can be stored in them; other than numbers, strings, and regular objects, functions can also be
  7.                 stored in them, for example.
  8. 20:35 < onpon4> Python's basic data types are integers (e.g. 42), which are of the class int; floats (e.g. 4.2), which are of the class float; strings (e.g. "abc"), which are of the class
  9.                 str; bytes (e.g. b"abc") which are of the class bytes; lists (e.g. [1,2,3]), which are of the class list; and tuples (e.g. (1,2,3)) which are of the class tuple; and
  10.                 dictionaries (e.g. {"a":3, 4:6}), which are of the class dict.
  11. 20:36 < onpon4> bytes is not something to worry about for the most part; it's basically non-unicode strings.
  12. 20:36 < onpon4> Lists and tuples are kind of like arrays, but multiple types can be stored in them.
  13. 20:36 < onpon4> Lists can be added to, while the contents of a tuple are static.
  14. 20:37 < onpon4> Dictionaries are what is known as an associative array: you decide how to index each value.
  15. 20:37 < Magnemania> Is there a difference between tuples and arrays in lower-level languages?
  16. 20:37 < onpon4> Magnemania: I'm not sure. It would depend on the language. Usually you use arrays more.
  17. 20:38 < onpon4> But while Python has arrays, they're not normally used.
  18. 20:39 < onpon4> An example of dictionaries: you might assign a to {"a":4, "b": 6}. In this case, getting a["b"] would give you 6.
  19. 20:39 < Magnemania> Let's hash them maps, sirrah!
  20. 20:40 < onpon4> Now, on to Python syntax and such.
  21. 20:40 < onpon4> I think a visual is helpful, so I've prepared a basic "hello, world" example: http://pastebin.com/e1DDwkvC
  22. 20:40 < onpon4> The first several lines, starting with "#" characters, are comments.
  23. 20:41 < onpon4> One significant comment, the first line, is the shebang. Shebangs are used by Unix and other POSIX systems to determine how to open the file. You should always include a
  24.                 shebang in Python files that are meant to be run as programs.
  25. 20:42 < onpon4> Below that is an import statement, on line 31. Import statements give you extra functionality from what are called modules. In this case, I've imported the sys module,
  26.                 which is part of the Python standard library.
  27. 20:43 < onpon4> At the bottom, line 38, is a conditional. You will notice that there are no delimiters such as braces; instead, Python uses indentation.
  28. 20:43 < onpon4> It's sort of a pseudo-standard to use 4 spaces, but this is not enforced by the Python interpreter.
  29. 20:44 < onpon4> Also don't forget the colon at the end of the if statement (":").
  30. 20:45 < onpon4> The conditional at line 38 is actually somewhat special: __main__ is a special global variable which is either "__main__" if this file is being executed, or the module name
  31.                 if this file is being imported. We check this so that the main function is only executed when this file is run.
  32. 20:46 < onpon4> At line 34 is a function definition.
  33. 20:46 < onpon4> Unlike some other languages, the function name "main" has no significance in Python; making a "main" function is just a useful practice.
  34. 20:47 < onpon4> We define functions with the def keyword, followed by the function name and a list of arguments.
  35. 20:47 < onpon4> In this case, I actually used a special type of argument, one preceded with "*". This means to take a variable set of arguments and put them into a tuple, called "args" in
  36.                 this case.
  37. 20:48 < onpon4> Conversely, using this syntax when calling a function (such as on line 39) takes a tuple and splits it into several arguments.
  38. 20:48 < onpon4> Any questions about the hello world program at this point?
  39. 20:49 < onpon4> By the way, I completely forgot: if you open IDLE, the standard Python IDE, you enter "interactive mode". Here, you can try out any Python code interactively.
  40. 20:49 < onpon4> If you don't have IDLE, you can open a terminal and run Python without any arguments (e.g. "python3").
  41. 20:50 < onpon4> One useful thing you can do in interactive mode is call the built-in help function, which lets you learn all about built-in functions, keywords, types, and a few topics.
  42. 20:51 < onpon4> You can also learn about standard library modules there.
  43. 20:51 < onpon4> Please see this file next: http://pastebin.com/C5Vna9Ui
  44. 20:52 < onpon4> This file has some examples of class definitions. You can see that you use the "class" keyword, followed by the class name, then a list of parents in parentheses.
  45. 20:52 < onpon4> On the following lines (indented) are definitions of methods (which are like functions, but at the level of objects).
  46. 20:53 < onpon4> Methods have an extra arguments, which is normally "self". This first argument is a reference to the object the method was called from.
  47. 20:54 < onpon4> One other thing I want to point out is docstrings. These are normal strings following either a class header or a function/method header. They are like comments, but are
  48.                 also parsed e.g. by the help function mentioned earlier.
  49. 20:54 < onpon4> Right, forgot this: negation is with the "not" operator, and there's also the "and" operator and "or" operator.
  50. 20:55 < onpon4> Now let's talk about loops. There's two basic kinds: while loops and for loops.
  51. 20:55 < onpon4> While loops are pretty simple, e.g. "while myvar:", and then the code block (indented, of course).
  52. 20:56 < onpon4> For loops are demonstrated in the Classes file, line 53 nd 65.
  53. 20:56 < onpon4> For loops in Python are not like For loops in C, but rather like so-called "for each" loops; they loop through a list or tuple.
  54. 20:57 < onpon4> You can get a C-like for loop with the built-in range function, as demonstrated in line 53.
  55. 20:58 < onpon4> And with that, I think I've covered all the basics. Probably should have made this longer, that was too fast. Any questions before I move on to the next workshop?
  56. 20:58 < Magnemania> Where does i start?  0?  1?
  57. 20:58 < onpon4> Ah, it starts at 0 by default.
  58. 20:58 < onpon4> The arguments are start, end, count (or something). Only end is required.
  59. 20:59 < onpon4> Note: range excludes end, so range(4) produces (0,1,2,3).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top