Lists have many shared properties with strings. For many purposes in Python, a string is a list of characters.
Lists are done with square brackets, with the elements separated by commas: [1, 2, 3] is a list, as is [1].
Lists can contain any Python object, including other lists: [[1, 2], 4].
List can contain mixtures of different kinds of objects: [[1, 2], 4, False, 7, 2.0].
To add something to the end of a list, use the append method -- given x = [1, 2, 3], x.append(0) adds 0 to the end.
Using append actually changes x! Lists are mutable!
If you want to avoid changing x, you can use +, as in x + [0]. This returns a new list, leaving x unchanged.
Notice the intuitive use of + there. For numerical types, it means addition. For str and list, it means concatenation.
What does [1] * 5 do, then? What about [1, 3] * 5?
The method insert can be used to add things to any place in a list. The pattern is x.insert(index, value), where index is the position you want the new element to occupy:
x = [1, 2, 3]x.insert(1, "a") changes x to [1, 'a', 2, 3]x.insert(len(x), "a") is the same as x.append("a")x.index(2) is just like x.find(2), but it raises an exception of there is no 2 in x.
x.sort() will sort x in place, using whatever logic Python has for sorting objects inside x. This will fail if the objects are of different types.
There are many other list methods: https://docs.python.org/3.7/tutorial/datastructures.html#more-on-lists
len is different than the methods above: it is a built-in function that can operate on many expressions. Thus, to get the length in characters of a string s, use len(s)
Similarly, to convert a string to an int, use int("1"). For a float, use float("1").
Concatenation: use +. Thus s + s returns the concatenation of s with itself. This is a common pattern in Python: the + operator is addition if the arguments are numerical, it is concatenation if the arguments are str, and it creates a new list if given lists as arguments.
You can also do "x" * 5, which return "xxxxx".
Iterating over a list:
for x in [1, 2, 3]:
print(x)
Iterating over a str:
for x in "abc":
print(x)
Lone if clause:
x = 3
if x > 2:
print("{} is greater than 2".format(x))
Adding an else clause:
if x > 2:
print("{} is greater than 2".format(x))
else:
print("{} is not greater than 2".format(x))
Adding an elif clause:
if x > 2:
print("{} is greater than 2".format(x))
elif x == 2:
print("{} equals 2".format(x))
else:
print("{} is not greater than 2".format(x))
There can be any number of elif statements.
The final else is optional, but it must occur at the end of the conditional block.
Positivity testing:
def is_positive(x):
if x > 0:
return True
else:
return False
Note: because x > 0 returns True or False according to the same logic, the entire body of this can be simplified to x > 0. The above shows off more of the possibilities for function formatting, though.
An answer to assignment 1, question 1; feel free to use it, but consider rewriting it as a one-liner using sum.
def mean(vals):
total = 0
length = len(vals)
for x in vals:
total = total + x
mu = total / length
return mu