View Full Version : Python quick questions
krypto_wizard
January 25th, 2006, 11:24 AM
Hi,
I am a python newbie. I started reading "ByteofPython" and was also looking at "dive into python".
I couldn't find these things
1. How to use 2 dimensional Array(or higher dimensional) in Python....nobody mentioned that.
2. How to pass an array as a function argument ? I was kinda confused and didn't get much really.
Thanks
ubuntumaneh
January 25th, 2006, 11:32 AM
1)
a=[1,2]
b=[2,2]
a+b
This simple example show you you don't have to declare the variable before use it. Also, have a look at the type "list" in your books.
2)
def soma_vec(a,b):
c=a+b
return c
a=[1,2]
b=[2,3]
print soma_vec(a,b)
x=1
y=2
print soma_vec(x,y)
diffuser78
January 25th, 2006, 11:39 AM
How to pass 3d array ? Is there anything like poiners
ubuntumaneh
January 25th, 2006, 11:48 AM
It was a very nice experience. I just got a book, like you. And I had the luck to have some problems to solve (mainly physics and math ones). That's the most important. I learned in a day the most common commands. Then I was able to make well structured programs. I started to write oo programs a month later. Since then I have being playing with it. It is fast and easier.
I read from time to time things from:
http://python.org/doc/current/modindex.html
to get acquainted with some modules.
My suggestion is: set some problems, and work on it with python. That is the best way. Examples are:
1) email server and client
2) sysadmin scripts, like a script to add new user with all configurations set by you, a program to rename multiple files
3) some mathematical and physics problems
have fun
ubuntumaneh
January 25th, 2006, 11:52 AM
Ther is no pointers. Consider the function built before. Just use:
a=[1,2,3]
b=[3,2,1]
print soma_vec(a,b)
x=[1,2,3,4,5,6,7]
y=[7,6,5,4,3,2,1]
print soma_vec(x,y)
krypto_wizard
January 25th, 2006, 11:57 AM
Thanks ubuntumaneh, I really appreciate your help.
Ther is no pointers. Consider the function built before. Just use:
a=[1,2,3]
b=[3,2,1]
print soma_vec(a,b)
x=[1,2,3,4,5,6,7]
y=[7,6,5,4,3,2,1]
print soma_vec(x,y)
Van_Gogh
January 26th, 2006, 03:10 AM
multidimensional array are typically implemented in Python as lists of lists, e.g.
2_dim_array = [[1,2,3], [4,5,6]] #2x3 matrix
elem_2_3 = 2_dim_array[1][2] #accesing element 2,3
Alternatively, if you need to do math and the array handling to be faster(the built-in lists are quite slow for math stuff), you could take a look at the modules Numeric or SciPy. For matrix manipulation etc. these are an order of magnitude faster than simple lists of lists.
krypto_wizard
January 26th, 2006, 10:48 AM
Thanks dude, this is the answer I was looking for. I wanted to write a class for matrix operations just for practice. This quite helps.
multidimensional array are typically implemented in Python as lists of lists, e.g.
2_dim_array = [[1,2,3], [4,5,6]] #2x3 matrix
elem_2_3 = 2_dim_array[1][2] #accesing element 2,3
Alternatively, if you need to do math and the array handling to be faster(the built-in lists are quite slow for math stuff), you could take a look at the modules Numeric or SciPy. For matrix manipulation etc. these are an order of magnitude faster than simple lists of lists.
krypto_wizard
January 26th, 2006, 04:54 PM
Thanks as I said earlier.
I am writing Matrix Program (manipulation) just to get some practice.
Can you give any starters on that.
multidimensional array are typically implemented in Python as lists of lists, e.g.
2_dim_array = [[1,2,3], [4,5,6]] #2x3 matrix
elem_2_3 = 2_dim_array[1][2] #accesing element 2,3
Alternatively, if you need to do math and the array handling to be faster(the built-in lists are quite slow for math stuff), you could take a look at the modules Numeric or SciPy. For matrix manipulation etc. these are an order of magnitude faster than simple lists of lists.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.