Python Basics: Tuples and Lists
1 post
Page 1 of 1
As we saw in the previous tutorial, Python data types also include Tuples and Lists. We'll see more about these in this tutorial.
Tuples
First and foremost, we must define what a Tuple is. A Python tuple is an ordered collection of object references. Objects referenced by a tuple do not have to be of the same type (for instance, you may have Numbers and Strings in the same Tuple). Like a String, it is immutable, which means it cannot be altered once created.
Creating Tuples
Python Tuples are simply created by assigning a comma-seperated list of objects to a variable, like so:
Lists
Like Tuples and Strings, Lists are also a collection type object. They are ordered, like Tuples, but unlike Tuples and Strings, they are mutable. This means that modifying a List in a method, for example, affects the actual List:
List creating in Python is similar to JavaScript. That is, a variable name, followed by square brackets, and then the contents of the List:
Like in VB.NET, Strings can be split into Lists by specifing a String and a delimiter:
Here are some interesting functions that Python Lists offer:
l.append(x): Appends x to the List l
l.count(x): Like with Tuples, counts the number of times x is found in the List
l.index(x): Like with Tuples, returns the index of the first instance of x in the List
l.insert(i,x): Inserts object x at index i of the List
l.pop(i): Removes the List item at index i
Lots more functions are available, but since I can't show them all here, I suggest you check out Python's official documentation on Lists: Python List documentation
List comprehension
One of the many neat features of Python is list comprehension. This allows you to create Lists using loops and conditionnals, like so:
That's all the important stuff to know about Tuples and Lists in Python. Hopefully, this tutorial has been of use to you, and as always, go crazy! cooll;
Tuples
First and foremost, we must define what a Tuple is. A Python tuple is an ordered collection of object references. Objects referenced by a tuple do not have to be of the same type (for instance, you may have Numbers and Strings in the same Tuple). Like a String, it is immutable, which means it cannot be altered once created.
Creating Tuples
Python Tuples are simply created by assigning a comma-seperated list of objects to a variable, like so:
Code: Select all
Tuples may also contain other tuples, for instance:my_tuple = "a string", 11, 42.3, "a number... no it isn't"
Code: Select all
Because Tuples are a sequence, they can be sliced, exactly like Strings:my_tuple = "a string", ("a string in another tuple", 11), 42.3
Code: Select all
They do however offer two aditionnal functions: count() and index(). The first returns the number of time a certain object occurs in the tuple, while the second return the index of the specified object in the tuple.my_tuple = "a string", 11, 42.3, "a number... no it isn't"
print(my_tuple[1]) #Prints 11
Lists
Like Tuples and Strings, Lists are also a collection type object. They are ordered, like Tuples, but unlike Tuples and Strings, they are mutable. This means that modifying a List in a method, for example, affects the actual List:
Code: Select all
Creating Listsmy_list = [1, "s", 15.54]
def change_item(l,i):
>>>>l[i] = 42
change_item[l,0]
print(my_list) #Prints [42, "s", 15.54]
List creating in Python is similar to JavaScript. That is, a variable name, followed by square brackets, and then the contents of the List:
Code: Select all
Like Tuples, Lists can contain more than one type of object, including other Lists
l = ["item1", 2, 3.33]
Code: Select all
Because Lists are sequences, they can also be sliced like Strings or Tuples:l = ["item1", ["item of another List", 4.56], 2, 3]
Code: Select all
Splitting Strings into Listsl = ["item1", 2, 3.33]
print(l[1]) #Prints out 2
Like in VB.NET, Strings can be split into Lists by specifing a String and a delimiter:
Code: Select all
More List functionsmy_string = "C:\Users\Craig\Documents\JustinBieber\Pics"
my_split_string = my_string.split("\")
print(my_split_string) #Prints out ["C", "Users", "Craig", "Documents", "JustinBieber", "Pics"]
Here are some interesting functions that Python Lists offer:
l.append(x): Appends x to the List l
l.count(x): Like with Tuples, counts the number of times x is found in the List
l.index(x): Like with Tuples, returns the index of the first instance of x in the List
l.insert(i,x): Inserts object x at index i of the List
l.pop(i): Removes the List item at index i
Lots more functions are available, but since I can't show them all here, I suggest you check out Python's official documentation on Lists: Python List documentation
List comprehension
One of the many neat features of Python is list comprehension. This allows you to create Lists using loops and conditionnals, like so:
Code: Select all
There might be a few things you don't understand in the above code, but basically, it creates a list populated with all the numbers from 0 to 50 whose modulo with 2 is 0 (which means they are evenly divisible by 2).my_list = [c for c in range(50) if x % 2 == 0]
That's all the important stuff to know about Tuples and Lists in Python. Hopefully, this tutorial has been of use to you, and as always, go crazy! cooll;
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023