1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Python {TUTORIAL} [Hard to understead, easy to use]

Discussion in 'Code Snippets and Tutorials' started by .aId, Nov 26, 2006.

Thread Status:
Not open for further replies.
  1. .aId

    .aId Level I

    Joined:
    Nov 17, 2006
    Messages:
    119
    Likes Received:
    0
    Location:
    LoserVille
    What is Python?
    Beafore to star, you have to know what is Python.
    Python is a unpopular language.Is often compared to Perl.
    Python, is a scripting languaje,this means it is interpreted rather than compiled, so it saves time during debugging and early development.

    Python is a high level language - this means that you can write big programs very fast and easier than with low level languages like C/C++.



    Installing Python
    First you need to download the program from http://www.python.org/download.


    Runing Python
    If you want to check something simple run the Python interpreter by typing python or following a shortcut (Windows). The interpreter isn’t very useful for large pieces of code so generally you would want to use your favorite text editor to create the scripts and then execute them.

    Coding
    Only one thing before that in other languages statements are separated by braces and such - Python identifies statements by indentation only.


    Code (Text):
    1. #!/usr/bin/env python
    2.  
    3. # This is a comment
    4. # comments can span one line only
    5.  
    6. print 'Hello world!'
    7. print "Hello world!"
    8.  
    You can delimit strings with single or double quotes - it doesn’t make any difference to Python. If you want to write a string on more than one line you can escape the end of the line with a backslash:
    Code (Text):
    1. foo = "I'm a very long string
    2.     which spans multiple lines"
    3. print foo
    4. or use HereDoc syntax with “”" or ‘'’:
    5.  
    6. foo = """I am a really long
    7.     comment but it doesn't
    8.     matter no backslashes are
    9.     needed
    10. """
    11.  
    You can also access directly single characters or slices from a string
    Code (Text):
    1. foo = "Hello"
    2. print foo[1] # outputs "e" since indexing starts at 0
    3. print foo[1:] # ello
    4. print foo[-1:] # o
    5. print foo[2] # ll
    6.  
    Control structures
    Code (Text):
    1. if 1 == 2 :
    2.     print 'One equals two'
    3. else :
    4.     print 'One does not equal two'
    5.  
    As you have noticed no braces are used, just indent, this promotes readable code.
    Code (Text):
    1. if 1 < 2 :
    2.     print 'One is lesser than two'
    3. elif 1 > 2 :
    4.     print 'One is greater than two'
    5.  
    Loops
    Code (Text):
    1. for i in range(0, 10) :
    2.     if i % 2 == 0 :
    3.         print i, 'is an even number'
    4.     else :
    5.         print i, 'is an odd number'
    6.  
    This example will loop through the numbers from 0 to 10 and test if the number is odd or even. The modulus operator (%) is used to calculate the remainder from dividing the number by two.

    The same example can be rewritten also with a while loop.
    Code (Text):
    1. i = 0
    2.  
    3. while i <= 10 :
    4.     if i % 2 == 0 :
    5.         print str(i) + ' is an even number'
    6.     else :
    7.         print str(i) + ' is an odd number'
    8.     i += 1
    9.  
    10. i = 'Hello world'
    11. print i
    12.  
    This example also shows that even though Python’s variables are loosely typed (you can assign any type of data to a single variable), you have to convert them to a single type if you want to operate on different kind of variables.

    To save the space that print adds between different parameters passed to it, you also have to add it to the string (when using string concatenation).

    Command-line parameters
    Code (Text):
    1. import sys
    2.  
    3. print sys.argv[0] # prints the filename of your script
    4. print sys.argv[1] # and the first parameter
    5. print sys.argv[1:] # or all parameters
    6.  
    If you want to pass parameters from the command line you will need the sys module, which is imported on the first line of the example. The module defines the argv list which contains the arguments to the script.

    Lists in Python are something similar to arrays in other languages yet Python has arrays also, which are more functional than lists so if you want to use arrays you would have to import the array module.




    :)


    If you like it, give me +rep, please.
     
  2. X Joe Kickass X

    X Joe Kickass X Level IV

    Joined:
    Nov 18, 2006
    Messages:
    1,401
    Likes Received:
    0
    Location:
    Behind You
    Plus rep for you mister
     
  3. .aId

    .aId Level I

    Joined:
    Nov 17, 2006
    Messages:
    119
    Likes Received:
    0
    Location:
    LoserVille
    Hey,, thx ^^
     
  4. lazypando

    lazypando Level IV

    Joined:
    Nov 16, 2006
    Messages:
    3,326
    Likes Received:
    94
    java is already kinda confusing but this is :shock:
     
  5. .aId

    .aId Level I

    Joined:
    Nov 17, 2006
    Messages:
    119
    Likes Received:
    0
    Location:
    LoserVille
    lol,, yes
    i know,,
    it take TO many time learn this,,
    but with practice,, your can make AWSOME things
     
  6. lazypando

    lazypando Level IV

    Joined:
    Nov 16, 2006
    Messages:
    3,326
    Likes Received:
    94
    agreed :? ...i'm learning java at school and we can barely do anything right now...
     
  7. Osiris

    Osiris Level I

    Joined:
    Nov 28, 2006
    Messages:
    107
    Likes Received:
    0
    Thanks for the post. Pretty informative information. Python is a blast and believe it or not , Not many ppl use it.
     
  8. angelo

    angelo Newbie

    Joined:
    Dec 2, 2006
    Messages:
    2
    Likes Received:
    0
    Location:
    BANNED
    WOW THANK FOR THE INFORMATION
     
  9. donutlink

    donutlink Newbie

    Joined:
    Nov 27, 2006
    Messages:
    23
    Likes Received:
    0
    I like your guide, but unfortunatly I have never used any programming and don't understand the commands. Can you point me in the direction of some coding tutorials so that I can get on my feet?

    +rep!
     
  10. .aId

    .aId Level I

    Joined:
    Nov 17, 2006
    Messages:
    119
    Likes Received:
    0
    Location:
    LoserVille

    I start with visual basic
    And thx for the rep
     
  11. Kadam

    Kadam Level II

    Joined:
    Dec 15, 2006
    Messages:
    259
    Likes Received:
    0
    Python.. I was trying to learn that, I swear it scrambled my brain
     
  12. luishem

    luishem Level II

    Joined:
    Dec 3, 2006
    Messages:
    349
    Likes Received:
    0
    Location:
    Bowerstones Jail
    it seems to have lots of things to make, right?
    but ill take time to learn it lol
     
  13. Anonymous

    Anonymous Guest

    kadam, please don't gravedig.
     
  14. Kadam

    Kadam Level II

    Joined:
    Dec 15, 2006
    Messages:
    259
    Likes Received:
    0
    Its not grave-digging if its still on the first page, please, refine your vocabulary.
     
  15. impega

    impega Level III

    Joined:
    Dec 7, 2006
    Messages:
    408
    Likes Received:
    2
    Location:
    Nottingham (SHOTTS!)
    Grave digging is when you post on really old topics... like this one

    The last post before yours was over 2 weeks ago >__<
     
  16. Kadam

    Kadam Level II

    Joined:
    Dec 15, 2006
    Messages:
    259
    Likes Received:
    0
    You people don't know the meaning of gravedigging, see its still on the first page, If I want to post in it I can, if it was on the 4th page(Note there isn't a 4th page), I wouldn't have though.
     
  17. expon

    expon Administrator
    Staff Member

    Joined:
    Oct 30, 2006
    Messages:
    1,393
    Likes Received:
    56
    Location:
    UK
Thread Status:
Not open for further replies.