Tuples and Lists
In []: a = 'acbde'
In []: b = ('a', 'b', 'c', 'd', 'e')
In []: c = ['a', 'b', 'c', 'd', 'e']
The values of a[3], b[3], and c[3] are all the same. In what ways are a, b, and c different?
A is a string, B is a tuple and C is a list. Strings are just something you type in and it can be anything as long as you have a ' before and after the string. a tuple is immutable, which means the values cannot be changed. A list can be changed, inserted, or deleted and lists are mutable.
2. Why do computer programming languages almost always have a variety of variable types?
Different situations call for a different usage of variable types. If you were doing any math, you wouldn't use a tuple because the values wouldn't change.
3. Why can't everything be represented with an integer?
Everything can't be represented with an integer because tuples cannot take integeres, even though everything is stored as 1s and 0s. Some things you want to do require a string, like printing back a message, and that would be difficult trying to print a message only using 1s and 0s.
No comments:
Post a Comment