How to create a list in python with length

Jul 17, 2012, by admin

python-logoIn Python, there is no such thing as an array. Instead, we have lists, which are even more flexible. In arrays, you can only have one data type. (String, integer, float, etc.) In lists, you can combine all kinds of data types in one, and you won’t get any errors.

Graeme-Cross'-list-of-things-he-hates-about-python-1Steps create a list in python with length

1.Open an interactive window. Try using IDLE instead of the Python command prompt.

python-IDLE-location

To open IDLE, go to Start Menu > All Programs > Python (version number) > IDLE (Python GUI).

2.Next, create your list. python-logo-1

First, write your variable name. This can be pretty much anything, but can not begin with a number. We will use too for the name.

Now, type an equal sign to assign this name to something.

Third, the hard part. Type a left bracket, ([), then some kind of datatype. For example, a string is enclosed in quotes: ‘This is a string’. An integer needs no quotes: 56. For this list, we will use 1,2,3. So: [1,2,3]. What you have now should look like: foo = [1,2,3]. You could also use a string, like foo = [‘John’, ‘John2’, ‘John3’]. All list items are separated by commas.

python-Foo-list