Python for Newbies

download Python for Newbies

of 18

Transcript of Python for Newbies

  • 8/16/2019 Python for Newbies

    1/18

    Python for Newbies

    by Pupp3tM, a.k.a. David Borowitz

    Table of Contents

    1. Introductiona. What is Python?

    b. The Interpreter2. Your First Programa. Hello, W orld!b. Explanation

    3. Variables and Matha. Variables: Strings, Numbers, etc.b. Math and Operators

    4. Input/Outputa. Printing informationb. Interacting with the Use r

    5. Program Controla. What If...b. For Ever...c. While We...and Others

    6. Intermediate Python

    a.  Tuples!b.  Strings and Slice Indexingc. File I/O

    7. Modulesa. Overview and Importingb.  Builtin Modulesc.  User-Defined Modules and Functions

    8. In Closinga. Why Python is Better than Perlb.  References

    Introduction

    What is Python?

    Python is, in short, a scripting language. It is similar in function to Perl, but to my knowledge is not nearly aspopular. To fix that is part of the goal of this tutorial. It is a very high-level language that can performcomplex tasks, but is surprisingly easy to learn. Many different add=ons (modules) are available to controleverything from MP3s to windowing toolkits (Unix). I find Python to be a just plain fun language to programin, as it is very intuitive and suited to a variety of uses.

    Python can be run on almost any platform, from 'nix to Windows. To get Python, first go to Python.org, andthen download away!

    This tutorial is geared towards people who have little experience with programming but know at least

    something. I make numerous references to languages like C and Perl, which are good to know, but you won'tlose much if you just skip over them.

    What's with the funky name, you ask? Some sort of carnivorous reptile? No, dear reader. Python's creatorswere (are) big Monty Python fans. The name comes from the BBC show "Monty Python's Flying Circus," and inthe official docs, it says "Making references to Monty Python skits in documentation is not only allowed, it isencouraged." That said, I'm afraid I haven't seen all too many Monty Python movies so we'll go a little lighton the references :)

    So sit back, grab a Coke, download Python, and get ready to learn :)

    Back to top

    The Interpreter

    Python is a scripted, i.e. interpreted, language. Unlike C, which has a compiler, we have in Python the...(drumroll please)...Python interpreter. Once you have Python correctly insta lled, in your Unix shell o r Dos box type'python' (Note: anytime I tell you to type something and put it in quotes, don't type the quotes. I'm sure you

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo1 / 18

    http://www.python.org/download/http://www.python.org/mailto:[email protected]

  • 8/16/2019 Python for Newbies

    2/18

    already figured this out, but ...). If you've used perl or some other interpreted language be fore, you mightnotice something: there's a prompt (those three little ">>>" things)! Not waiting for stdin or saying "Usage:python "! That's right, Python has an interactive mode. In this mode, you can type any Pythoncommand and it will work just like you typed it from a script, with a few differences. Most importantly, if youtype a variable of some sort or something that returns a value of some sort (except assignments), it will printthe result automatically. Neat for trying stuff out (and learning!). Now type '1 + 1' at the prompt you shouldstill be a t. What do you see? 2! Now ain't that a powerful language:

    >>> 1 + 1

    2

    You can do this with any kind of variable or math expression (see part 3b) and get some output. Works withstrings too:

    >>> "elp! I'm being oppressed"

    "elp! I'm being oppressed"

    Note the bad Monty Python reference :). Anyway, that's all for the interpreter. Now we get into the meat of things. If at any point there's a sample script and you don't feel like creating a new file for it, feel free to type(or better yet copy & paste) it into the interpreter, it should work the same. Wait, scratch that :). If I putsomething in a script, it most likely means you only get the effect from a script and it won't work ininteractive mode. Sorry.

    Wait! One note before we leave. To quit the interpreter, you can't do anything fancy like type 'quit'. For somereason (beats me), that doesn't work. You have to type Ctrl+D to quit (on Unix at least, haven't tested it onWindows). Anyway if you type 'quit' it'll say something like 'Use Ctrl-D (i.e. EOF) to exit.' so listen to that.

    Back to top

    Your First Program

    Hello, World!

    Alright, if you've ever read a programming tutorial before you know they a ll have some sort of program that just prints "He llo, World!" or something on the screen. Unfortunately, this tutorial is no different. A lright , here

    goes:#!/usr/bin/python

    #You only need the above line for Unix; replace that with your path to python

    hello = "Hello, World!"

    print hello

    That's it! Put that in a text file, name it hello.py, run it, and we're good. On Unix you may have to 'chmod +xhello.py' to run it, or just type 'python hello.py'. Windows, I'm pretty sure .py files are associated with thePython interpreter automatically.

    Back to top

    Explanation

    "Wow," you may be saying, "that was easy." Indeed it was. Now, though, you probably only have someremote clue of what's going on. We'll take it line by line.

    #!/usr/bin/python

    This line, like you should've read, is only needed for Unix users. It tells your shell where the pythonexecutable is so it can run the script.

    #You only need the above line for Unix; replace that with your path to python

    A pretty useless line, you may think, but it illustrates an important point: comments! Any good programmerknows that you need comments in your code, so just put # and anything after that on that line is a commentand will be ignored by the interpreter.

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo2 / 18

  • 8/16/2019 Python for Newbies

    3/18

    hello = "Hello, World!"

    I didn't really need this line, but I thought a Hello, World! script with only one line is kinda lame, so I addedit anyway. hello is a string variable, which we'll talk about later, and the = operator assigns "Hello, World!" asits value. I'm sure if you've programmed before you've seen all this.

    print hello

    This last line, as you may well guess, prints the contents of hello. If you're a perl guy (or girl :), you need toknow that if you're printing a variable you can't put the name of the variable in quotes. But that's not too

    inconvenient, as we should see later.

    Alright! Our first program! Now just a note or two be fore we move on. First, you may notice that there'snothing on the end of any lines, no ; like in perl or C. That's one of the convenient features of Python. I thinkit's more intuitive because blocks (later dammit!) are determined by indenting and every statement has itsown line...you may disagree I guess but you'll just have to get used to it.

    Back to top

    Variables and Math

    Variables: Strings, Numbers, etc.

    In Python, just like any other language worth its salt, there are a number of data types, Mainly, there arenumbers (of arbitrary precision), strings, lis ts, and tuples . Tuples are more of an "advanced" variable type,kind of like lists, so I won't go into them here, but I'll touch on them a bit in the Intermediate Python section.

    Numbers. What can I say? Numbers are numbers. To assign them, just like you did with the string in HelloWorld!, just put the name of your variable, the = operator, and what you want it to be:

    >>> a = 5

    >>>

    A word on variable names: I'm not quite sure exactly how long Python allows them to be (should I know?), butas a general rule you shouldn't have names more than 20 or so characters. Like other languages, variablenames must be all alphanumeric characters and underscores (_), but you can start names with underscores,unlike some languages.

    Anyway, on to st rings. Strings store, obviously, st rings of characters. Nothing you haven't heard before. Forstrings, you can use either type of quotes ("" or ''), but be sure you know which kind. If you want t o use, say,an apostrophe inside a string, you need to make sure of some things. If the string has "" quotes, don't worryabout it, just put your apostraph. If you were in '' quotes, though, you would need to escape the apostrophe.This is so the interpreter doesn't think the apostrophe, which is just like a quote, is the end of your string. Toescape the apostrophe, just put a \ (backslash) in front of the quote. Try it! It's great:

    >>> "I'm Dave"

    "I'm Dave"

    >>> 'Python\'s my favorite language'

    "Python's my favorite language"

    >>> "\"I can't think of anything else to say!\" he said"

    '"I can\'t think of anything else to say!" he said'

    >>> 'That's too bad'

      File "", line 1

      'That's too bad'

      ^

    SyntaxError: invalid syntax

    Note the las t two examples. The first one illustrates escaping of the "" quotes and also this: If you have bothtypes of quotes in a string, Python will default to one and escape the other. Don't worry; if you're printing thisto the screen it won't show the \ character in your output. In the last example, that's what happens when youdon't escape quotes. You may notice Python has a nice way of handling errors, but we won't go into that.

    Speaking of escaping, here are a few more characters you can escape (if it says 'n' then you use '\n' to escape

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo3 / 18

  • 8/16/2019 Python for Newbies

    4/18

    it):

    n - Prints a newline, i.e. starts a new line.t - Horizontal tabb - Backslash. Deletes the last character typeda - System beep\ - Prints a backslashr - Prints a carriage return. Use newline unle ss you know what you're doing.

    Of course there are more, but you won't use them too often.

    With strings, you can also concatenate them (stick them together) with the + operator. If you have two literalstrings (i.e. not functions that return a string), you don't even need an operator:

    >>> 'holy' + 'grail'

    'holygrail'

    >>> 'BS' 'RF'

    BSRF

    Next come lists. Lists are called arrays in some languages, but it's all the same (well, not quite...). A list is abunch of values grouped together in brackets []. Lists can contain any number of other variables, even s tringsand numbers in the same list, but no tuples or nested lists. Assigning lists is simple:

    >>> a = [1, "thin", "mint", 0]

    >>> a

    [1, 'thin', 'mint', 0]

    Lists can be accessed simply by name, but you can access any variable in a list with its index by putting theindex number in brackets right after the name. Note that index numbering starts at 0:

    >>> a[0]

    1

    You can also use negative index numbers to start counting from the end of the list:

    >>> a[-1]

    0

    That's all for indexes for now, but in the Intermediate string section we get to learn about fun things like sliceindexes to access more than one element in a list. You can also use your masterful index-calling knowledge instrings. Characters in a string act just like elements in a list:

    >>> a = "BSRF"

    >>> a[1]

    S

    Be warned though: You can replace individual elements in a list, but not in a string:>>> a = [1, "thin", "mint", 0]

    >>> b = "BSRF"

    >>> a[3] = 4

    >>> a

    [1, 'thin', 'mint', 4]

    >>> b[1] = "M"

    Traceback (innermost last):

      File "Traceback (innermost last):

      File "", line 1, in ?

    TypeError: object doesn't support item assignment", line 1, in ?

    That's all folks! For data types at least...

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo4 / 18

  • 8/16/2019 Python for Newbies

    5/18

    Back to top

    Math and Operators

    There are a few basic math operators in Python: +, -, *, /, **, and %. If you went to any school at all youknow what +, -, *, and / do. That leaves ** and %. ** is your exponentiatingneniating operator:

    >>> 2 ** 6

    64

    Likewise % is your standard modulus operator. Modulus? Yeah. In case you don't know, the % operatordivides the first number by the second number and gives the remainder. Observe:

    >>> 4 % 2

    0

    >>> 10 % 3

    1

    >>> 21 % 6

    3

    You may have noticed in the course of trying out some of this stuff that if you divide, say, 1 by 3, you get 0.This is because Python always rounds down when working with integers. To work in floating-point numbers,

     just put a . (decimal point) in your express ion somewhere:

    >>> #Bad

    ... 1/3

    0

    >>> #Good

    ... 1./3

    0.333333333333

    >>> #Good also

    ... 1/3.

    0.333333333333

    In addition to your basic math operators, there are comparison operators in Python as well (what did youexpect?). In the a couple more sections we'll learn how to use them in context, but here they are anyway:

    a == b checks if two a is equal to b (a and b can be any data type, not just numbers)a > b checks if a is greater than b (see parentheses above)

    a < b checks if a is less than b (etc.)a >= b checks if a is greater than or equal to ba >> #A return value of 1 means true, 0 means false

    ... [1, 2, 3, 4] > [1, 2, 3]

    1

    >>> [1, 2, 4] > [1, 2, 3]

    1

    >>> [1, 2, 3] < [1, 3, 2]

    1

    >>> [1, 2, -1] > [1, 2]

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo5 / 18

  • 8/16/2019 Python for Newbies

    6/18

    1

    Also notice how list comparison is performed. The first values are compared. If they're equal, the next valuesare compared. If those two are equal, the next values are compared. This continues until the value in one isnot equal to the value in the other (if all are equal then the lists are equal). Look at the third example: eventhough the 3rd value (index 2, remember) of the first list (3) is greater than the third of the second (2), thesecond list is still greater than the first because the interpreter never got that far; it found that the secondnumber in the second list (3) was greater than in the first list (2), so it stopped (confused? That's Ok). If theinterpreter gets to a null value (i.e. there are no items left in the list), then the longer list is greater (see 4thexample above).

    Last thing on operators: logical ope rators. and, or, not (there's no xor, unlike perl). and returns the last valueencountered if true, 0 if false. or is the same, but it's true if only one of the arguments is non-zero (or non-null for strings). Finally, not returns 1 if the single argument is ze ro (null) or 0 if the argument is non-zero.Observe:

    >>> 1 and 4

    4

    >>> 2 or 0

    2

    >>> 'monty' and 'python'

    'python'

    >>> not 0

    1

    >>> not ''

    1

    Back to top

    Input/Output

    Printing information

    In our Hello, World! example above, we already saw one way of printing output, namely the (self-explanatory)print statement. print works pretty much like it does in pe rl, printing a string or any sort of variable orexpression out on the screen. Anything you enter with print automatically has a newline, "\n", appended to it.

    >>> print "She's a witch!"

    She's a witch!

    >>> print 5 + 3

    8

    >>> a = "Burn her!"

    >>> print a

    Burn her!

    >>> #Don't worry if you don't know what this does; it's covered in the Intermediate section

    ... print "Yeah, b" + a[1:]

    Yeah, burn her!

    If you don't want a newline appended, as is sometimes the case, simply add a ',' to the end of the line withyour print statement. Note that this only works in non-interactive mode, so you need to make a new file.py orsomething. Try this on for size:

    #!/usr/bin/python

    a = 5

    print "The value of a is",

    print a,

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo6 / 18

  • 8/16/2019 Python for Newbies

    7/18

    print "!"

    Output:

    The value of a is 5 !

    There's a few more ways you can put (non-string) variables in a print statement. You can use a comma, whichwill put a space around the variable. The final main way is to convert your variable to a string first, which isdone simply by putting it in some backwards quotes (``). Then just append this to your string like you would

    any other string variable or value (see above). Check out these examples:>>> a=5

    >>> #Note the spacing around the number

    ... print "A duck costs $", a, "."

    A duck costs $ 5 .

    >>> #Same thing, different way of putting it in

    ... print "A duck costs $" + `a` + "."

    A duck costs $5.

    For those of you who know what stderr is (or stdout and stdin for that matter), yes, you can access it directlyin Python. This does however use a bit more of an advanced technique, so to really understand what's goingon here you should read File I/O and Modules. Anyway, try out this script:

    #!/usr/bin/python

    import sys #Didn't I tell you to read about modules?

    sys.stderr.write( "This is some error text\n" ) #You do need a \n here since

    #you're writing directly to the file descriptor (READ FILE I/O)

    Run it, it looks more or less normal, just prints "This is some error text". Now try running it as "python tes t.py

    > /dev/null" (Unix) or "test.py > nul" (Dos/Win). What's this? The output is still there, even though youredirected it to /dev/null or nul, the wastebasket of output. Instead of writing to the standard output (stdout),which is what print does, here you wrote to the standard error (stderr), which is separate from stdout (eventhough they appear on the same screen) and informs you of any errors your program might encounter.

    Back to top

    Interacting with the User

    Alright, I admit it, this title is a bit too broad. In this section I'm just covering how to input variables from theuser, not like catching keypresses or anything. Anyway, inputting from the, er, standard input is done with twosimple functions in Python: input and raw_input. I'll discuss raw_input first 'cause it's simpler to use.

    raw_input takes one argument, which is the prompt that'll come up while it waits for the user to typesomething. It returns the string that is typed, so it is most useful in assignments. A simple example:

    >>> a = raw_input( "Type something: " )

    Type something: Dave

    >>> a

    'Dave'

    Don't be too confused. I typed in 'Dave', and raw_input assigned it to the variable a. "That's all well andgood," you say, "but what if I don't want to input a string? What if I want a number or list?" Well, there arereally about 3 ways. First and simplest is the input function, which you'll see if you just scroll down a bit.Another is string.atoi, which although a bit more advanced, adds some flexibility to the input process,

    although it only changes strings to ints (Subliminal message: Modules). I won't go into too much detail, buthere's a simple example of atoi:

    >>> from string import atoi

    >>> a = "5"

    >>> a

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo7 / 18

  • 8/16/2019 Python for Newbies

    8/18

    '5'

    >>> a = atoi(a)

    >>> a

    5

    >>> #If you have Python 2.0 or higher, just do this

    ... a = "5"

    >>> a = int(a)

    >>> a

    5

    Wow, a was converted from '5' to 5. Now let's move on. If you want to input any type of variable at all, like Isaid before, you want to use input. Input works just like raw_input, except Python interprets the variables aswhatever is typed in. If that didn't make much sense, think of it this way: If you have a straight-up number,it's stored as a number. If you have some values in [] brackets, it's stored as a list. You can even putsomething in quotes to have a string. Check it out:

    >>> a = input( "Enter something: " )

    Enter something: 5

    >>> a

    5

    >>> a = input( "Enter something: " )

    Enter something: [3, 6, 'Hello']

    >>> a

    [3, 6, 'Hello']

    >>> a = input( "Enter something: " )

    Enter something: Hello

    Traceback (most recent call last):

      File "", line 1, in ?

      File "", line 0, in ?

    NameError: There is no variable named 'Hello'

    Uh oh! What happened there? Well, the one pitfall of input is that it looks at everything as some sort of variable; it thought Hello was the name of a variable, so it screwed up when it found out there was no Hello.There are a few ways to get around this, like exception catching, but that's beyond the scope of this tutorial.For now, you'll have to trust your user to input the type of variable you want (I know, I know, you usuallyshouldn't trust your users, but...). Onward, onward.

    Back to top

    Program Control

    What If...

    If you've ever  programmed before, in something from Basic to Flash, you've seen if blocks. Hell, I don't evenknow why I devoted a whole section to them, since all I have to do is show you the structure, right? Alright,remember those comparison operators you used before? Good. Take a gander at this example code:

    >>> name = raw_input( "What's your name? " )

    What's your name? David

    >>> if name < "Dave":

    ... print "Your name comes before mine." #That's a tab there

    ... elif name > "Dave":

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo8 / 18

  • 8/16/2019 Python for Newbies

    9/18

    ... print "Your name comes after mine."

    ... else:

    ... print "Hey, we have the same name!"

    ...

    Your name comes after mine.

    Lame example, I know, but it gets the point across. Get over it. Now here you might notice a few things

    different than other languages you know (you might not, what can I say). We'll try to keep it in some sort of order too. First, you see the if, then a conditional statement. The conditional statement is just like we sawearlier, one expression and an operator and another expression. You could also just have an expression, andif'll see it as true if the expression evaluates to non-zero. But anyway. Next comes a colon, then a newline.You need this newline. On the next line or two or three or however many, the code is indented. One space,four spaces, eight spaces, it doesn't matter, it just has to be indented. Depending on how messy yougenerally code, this may be a convenient, code=beautifying feature or the code block from hell. I personallylike it. The next line has a cryptic-looking statement elif. First notice it's not indented; it's not in the samecode block. elif is not life backwards, it's short for else if. If the first condition isn't true, it checks the nextone. Next block, there we go. Finally comes else: the fallback statement, in case none of the above conditions(there can be as many as you want, which serves as a replacement for C-style switch statements) are met.

    There it is, if in a nutshell.

    Back to top

    For Ever...

    Wow, don't I come up with the worst section titles? Anyway, on to the next standard loop: the for loop.Python's for loop is probably a bit different from those you've seen, unless you've seen perl's or PHP's foreachloop. for loops in Python run through each item in a string or list and assign it to a particular, specifiedvariable. The syntax is intuitive and might even resemble human speech. Watch and learn:

    >>> for letter in "Brian": #Blocks are treated the same way as above

    ... print letter

    ... #This just prints each letter in "Brian" on its own line

    ...

    B

    r

    i

    a

    n

    Very simple. But you might say this odd for loop is lacking because it doesn't go through, for example, thenumbers from 1 to 10 and put them in i, like in *other* languages. That is easily overcome with theubiquitously useful range() function. range() will return a list , perfect for running through with fo r, of numbers

    in a specified range (now where do those Python creators come up with those names?). You can specify one of a number of arguments, as follows:

    >>> #This returns a list of 6 numbers, starting at 0

    ... range(6) #which means the argument you put there is not actually in the list.

    [0, 1, 2, 3, 4, 5]

    >>> #A list from 6 to 10-1, or 6-9.

    ... range(6,10) Remember 10 doesn't actually appear, just like above.

    [6, 7, 8, 9]

    >>> range(6,10,2) #Same as above, only increments by 2.

    [6, 8]

    >>> range(10,6,-1) #Negative increments work too. The last number, 6, still doesn't appear.

    [10, 9, 8, 7]

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo9 / 18

  • 8/16/2019 Python for Newbies

    10/18

    Those should have been pretty much self-explanatory. Another little trick, if you want to iterate through theelements in a list by index number, is to use the len() function (which returns the length of the list) togetherwith the range() function:

    >>> l = ["I'm", "not", "dead"]

    >>> for i in range(len(l)):

    ... print "Item", i, "of l is "+l[i]

    ...

    Item 0 of l is I'm

    Item 1 of l is not

    Item 2 of l is dead

    Well, that about wraps it up for for loops. Remember: if you can't come to grips with for really being aforeach, range() is your friend.

    Back to top

    While We...and Others

    I'm not even going to say  anything about this title. Moving on, then, to the last of the ever-so-common loops:while. while performs its little block of code forever and ever, or at least until some predefined condition ismet.

    >>> while name != "Dave"

    ... name = raw_input( "What's my name? " )

    ...

    What's my name? David

    What's my name? Monty

    What's my name? Dave

    >>>

    Notice how it obstinately refuses to let me go on until I give it what it's looking for. Of course, while loopsneed not be dependent on user input. This loop here will calculate every cube that's under 1000:

    >>> c = 0

    >>> while c**3

  • 8/16/2019 Python for Newbies

    11/18

    and go on with the next iteration. else clauses, which can be used on any loop (not just if), specify code toexecute after the loop is finished, e.g. in a for loop there are no more items to iterate through. Look at thisslightly-modified-but-still-cheesy example:

    >>> for i in range(3):

    ... name = raw_input( "What's my name? " )

    ... if name != "Dave":

    ... print "Wrong!"

    ... continue

    ... else:

    ... print "Damn straight!"

    ... break

    ... else:

    ... print "Too many guesses! You're wrong!"

    ...

    What's my name? David

    Wrong!

    What's my name? David

    Wrong!

    What's my name? David

    Wrong!

    Too many guesses! You're wrong!

    >>> #Try it again, but I won't type the code here again

    What's my name? Dave

    Damn straight!

    Well, so much for control structures...

    Back to top

    Intermediate Python

    Tuples!

    Well, here we are, at the much-hyped tuple section. Tuples are another type of variable that resemble listsbut are more general and have a wider range of features. They are usually put in parentheses (), butsometimes parentheses aren't even necessary to assign them:

    >>> t = (1, 2, 3) #Wow, our first tuple :)

    >>> t

    (1, 2, 3)

    >>> t + 1, 2, 3 #Who needs parentheses? Well most of the time you do...

    >>> t

    (1, 2, 3)

    >>> t[0] #Still use brackets to get indexes

    1

    >>> t = 1, #You need the trailing comma, or else t is just an int

    (1)

    Great, you say, but what makes these "tuples" more special than any old lists? Well, for one thing, tuples canbe nested in one another:

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo11 / 18

  • 8/16/2019 Python for Newbies

    12/18

    >>> tu = tuple(range(1,4)) #Note use of tuple function

    >>> ple = tuple(range(4,7))

    >>> tuples = tu, ple

    >>> tuples

    ((1, 2, 3), (4, 5, 6))

    >>> print tuples[1][1] #Access it like this

    5

    Nesting tuples together like this can provide a substitute for things like two-dimensional arrays (arrays of arrays) in C without all that pesky pointing. Also notice, just for convenience, the tuple() function. This justconverts any old list into any old tuple. Its opposite, the list() function, also exists. There's another little niftyfeature of tuples that bears talking about: packing and unpacking. Packing a tuple is basically what we'veseen above, putting two or more variables in a tuple just with commas. Unpacking can also be useful. Try tosee how this works:

    >>> n = tuple(range(1,6))

    >>> a1, a2, a3, a4, a5 = n

    >>> print a1, a2, a3, a4, a5

    1 2 3 4 5

    In case you didn't quite understand that, what the second line did was take each member of the tuple n andassign it to a different variable, a1 through a5. This unpacking, just so you know, also works with lists. Justput the list of variables in list brackets []. Note how these two examples do the same thing:

    >>> l = range(1,4)

    >>> [l1, l2, l3] = l

    >>> t1, t2, t3 = tuple(l)

    >>> print l1, l2, l3, "\n", t1, t2, t3

    1 2 3

    1 2 3

    When you're unpacking a bunch of tuples, just remember that the number of variables in your list has to equalthe length of the tuple. There is one more interesting opportunity afforded by tuple packing and unpacking,namely multiple assignment. It may seem like this has nothing whatsoever to do with tuples, but look now,I'll explain later:

    >>> a, b = 0, 1

    >>> a

    0

    >>> b

    1

    Look at that, a and b assigned different values in the same line! Very useful, very simple. Now, since this isthe Intermediate section, an explanation (rather simple though) is in order. First look on the right: the values0 and 1 are packed into a "temporary" tuple. which I'll call t but I'm not sure really has a name. Now on theleft. a and b, since they are a comma-separated sequence, are about to receive an unpacked tuple. This tupleis t. Just like normal unpacking, the first element of t goes in a and the second in b. Voila! There's yourmultiple ass ignment.

    Back to top

    Strings and Slice Indexing

    Remember waaayyy back up somewhere where I had a string indexed like a[1:]? Well, that's a simple exampleof slice indexing. With slice indexing you can take more than just one element of a string (or list or tuple, butit's usually more useful with strings), in fact you can take a whole slice out of it (hence the name). Just puttwo numbers, the starting and ending indices, inside your brackets. Observe:

    >>> s = 'supercalifragalisticexpialidotious'

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo12 / 18

  • 8/16/2019 Python for Newbies

    13/18

    >>> s[0:5]

    'super'

    Wait, that's not right! Element 5 of s is 'c', not 'r'! Well, I lied before. The second number is one more thanthe highest index you want. (Remember range()? It's just like that.) Another one of those things you'll justhave to deal with, I guess.

    Now, what about that example I gave earlier? There was no second index, right? Precisely. There are of coursedefault indexes for before and after the :. Before the colon defaults to 0, after defaults to len(s), or the

    length of the string:

    >>> s = 'supercalifragalisticexpialidotious'

    >>> s[:5]

    'super'

    >>> s[27:]

    'dotious'

    You may think it inconvenient to have to count all the way to the 27th letter of s in order to get dotious. Sayyou just wanted the last 7 letters. Remember I said negative index numbers work too? Well they do here, too,with slicing. Just make sure the beginning index comes before the ending (you'll see what I mean):

    >>> s = 'supercalifragalisticexpialidotious'

    >>> s[-7:]

    'dotious'

    >>> s[int(-.5*len(s)):len(s)-5] #Indices can be any int expressions

    'ticexpialido'

    >>> s[:] #A plain colon is the same as no colon

    'supercalifragalisticexpialidotious'

    >>> s[:10]+s[10:] #As a rule of thumb, s[:n]+s[n:]=s

    'supercalifragalisticexpialidotious'

    >>> s[-5:5]

    ''

    See what happens when the indices don't match up? No string. Hey, it's better than an error, but still notwhat you want. Moving on, then, why would I devote an entire section to slice indexing? Well, I think it'spretty damn neat. It also lets you splice (not slice, splice) together strings in weird and sometimes cool (notto mention useful) ways. Look at this non-useful way:

    >>> #Mixes two names together. You could make this more sophisticated, like making sure

    ... #a vowel in the first name is followed by a consonant in the second.

    ... name1 = raw_input( "Type a name: " )

    Type a name: Dave

    >>> name2 = raw_input( "Type another name: " )

    Type another name: Guido

    >>> print name1[:3]+name2[-3:]

    Davido

    Of course, I'm sure you'll find much more practical uses for splicing and slicing, but hey, I got ta show you howit works first, don't I?

    Back to top

    File I/O

    How can you have a programming language without a way to access other files on the system? I really don't

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo13 / 18

  • 8/16/2019 Python for Newbies

    14/18

    know, but that's alright since Python can do it. Well, then you're probably wondering how. Basically, first youneed to open the file and assign it t o an object. Object? We haven't talked about those, have we? Objectsare like structures (more properly classes) in C, record in some other languages. Basical ly they are a data typethat is made up of other data types combined, including (and in some cases especially) functions. File handles(variables that refer to a file to be opened) are, predictably, of the file object type. You assign a file handleusing the open function, like this:

    >>> f = open( "myfile.txt", "r" )

    >>> f #This will vary

    I hope you know what myfile.txt is (hint: the file to be opened). The "r" part is a mode for opening the file.The mode you open the file in tells Python whether you're going to read data from the file, write data to thefile, etc. Here's a list of the modes you can use:

    r - Readingw - Writinga - Appending (same as writing, only any new data is written at the end of the file)r+ - Reading and writingNo argument - Reading

    Note that Python won't give you an error if you specify some mode other than these. Be warned that it mightmake some unexpected things happen later on, though. In Windows and MacOS there is also a binary mode,"b", which is important but a bit confusing. Binary mode treats al l characters as normal characters, includingnewlines. The other modes, as we shall soon see, can rely on newlines to separate or find data. The first of these newline-using methods is readline. To access the readline method for a file object, put 'readline()' afterthe object's name and a period. This returns a string containing the next line in a file. Also available isreadlines, which reads all the lines of a file into a list. readlines can take an optional argument that specifiesthe most bytes it should read from the file, plus whatever it needs to reach the end of a line. One more thing:readline (or readlines) returns an empty string '' if the end of the file has been reached. Finally, an example:

    >>> f.readlines() #Our imaginary file; create it if you want

    ['This is the first line\012', 'This is the second line\012', "Hmm I'm running out

    of creative things to say\012", 'Alright, this is the last line']

    >>> f.seek(0) #You need this to go back to the beginning of the file; see below

    >>> f.readline()

    'This is the first line\012'

    Just in case you're wondering, the \012 at the end of every line is just a newline. When reading files, Pythonreplaces simple \ characters like \n with their hex (ASCII) code. If you didn't get that , just remember that\012's are really \n's. There are three (maybe four) more important methods of files . First, read and s eek. read

     just reads the enti re fi le into a st ring, o r a maximum number of bytes if you specify it. seek needs oneargument, which is the offset in the file to go to, or seek. Huh? Well, Python reads files by putting a littlepointer in one and moving it along. When you read from a file, the little pointer moves along to the end of thepart you read. If you read again, it'll start from where the pointer left off. seek will tell the pointer to go back(or forward) to a specified point in the file. Here's another example, using the same imaginary file as above:

    >>> f.seek(12) #Offsets start at 0, therefore 12 is 'f'

    >>> f.read(10)

    'first line'

    >>> f.seek(0)

    >>> f.read() #Whole file. You probably shouldn't do this with big files.

    "This is the first line\012This is the second line\012Hmm I'm running out

    of creative things to say\012Alright, this is the last line"

    Then, finally, there is the write function, which (surprise!) writes to your file. Just put in the parentheses thestring you want to write. Be sure you have a file opened in mode w, r+, or a, though! Also, if you want tocreate a new file you must use the mode w or a, not r+. The last method is close, which closes the file andfrees up any memory it's been using. It's a lways good programming practice to close any files when you'redone with them or before you leave. One last example for this section, then we move on:

    >>> f = open( "mynewfile.txt", "w" )

    >>> f.write( "This is some text" )

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo14 / 18

  • 8/16/2019 Python for Newbies

    15/18

    >>> f.close()

    >>> f.read() #Oops! Can't read a closed file (or a write-only file for that matter)

    Traceback (most recent call last):

      File "", line 1, in ?

    ValueError: I/O operation on closed file

    >>> f = open( "mynewfile.txt" )

    >>> f.read()

    'This is some text'

    Back to top

    Modules

    Overview and Importing

    Before we talk about Python's wide variety of builtin modules, let's go over what a module is first and how toget to them. A module is a script (or compiled library) that can be loaded into the interpreter environmentseparately from the core language of Python. Modules are accessed with the import statement, which can beused in a number of ways:

    >>> import sys #For these examples we're using the builtin sys module

    >>> sys.argv #Access names from the modules like you would an object

    ['']

    >>> from sys import argv #Another way to call it

    >>> argv

    ['']

    >>> from sys import * #Get everything in the sys module

    >>> stdout

    Confusing? Not really. Let's go through it. The first line imports the module sys (one of the builtin modules).Notice on the next line that we needed to call a member of sys, argv (the arguments passed to theinterpreter), with a . like we did for objects. This is using the so-called sys namespace, i.e. all sys membersare accessed using, obviously, the name 'sys'. Alternatively, if we put 'from import ',we use the global namespace. Then you can access the method's members just like any other sort of localvariables or base functions. If you want to use the whole module in the global namespace, just do a 'from import *'. Not too ha rd, eh?

    Back to top

    Builtin Modules

    Python, being the versatile language it is, has a large selection of modules built into the default distribution.And I do mean large: at my count, there are 196 modules available in Python 2.0 (including obsolete and OS-specific ones). When I say "builtin," this doesn't mean you don't have to import them, just that they shouldbe included in Python installation. So, I'll list a choice 5 or so here, with basic descriptions of what they doand only a few of their members.

    sys - Bas ic system/program functionsargv - List of commands passed to the interpreterstdout, stdin, stderr - Basic standard output, standard input, and standard error. These can beaccessed like normal files.exit - Exits the program gracefully, accepting a value as the exit code (basically 0=good exit, non-

    0=error)path - The paths Python looks in to find modules to import

    math - Mathematical functions and constants. I won't list them all here, but math contains manyfunctions like sin, acos, tanh, log, etc. Here are the constants:

    pi - The mathematical constant π. The ratio of a circle's diameter to its circumference, approximately3.1415926535897931e - The base of the natural logarithm (ln). Get it with the sum of 1/k! for all numbers k 0->infinity.

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo15 / 18

  • 8/16/2019 Python for Newbies

    16/18

    Oddly enough, factorials (and infinity) are not in the math module.

    string - Functions for more manipulat ions o f stringsprintable - A string of all so-called printable characters. Also available are digits, letters (uppercaseand lowercase), whitespace, and punctuation.atoi, atof, atol - Convert strings to int, float, and long, respectively. In Python 2.0 you should usebuiltins functions int, float, and long.find - Find the lowest index where the second string (argument) appears in the first.count - Count the number of times the second string appears in the first.split - Split the string given in the first argument by whitespace characters; return a list. If a secondargument is given, the string is split according to that instead of whitespace.strip - Strip leading and trailing whitespace from the given string.

    replace - Replace a ll occurrences of the second st ring in the first st ring with the third string. Err,look at i t this way: replace( st r, "old", "new" ) replaces all occurrences of "old" in str with "new".

    randomseed - Seeds the random number generator with three optional arguments; defaults to the currenttimerandom - Returns the next random number as a floating-point number between 0 and 1.randint - Returns a random number between two given integers.uniform - Same as randint, only with floating-point numbers.choice - Randomly chooses an element from the specified list or tuple.

    time - Functions for getting and manipulat ing local and arbitrary times. See the Python librarydocumentation for more complete information.

    time - No a rguments, returns a long number that's your system's current t ime, in s econdssince...some time. Not really useful without some sort of parser, except for seeding randoms.localtime - A parser. Given a t ime (like that returned by the t ime function), it returns a tuple with

    the following information: year (four-digit), month (1-12), day (1-31), hour (0-23), minute (0-59),second (0-61 - 60 & 61 for leap seconds, never used), weekday (0-6, Monday is 0), day (1-366),daylight savings time (0 - no, 1 - yes, -1 - system default). Useful :)gmtime - Same as above, except in Greenwich Mean Time, not your system's local time.asctime - Given a tuple from localtime or gmtime, returns a s tring in the format 'Thu Mar 2218:24:35 2001'.

    re - Regular express ion (regexp) operators. I won't cover them here, but I t hought all you Perl fans outthere might like to know they exist in Python. There are some definite peculiarities about regexps inPython, so read the docs...docs are your friends :).

    Back to top

    User-Defined Modules and Functions

    Of course, what use are all these modules if you can't use your own? Well, a user-defined module is just a .pyfile in one of Python's module directories. To import the module, it's run, and any functions, variables,classes, etc. are imported in. You can specify what to import, just like for any other module. Well, then, that'sall well and good, but a big part of these modules is functions. How to make your own functions, then? Ok.

    Alright. So we want a function. Since I like math, we'll do a simple logarithm to any base using the logfunction in the math module. Here we go, try this:

    >>> def logb( b, n ):

    ... from math import log

    ... a = log(n)/log(b)

    ... if a - int(a) == 0: #a is an int

    ... return int(a)

    ... else:

    ... return a

    ...

    How nice. Only a couple things should need explaining, unless you haven't taken high school algebra (hey nooffense...I'm just a freshman :). This 'def' thing? Well, it's just the name of the function and the arguments ittakes. I should hope you have a good idea of what arguments are now. Then there's return. It's...uh...whatthe function returns :). Execution stops after a return statemnt, just like a break statement. Except it returnssomething. Ok. Well, I've kinda run out of things to say...so here's an example module, just for practice. Hey,

    this is a tutorial, isn't it? Let's go, put this in moremath.py:

    #!/usr/bin/python

    def logb( b, n ): #Definition

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo16 / 18

  • 8/16/2019 Python for Newbies

    17/18

      from math import log #Need this!

      a = log(n)/log(b) #Change of base formula

      if a - int(a) == 0: #a is an int

      return int(a) #Get rid of that decimal :)

      else: #a is a float

      return a #Just return it

    #Ye Olde Golden Ratio

    phi = 1.6180339887498948

    And now in the interpreter:

    >>> import moremath

    >>> moremath.phi #Floating point numbers aren't that accurate, you may notice

    1.6180339887499999

    >>> from moremath import logb

    >>> logb(2,1024)

    10

    Wow! A module! Just like other modules! But you did it myself ! Or I did it yourself...or...wait...you did ityourself. If you look in the di rectory where you put moremath.py now, you'll now find a moremath.pyc file.This is a 'compiled' file, which is not really compiled (it's bytecode - kinda like a Java .class file), but has lessmemory overhead than a .py file. As far as I know .pyc files are not portable, though, so if you're just usingthis module for yourself you can use the .pyc but if you want to distribute it, I recommend you keep the .pyfile. In any case, it's certainly easier to edit your source if you still have a .py file. Any questions :)?

    Back to top

    In Closing

    Why Python is Better than Perl

    I admit it: maybe Python isn't better than Perl. It's different, to say the least. For many things it is  better.And it's got definite advantages, and that's what I thought I'd just review, or look at, or list, or whatever.Here goes.

    Intuitiveness. Instead of all that nasty, obfuscated code, Python has syntax and blocking that resembles,somewhat, normal (English) speech and indenting.High-levelness. List slicing, tuple packing, etc. allow for high-level manipulation of data without all thatmucking about with pointers...wait...that's C...and efficient though it may be, it sometimes evenconfuses the überhacker in me. But it's still easier in Python than Perl.More integrated object-oriented (OO) support. OO in Perl involves working with hashes as classes and

    weird stuff like that. Bottom line, Python was designed from the start to be OO...even though I didn'ttalk about classes too much. Sorry.Better C integration. Well, this is opinionated here, but I think embedding Python in C is easier thanPerl...what can I say?Interactive mode. No need to put scripts in separate files. You know what I'm saying.Wider variety of data structures. Perl has arrays, hashes, and scalars. Python has scalars (int, float,long), lists, dictionaries (which I didn't talk about; they're like hashes or associative arrays), tuples. Andit's extensible and evolving, according to the docs.range function. People complain about the lack of a generic for loop, like Perl has. range really makes thefor loop...without having a whole different type of loop. Less overhead. And you can use range for otherthings, too.Monty Python. 'Nuff said.

    Back to top

    References

    Python.org. A must-see.Current Python docs. Gotta read them.Zope. Great webserver written in Python.

    Python for Newbies 10/22/2014

    file:///C:/Users/Shivam/Downloads/500+%20Hacking%20Tutorials%5BTeam%20Nanban%5D%5BTPB%5D/500+%20Hacking%20Tutorials/500+%20Hacking%20Tutorials/Tutorials%20-%20blacksun.bo17 / 18

    http://www.zope.org/http://www.python.org/doc/current/http://www.python.org/

  • 8/16/2019 Python for Newbies

    18/18

    Python section at P rogrammersHaven. More good links.Black Sun Research Facility. Great sit e for networking, programming, cracking, OSes, etc. This tutorialshould be published there :)Pythonline. Monty Python, may or may not be the official site, I'm not sure.

    Any comments, questions, glowing praise, rants, hatemail, feel free to mail me or leave a message for me atBSRF's message board. I'm a member, I check it regularly. Hatemai l is ok, but death threats are discouraged.

    Well, we've come to the end, at long last. I hope you've learned something, and you aren't too high on theadrenaline rush of coding. I'm not good at these ending things, so I think I'll just trail off right...about...now...

    Back to top

    Python for Newbies 10/22/2014

    http://dvd.box.sk/wb/?did=blacksunmailto:[email protected]://www.pythonline.com/http://blacksun.box.sk/http://www.programmershaven.com/zone23/links/link281.htm