Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 20:29 < onpon4> Alright, it's about time to start.
- 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.
- 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.
- 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.
- 20:32 < onpon4> So, for example, there's no "int i = 3"; you just do "i = 3".
- 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
- stored in them, for example.
- 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
- 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
- dictionaries (e.g. {"a":3, 4:6}), which are of the class dict.
- 20:36 < onpon4> bytes is not something to worry about for the most part; it's basically non-unicode strings.
- 20:36 < onpon4> Lists and tuples are kind of like arrays, but multiple types can be stored in them.
- 20:36 < onpon4> Lists can be added to, while the contents of a tuple are static.
- 20:37 < onpon4> Dictionaries are what is known as an associative array: you decide how to index each value.
- 20:37 < Magnemania> Is there a difference between tuples and arrays in lower-level languages?
- 20:37 < onpon4> Magnemania: I'm not sure. It would depend on the language. Usually you use arrays more.
- 20:38 < onpon4> But while Python has arrays, they're not normally used.
- 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.
- 20:39 < Magnemania> Let's hash them maps, sirrah!
- 20:40 < onpon4> Now, on to Python syntax and such.
- 20:40 < onpon4> I think a visual is helpful, so I've prepared a basic "hello, world" example: http://pastebin.com/e1DDwkvC
- 20:40 < onpon4> The first several lines, starting with "#" characters, are comments.
- 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
- shebang in Python files that are meant to be run as programs.
- 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,
- which is part of the Python standard library.
- 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.
- 20:43 < onpon4> It's sort of a pseudo-standard to use 4 spaces, but this is not enforced by the Python interpreter.
- 20:44 < onpon4> Also don't forget the colon at the end of the if statement (":").
- 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
- if this file is being imported. We check this so that the main function is only executed when this file is run.
- 20:46 < onpon4> At line 34 is a function definition.
- 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.
- 20:47 < onpon4> We define functions with the def keyword, followed by the function name and a list of arguments.
- 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
- this case.
- 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.
- 20:48 < onpon4> Any questions about the hello world program at this point?
- 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.
- 20:49 < onpon4> If you don't have IDLE, you can open a terminal and run Python without any arguments (e.g. "python3").
- 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.
- 20:51 < onpon4> You can also learn about standard library modules there.
- 20:51 < onpon4> Please see this file next: http://pastebin.com/C5Vna9Ui
- 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.
- 20:52 < onpon4> On the following lines (indented) are definitions of methods (which are like functions, but at the level of objects).
- 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.
- 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
- also parsed e.g. by the help function mentioned earlier.
- 20:54 < onpon4> Right, forgot this: negation is with the "not" operator, and there's also the "and" operator and "or" operator.
- 20:55 < onpon4> Now let's talk about loops. There's two basic kinds: while loops and for loops.
- 20:55 < onpon4> While loops are pretty simple, e.g. "while myvar:", and then the code block (indented, of course).
- 20:56 < onpon4> For loops are demonstrated in the Classes file, line 53 nd 65.
- 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.
- 20:57 < onpon4> You can get a C-like for loop with the built-in range function, as demonstrated in line 53.
- 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?
- 20:58 < Magnemania> Where does i start? 0? 1?
- 20:58 < onpon4> Ah, it starts at 0 by default.
- 20:58 < onpon4> The arguments are start, end, count (or something). Only end is required.
- 20:59 < onpon4> Note: range excludes end, so range(4) produces (0,1,2,3).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement