471,344 Members | 1,006 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

how to write an array using python

Im new to the python programming and I wanted to know how to write an array using python
Jun 12 '07 #1
6 13039
Motoma
3,237 Expert 2GB
Im new to the python programming and I wanted to know how to write an array using python
Expand|Select|Wrap|Line Numbers
  1. arr = [1, 2, 3, 'four', [5, 6]]
  2.  
Jun 12 '07 #2
Thanks, one more thing how do you uss a for loop to print out lines with the line number, followed by the word
Jun 12 '07 #3
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. arr = [1, 2, 3, 'four', [5, 6]]
  2.  
Yea, just wanted to add that python has other array-like data types too:

Tuples are like lists except they can't be modified (immutable):
Expand|Select|Wrap|Line Numbers
  1. myTup = (1, 2, 3, "string", (5, 6), ["string2", 9])
  2.  
Dictionaries are like the C++ maps, they store corresponding values:
Expand|Select|Wrap|Line Numbers
  1. myDict = {"val":6, "val2":8, "val3":10}
  2.  
Jun 12 '07 #4
ilikepython
844 Expert 512MB
Thanks, one more thing how do you uss a for loop to print out lines with the line number, followed by the word
You can use enumerate():
Expand|Select|Wrap|Line Numbers
  1. lines = ["This is the first line", "This is the second line"]
  2.  
  3. for (lineNum, line) in enumerate(lines):
  4.     print lineNum + 1, line                        # enumerate returns the index which starts at zero
  5.  
or the index function of lists:
Expand|Select|Wrap|Line Numbers
  1. lines = ["This is the first line", "This is the second line"]
  2.  
  3. for line in lines:
  4.     print lines.index(line) + 1, line
  5.  
Jun 12 '07 #5
Thank you so mush you been a lot of help, im gonna check it and see if it works
Jun 12 '07 #6
bartonc
6,596 Expert 4TB
Thank you so mush you been a lot of help, im gonna check it and see if it works
I must ask that you please follow the Posting Guidelines for future posts on this site.

Thanks for joining.
Jun 12 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Phil | last post: by
6 posts views Thread by Peter Wuertz | last post: by
4 posts views Thread by DeveloperX | last post: by
8 posts views Thread by Santiago Romero | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.