Anyone Can Code.
A sequence is a collection of values inside a storage unit. A variable can store a sequence of values in three forms:
In this tutorial we will be briefly touch upon these data types, but we will explore them further in tutorial 6.
In Python, strings can be any assortment of alpha-numeric characters or symbols surrounded by single quotation marks, double quotation marks, or for a multi line string, triple quotation marks. The string itself can be an entire sentence or just a single word/number/special character.
To assign a variable with a string value, you use a
= operator and then enclose the
string within single, double or triple quotes.
x = 'hello' y = '''hel lo''' print(x) print(y)hello hel lo
Strings are immutable which means that thier values cannot be overwritten. You can, however, add values to it at the end.
Two strings can be combined, or concatenated, using the
+ operator. However,
strings cannot be combined with numbers. Python will give an error if you try to combine strings
a = 'Hello' b = '5' c = a + b print(c)Hello5
String slicing is also an important part of strings but it will be covered in a different tutorial along with an in-depth look at dictionaries, lists and tuples.
A list is a collection of data which is ordered and mutable (its contents can be overwritten). It allows duplicate members and is written using square brackets. Each member of the list is separated using a comma and the list can contain multiple data types (although it is preferred to keep the data type consistent throughout a list). A list is assigned to a variable and the variable identifier (name) is used throughout a program to reference the entire list itself or individual members of the list.
list_name = ['food', 'cupboard', 'fifteen', 7] print(list_name)['food', 'cupboard', 'fifteen', 7]
In Python, a tuple is similar to a list. It is a collection of data values which is ordered but unlike lists, tuples are immutable (its values cannot be overwritten). Tuples consist of comma separated values, enclosed in round brackets.
tuple_name = ('food', 'cupboard', 'fifteen', 7) print(tuple_name)('food', 'cupboard', 'fifteen', 7)
each member in a sequence has a unique position number (or more commonly known in Python as an index). However, in Python and other programming languages, the index values don't start with 1. The first member of any sequence always has the index value of 0 and it goes up from there. Individual members of a sequence can be accessed by referring to its index value.
In a list or tuple, the first index value (0) is held by the first element in the list/tuple.
string1 = "qprogramming" list1 = [2,3,4] tup1 = (5,7,8) print(string1) print(list1) print(tup1)r 3 8
To convert between sequences, we can use built-in functions (str,list,tuple):
tuple_name = ('food', 'cupboard', 'fifteen', 7) x = str(tuple_name) print(x) print(x) x = list(tuple_name) print(x) print(x) x = tuple(x) print(x)('food', 'cupboard', 'fifteen', 7) f ['food', 'cupboard', 'fifteen', 7] 7 ('food', 'cupboard', 'fifteen', 7)
As you can see, we used
tuple to convert
between different types of sequences. To prove that they were actually
converted, we used indexing to access different elements. In a string we will get a single
character, but for lists and tuples we will get complete values.
That's it for this tutorial! Click Next to access the next tutorial!