Python Introduction
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Python Introduction

  1. #1
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914

    Python Introduction

    Hey Hey Everyone,

    So you want to learn Python.... Well so do I. I'm writing this tutorial as a learning aid for both you and me. I'm going to pass on describing Python to you and instead let Webopedia do it for me.

    Source: Webopedia Term: Python

    An interpreted, object-oriented programming language developed by Guido van Rossum. The name comes from one of van Rossum's favorite television shows, Monty Python's Flying Circus. Python is very portable since Python interpreters are available for most operating system platforms. Although Python is copyrighted, the source code is open source, and unlike GNU software, it can be commercially re-sold.
    For those of you who do not have a Python interpreter, they are available on the net for download

    Python.org Download Page
    ActivePython from ActiveState

    Now for the tutorial.

    I'll try and make this as OS inspecific as possible. All these walk-throughs assume you have installed Python already.

    Let's start with the most basic of programs. The obvious Hello World! Program.

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    print "Hello World!"
    3. Save the script as helloworld.py
    4. Open a command prompt and type python helloworld.py
    5. Voila! Hello World! is displayed on the screen.

    This simply demonstrates the print command in Python. Now let's look at variables and user input.

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    age = input("What is your age: ")
    	print "So you are",age,"?"
    3. Save the script as age.py
    4. Open a command prompt and type python age.py
    5. You will see an prompt which says What is your age:. Enter your age and press return. You will now see So you are 21 ?.

    You will notice you do not have to declare variables in Python, simply reference them. In this example you also see how the input command is used. To input a value simply define a variable name for the value and then set it equal to the input command. The structure of the input command is
    Code:
    input("Question to prompt with")
    . You also see how to output more than one string of data, using the , to concatenate the strings. However use of the comma also automatically inserts a space. You MUST remember that input only works with numbers

    Now we will attempt a simple text calculator with 4 commands (add, subtract, multiply and divide).

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    calc = raw_input("Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: ")
    firstnum = input("Enter the first number: ")
    secondnum = input("Enter the second number: ")
    if calc == "a" : print firstnum,"+",secondnum,"=",firstnum+secondnum
    elif calc == "s" : print firstnum,"-",secondnum,"=",firstnum-secondnum
    elif calc == "d" : print firstnum,"/",secondnum,"=",firstnum/secondnum
    elif calc == "m" : print firstnum,"*",secondnum,"=",firstnum*secondnum
    else : print "Error: Unknown Function Entered"
    3. Save the script as calc.py
    4. Open a command prompt and type python calc.py
    5. You will see a prompt which says Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: . For the sake of demonstration enter a and press enter. You will then see Enter the first number: . Enter 5 and press enter. Now Enter the second number: " appears. Enter 7 and hit enter. The screen will now display 5 + 7 = 12. Feel free to experiment with other functions and numbers now.

    You have now been introduced to the command raw_input. It fuctions the same way as input but is used for alphanumeric entries. You are also introduced to basic mathematical functions in python (+,-,/,*) and if statements. The syntax for an if statement is
    Code:
     if (statement) : <code>
    elif (statement) : <code>
    else : <code>
    One last example. We will attempt to incorporate a basic introduction to sockets into this example. *Crosses his fingers*

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    from socket import *
    s= socket(AF_INET, SOCK_STREAM)
    yourip = raw_input("Enter your IP Address: ")
    yourport = input("Enter a Port Number to Listen on: ")
    s.bind((yourip, yourport))
    s.listen(1)
    (incomingport, incomingaddress) = s.accept()
    print "Connection Attempt from", incomingaddress
    s.close
    3. Save the script as server.py
    4. Open a command prompt and type python server.py
    5. You will see a prompt which reads Enter your IP Address: . Do as it says and press enter. You will then see a prompt which says Enter a Port Number to Listen on: . Answer the question and press enter. Telnet to the port you entered and wait a few seconds. Close the session and return to your python window. You will see the text Connection Attempt from <your ip, port used>.

    In this example we played with sockets a little bit. The first task was to include the socket module. We did this using the statement
    Code:
    from socket import *
    . We then defined the type of socket (AF_INET, SOCK_STEAM) and created a variable for the socket to be attached to (s). We could then issue our commands based on the varible s. We bound the socket to the ip and port you had previously entered with the bind command and then started listening on that port. Upon a connection attempt we accepted the connection, printed the intruders IP to the screen and closed the socket.


    Anyways... Hope you find this helpful. I'll definately add another one if people want it, probably before the end of the weekend, as I have no plans. Anyways That's all for now, maybe more to come.


    Peace

    HT

    PS - I have proofread this, but I'm sure i've missed stuff. If you notice anything please PM so I can fix it... Thanks.

  2. #2
    Junior Member
    Join Date
    Nov 2001
    Posts
    11
    Wow thats really neat... thanks for that this has made me look into python a little more...

    good post :-)
    A+ Cert. Computer Technician

  3. #3
    Member
    Join Date
    Mar 2003
    Posts
    90
    Good job and thanks, quite helpful for a python newbie like me.
    Look forward to the next one.
    Cheers
    \"Great spirits always encounter strong opposition from mediocre minds.\"
    Albert Einstein

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,786
    Thanks HT I like your “by example” approach!

    I have a question about the libs.

    I assume

    “from socket import *”

    is calling lib functions. Is it like perl with modules which are easy to update (ppm) or are they more like header files…either you have them or you have to go looking for them for your os.

    Where you have “print "Connection Attempt from", incomingaddress”. Could that have just as well been a call to an external i.e. system()

    What advantages does python have over perl?

    You have my interest up so please do another tut.
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  5. #5
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    Originally posted here by Tedob1
    Thanks HT I like your “by example” approach!

    I have a question about the libs.

    I assume

    “from socket import *”

    is calling lib functions. Is it like perl with modules which are easy to update (ppm) or are they more like header files…either you have them or you have to go looking for them for your os.

    Where you have “print "Connection Attempt from", incomingaddress”. Could that have just as well been a call to an external i.e. system()

    What advantages does python have over perl?

    You have my interest up so please do another tut.

    I dunno if python really has any advantages over perl. it'd be something i'd have to research.. my perl knowledge is pretty basic.. but then again so is my python knowledge.

    Yes you could have called an external command. I was just going for simplicity. I'll be sure to include calling applications in my next section....

    as far as the lib functions.. that's exactly what it's doing.. and yeah they are modules.. as for updating them I'm not sure... Never had to do that yet.... import <lib name> also works... Not real sure of the difference, I just know how I learned it.. (which was only last week)

    As i keep learning I'll be sure to kick out more stuff and try and keep all your questions answered.

  6. #6
    Senior Member Maestr0's Avatar
    Join Date
    May 2003
    Posts
    604
    Very nice.

    -Maestr0
    \"If computers are to become smart enough to design their own successors, initiating a process that will lead to God-like omniscience after a number of ever swifter passages from one generation of computers to the next, someone is going to have to write the software that gets the process going, and humans have given absolutely no evidence of being able to write such software.\" -Jaron Lanier

  7. #7
    Doc d00dz Attackin's Avatar
    Join Date
    Mar 2003
    Location
    Florida
    Posts
    661
    I love the way you do your tut's there neat, understanding, and there step-by-step. Keep them 'coming. This is a awesome Tut. A standing ovation goes to HT .
    Cya
    First you listen, then you do, finally you teach.
    Duck Hunting Chat
    VirtualConvenience
    RROD

  8. #8
    Junior Member
    Join Date
    May 2003
    Posts
    12
    Great Thread!!!!!!! You have renewed what I had started to only dabble in last year. Python is my first language I ever attempted to learn. Hats Of From Controverzy226.
    Controvesial way are the nature of us all. Exit Controversy Enter Controverzy226 Newbie With Desire To Be the Best 05-16-2003

  9. #9
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    What advantages does python have over perl?
    As a seasoned perl programmer, I'd have to say

    "python code does not look like you've opened a binary file by accident"



    Seriously though, a lot of perl code really does look like a heap of ****. You can stare at it for hours and still not understand it.

    DOS Edit
    I think it's a fairly safe bet that under DOS, python does not support sockets.

  10. #10
    Junior Member
    Join Date
    May 2003
    Posts
    12
    I was just wondering what the next language I was going to learn should be. Looks like I'll be learning Python next thanks to you, HT.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides