October 31st, 2012, 04:27 PM

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.
October 31st, 2012, 08:04 PM

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.35e3 ! thickness in meters
becomes
thickness=outer*6.35e3 ! 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.35e3 ! 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.35e3 ! 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 (n1)*n/2 terms
which is order n squared.
Last edited by b49P23TIvg; October 31st, 2012 at 08:07 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
October 31st, 2012, 08:33 PM

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 101000 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 "bigO 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 worstcase) 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 largeenough array, the running time will be no worse than C*{array size}*log({array size}).
This notion (along with related ones like littleO, bigOmega, and bigTheta) 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.
November 1st, 2012, 06:15 PM

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 "nonblocking" 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.
November 4th, 2012, 05:28 AM

Originally Posted by Only2
Answers would be much appreciated.
Bump?
November 4th, 2012, 08:57 AM

Good luck, you seem to have jumped from "learning python" to "I didn't yet learn to program and I want to write a multibillion 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!
November 4th, 2012, 10:28 AM

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 multibillion 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 multibillion dollar games are written".
As for the fpsthing, one player could still have 20fps while the other has 40fps. Good to know the 60fps limit though.
November 4th, 2012, 11:04 AM

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 11:09 AM.
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
November 4th, 2012, 01:30 PM

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?
November 4th, 2012, 01:49 PM

[code]
Code tags[/code] are essential for python code and Makefiles!