#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Python - Where to study?


    Hey, I have been studying how to program on Python for some time now, and I've completed the cscircles Python tutorial and read the InventWithPython -book. I'm not sure if this is the right place to ask, but I'll post away.

    I've got the basics down quite well, but I'd like to go a little more advanced, but where should I start? I want to learn for example how to modify all sorts of files with Python and how to get information off of websites and other running programs.

    I'd also like to learn the fundamentals of a programming language, as how libraries, modules and data structures work, for example.

    I am probably going to start studying some other languages such as Java or C++ as well, so I'd like to know some of the differences. I've also heard that those are faster than Python, but I'd like to know how much faster they actually are.

    I'm also interested in the O(n) notation, and wish someone would elaborate what that means, and if there are any other notations. As far as I know, O(n) describes the time spent on executing the program.

    I'll ask more later if you don't mind.

    Thanks for any advice in advance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    Instructions, write a program that reads an input file, transforming it to an output file:
    1) from the input remove all lines before the final line that starts with /COM,ANSYS
    2) with remaining lines
    a) delete all lines that start with /COM or with !
    b) transform lines that begin with *SET, into assignments with an = sign. Example,
    *SET,thickness,outer*6.35e-3 ! thickness in meters
    becomes
    thickness=outer*6.35e-3 ! thickness in meters

    Here's data for your input file:
    Code:
    /BATCH  
    /COM,ANSYS RELEASE 11.0SP1 UP20070830       18:15:00    07/25/2012
    /gopr   
    *SET,outer,3
    *SET,middle,5   
    *SET,inner,2.9534   
    *SET,thickness,outer*6.35e-3  ! thickness in meters 
    *SET,frit,middle*0.001  
    *SET,span,inner ! meters
    pyrex,1    ! load pyrex properties as material list 1   
    dwlprep,thickness,span,frit 
    dwlload 
    dwlsolve
    /BATCH  
    /COM,ANSYS RELEASE 11.0SP1 UP20070830       18:43:13    07/25/2012
    /gopr   
    *SET,outer,3
    *SET,middle,3   
    *SET,inner,2.573
    *SET,thickness,outer*6.35e-3  ! thickness in meters 
    *SET,frit,middle*0.001  
    *SET,span,inner ! meters
    pyrex,1    ! load pyrex properties as material list 1   
    dwlprep,thickness,span,frit 
    dwlload 
    dwlsolve
    dwlpost 
    ! /exi

    Big O notation. O(n).

    Suppose you have a list, length n, of angles. You want to find the sin and cosine of each angle. Your program has to examine each angle. Excluding the effects of differing kinds of memory your operating system uses depending on the list size, you expect the run time will be proportional to the length of the list. This is an O(n) computation.

    Now suppose you have a collection of objects having position. You want to find the two objects that are closest together. One algorithm involves computing the entire upper triangular part of the matrix of distances. Since that matrix is an area with edge lengths n the algorithm must be O(n**2). I'll spell it out for those who might not understand (and someone else can spell it out for me if I don't understand!)
    Suppose there are 4 objects, [A,B,C,D]. Let d(o1,o2) be the distance function. We need
    Code:
            d(A,B), d(A,C), d(A,D)
                    d(B,C), d(B,D)
                            d(C,D)
    which is 6 distance computations, and as we know,
    6 = 3+2+1
    which is (n-1)*n/2 terms
    which is order n squared.
    Last edited by b49P23TIvg; October 31st, 2012 at 07:07 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Only2
    I've got the basics down quite well, but I'd like to go a little more advanced, but where should I start? I want to learn for example how to modify all sorts of files with Python and how to get information off of websites and other running programs.
    "Modifying all sorts of files" is too vague for me to give any useful pointers.

    "Websites and other running programs"? First of all, a website isn't a running program - unless you mean that you want to get information about a website from a running browser, which is a strange thing to want to do unless you're writing a browser plugin (but in Python?). I can think of two obvious ways to get information from a website (affectionately known as "scraping") in Python. Both of them begin by making an HTTP request to the website and storing the reply as text somewhere, like a string variable. The first way is more disciplined: you parse the html into an internal data structure, from which all the information you want can be accessed easily. To go down this path, you probably want to look here: http://docs.python.org/2/library/markup.html The second way is to search for the information you want using pattern matching. This might be attractive if you're experienced with regular expressions, but this sort of solution will likely be more fragile and more difficult to maintain than the other method.
    Originally Posted by Only2
    I am probably going to start studying some other languages such as Java or C++ as well, so I'd like to know some of the differences. I've also heard that those are faster than Python, but I'd like to know how much faster they actually are.
    Well written C++ code can be drastically more efficient than the equivalent Python. Factors of 10-1000 aren't uncommon. The actual speedup depends a lot on the specific computation. I don't work with Java myself, but I'm guessing the story is similar, just not as drastic.
    Originally Posted by Only2
    I'm also interested in the O(n) notation, and wish someone would elaborate what that means, and if there are any other notations. As far as I know, O(n) describes the time spent on executing the program.
    The notation you're referring to, called "big-O notation," is a way to quantify the rate of growth of a function as a function of its argument. A typical use case is, as you mentioned, the running time of a program or algorithm, in which case the "function" is the running time and the "argument" is the size of the input.

    Suppose f(n) >= 0 for all n >= 0. To say that the function f(n) is O(g(n)) means simply that there are some constants C and N such that f(n) < C*g(n) for all n > N. Informally, it means that the rate of growth of f is no greater than the rate of growth of g for large values of n, up to multiplication by a constant factor.

    In the context of computer science: like I said, usually here f(n) will be the running time (usually worst-case) of a program or algorithm, and n will be the size of the input (as in: the number of characters in a string, the number of items in an array, number of queries/operations, etc.). g(n) will be a simple mathematical formula. For example, the running time of mergesort on an array of length n is O(n*log(n)). Mathematically, this means that for any given particular implementation of mergesort, there is a particular constant C such that for any large-enough array, the running time will be no worse than C*{array size}*log({array size}).

    This notion (along with related ones like little-O, big-Omega, and big-Theta) is crucial in computer science because the literal running time of an algorithm depends on the hardware and implementation in subtle ways, while assertions like "mergesort on an array of n items is O(n*log(n))" can be made without knowing anything about the implementation, whose details get conveniently swept into the implied constans C and N.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Good replies there, excellent ones.

    Here's a few more questions.

    As I read the InventWithPython -book, I began wondering how game servers work, and how fps (frames per second) is related to them.

    In a tutorial, the game loop was executed as usual, but at the end of it, it had some sort of command along the lines of pygame.tick(40). In short, it adjusted the speed of execution so that the game loop would be executed 40 times per second, resulting in 40 screen updates per second.

    Now, I know some games that run 200+ fps, but that of course is different for other players. Some can have 200+ while another one could have 23. So, how do servers and fps's work? Or online games in general?

    Speaking of servers, I'd like to know how they actually work, and how many people can connect via the internet and affect the running of the program together. A friend of mine also mentioned the terms "non-blocking" and "blocking" socket, as well as "asynchronised". Elaborating of the terms would be appreciated.

    From servers to code structures.

    I opened my Minecraft folders and saw loads of different files. I'd like to know why they are separate files, how the executable reads them, and if you can do the same with Python, and if it is only voluntary to make separate files for one program on Python.

    Answers would be much appreciated.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Only2
    Answers would be much appreciated.
    Bump?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    Good luck, you seem to have jumped from "learning python" to "I didn't yet learn to program and I want to write a multi-billion dollar game."

    I don't understand 200 frames per second, 60 fps is as fast as a human can comprehend.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Good luck, you seem to have jumped from "learning python" to "I didn't yet learn to program and I want to write a multi-billion dollar game."

    I don't understand 200 frames per second, 60 fps is as fast as a human can comprehend.
    That's a way to say it. It's more of a "I want to know how multi-billion dollar games are written".

    As for the fps-thing, one player could still have 20fps while the other has 40fps. Good to know the 60fps limit though.
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    If I would write a large program in C++, I would write it in modular fashion to test each file/module and then link them together at the end.

    A large Python program can be written in the same fashion, then tested and imported into the main program.

    Python is often called a glue language since you can write speed sensitive parts of your code in C/C++. The major speed improvement with Python is the speed of developing a larger program. Comparing the execution speed of a compiled language like Assembly, C or C++ with an interpreted language like Python or Java is rather nonsensical.
    Last edited by Dietrich; November 4th, 2012 at 10:09 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Dietrich
    Python is often called a glue language since you can write speed sensitive parts of your code in C/C++. The major speed improvement with Python is the speed of developing a larger program.
    How do you attach two different languages together to form a program?
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481

IMN logo majestic logo threadwatch logo seochat tools logo