Results 1 to 6 of 6

Thread: Fundamentals of Python

  1. #1
    Join Date
    Jul 2010
    Posts
    32

    Fundamentals of Python

    I have recently started learning about the python programming language. I have read that the Python is free to use, even for commercial products, because of its OSI-approved open source license, so I am gaining some more interest in this language. Also the another thing that I liked is that Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines. This is really very good point of this language. I am having knowledge about the Java, VB.Net, etc. but newbie in Python. So I want to clear some basic concepts and then I can decide whether I have to move for this language or not. Please provide some useful information about the Python soon.

  2. #2
    Join Date
    Feb 2010
    Posts
    184

    Re: Fundamentals of Python

    First, think of Python as pseudo-code. It is almost true. The variables have no types, nor claim to. They appear when the checks and disappear when not in use anymore. The allocation is done by the operator = . Equality is to check with the operator == . You can assign multiple variables at once:
    x, y, z = 1,2,3
    The blocks are indicated through indentation, and only through indentation. (Neither BEGIN / END or scratch it.) Some common control structures:
    Code:
    if x <5 or (x> 10 and x <20): 
              print "The value is OK." 
    
          if x <5 or 10 <x <20: 
              print "The value is OK." 
    
          for i in [1,2,3,4,5]: 
              print "Iteration number", the 
    
    
          x = 10 
          while x> = 0: 
              print "x is still not negative." 
              x = x-1
    The first two examples are equivalent. The index variable in the cycle time for iterates through the elements of a list (written as an example). To make a loop for "normal" (ie, a cycle count), use the built-in range().

  3. #3
    Join Date
    Feb 2010
    Posts
    641

    Re: Fundamentals of Python

    The function input shows the prompt (which may be empty) and allows the user to enter any valid Python value. In this case we expected a number - if you enter something else (such as a string), the program crashes. To avoid this we need a little 'error control. Here I will not talk about it, suffice it to say that if you want that user input is taken verbatim as a string (so that everything can be entered), instead you use the function raw_input . If I wanted to convert the string s to an integer, you could then use int(s). Also you should remember that if you want to read a string input , the user must explicitly write the quotes. In Python, strings can be enclosed in single or double quotes. So we covered control structures, input and output - now we need some good data structures. The most important are lists and dictionaries.

  4. #4
    Join Date
    Feb 2010
    Posts
    658

    Re: Fundamentals of Python

    One of the nice things is that you can access lists of the items separately or in groups, through indexing and slicing. The radio is done (as in many other languages) hanging brackets index to the list. (Note that the first element has index 0).
    print name [1], name [0] # print "Mine Sweeper"

    name [0] = "Smith"
    The slicing is almost like the indicative, except that write-off is the index of the result is to end with a colon ( : ) to separate them:
    x = ["spam," "spam," "spam," "spam," "spam," "eggs", "and", "spam"]

    print x [5:7] # Print the list ["eggs", "and"]
    Note that the end is non-inclusive. If there is an index, it means you want everything in that direction. For example, list[:3] means "the beginning of each item list until the element 3 is not included "(You could argue that actually means the item 4, as part of the count to 0 ... oh well). list[3:] On the other hand, would mean "each element of list , starting from the 3 (inclusive) to the last, inclusive. " To get really interesting results, you can also use negative numbers: list[-3] is the third element from the end of the list ... About indexing, you might be interested to know that the built-in function len gives the length of a list.

  5. #5
    Join Date
    Feb 2010
    Posts
    669

    Re: Fundamentals of Python

    We want to give a name to a piece of code, and call a couple of parameters. In other words - we want to define a function (or "procedure"). It's easy. Use the keyword def as follows:
    def square (x):
    return x * x

    print square (2) Print # 4
    For those of you who understand this: All parameters are passed by reference in Python (as, for example, in Java). For those who do not understand: do not you worry. Python has a great abundance of pretty things like named arguments and default arguments, and can handle a variable number of arguments to a single function. If you can use the functions in general, this is what you need to know about them in Python. (Ah yes ... the keyword return terminates execution of the function and returns the given value). One thing that might be useful to know, though, is that Python functions are values. So if you have a function as a square , you could do something like:
    Code:
    queeble = square 
          queeble (2) 
          Printing 4
    To call a function without arguments, you must remember to write doit() and do not doit. The latter, as we have shown, returns only the function itself, as the value.

  6. #6
    alexborow Guest

    Re: Fundamentals of Python

    This is really very good point of this language. I have knowledge about the Java, VB.Net, etc. but newbie in Python. Here all these things are great to know about it.

Similar Threads

  1. Fundamentals of WML
    By Dante i in forum Software Development
    Replies: 6
    Last Post: 09-12-2010, 06:20 AM
  2. Replies: 3
    Last Post: 30-10-2009, 11:32 AM
  3. Network Guide Part 1 - Network Fundamentals
    By mindreader in forum Networking & Security
    Replies: 11
    Last Post: 27-07-2009, 07:45 AM
  4. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM
  5. Networking Guide Part 3 - TCP/IP Fundamentals
    By mindreader in forum Networking & Security
    Replies: 26
    Last Post: 12-11-2004, 09:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,714,806.19867 seconds with 17 queries