how to write an array using python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nubean19
    New Member
    • Jun 2007
    • 4

    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
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by nubean19
    Im new to the python programming and I wanted to know how to write an array using python
    [code=python]
    arr = [1, 2, 3, 'four', [5, 6]]
    [/code]

    Comment

    • nubean19
      New Member
      • Jun 2007
      • 4

      #3
      Thanks, one more thing how do you uss a for loop to print out lines with the line number, followed by the word

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Motoma
        [code=python]
        arr = [1, 2, 3, 'four', [5, 6]]
        [/code]
        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):
        [code=python]
        myTup = (1, 2, 3, "string", (5, 6), ["string2", 9])
        [/code]

        Dictionaries are like the C++ maps, they store corresponding values:
        [code=python]
        myDict = {"val":6, "val2":8, "val3":10}
        [/code]

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by nubean19
          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():
          [code=python]
          lines = ["This is the first line", "This is the second line"]

          for (lineNum, line) in enumerate(lines ):
          print lineNum + 1, line # enumerate returns the index which starts at zero
          [/code]

          or the index function of lists:
          [code=python]
          lines = ["This is the first line", "This is the second line"]

          for line in lines:
          print lines.index(lin e) + 1, line
          [/code]

          Comment

          • nubean19
            New Member
            • Jun 2007
            • 4

            #6
            Thank you so mush you been a lot of help, im gonna check it and see if it works

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by nubean19
              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.

              Comment

              Working...