



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Disc. # 04-11-
String can be constructed by concatenating substrings together. We can use arithmetic + symbol to do this task, and the resultant string can be the “sum” of as many substrings as we supply. Let’s see a few examples:
>>> print "ECS " + "10 " + "is " + "fun" ECS 10 is fun >>> substr1 = "ECS " >>> substr2 = "10 " >>> substr3 = "is " >>> substr4 = "fun " >>> >>> print substr1 + substr2 + substr3 + substr ECS 10 is fun >>> >>> # But we cannot subtract strings >>> myString = substr1 + substr2 + substr3 + substr >>> print myString ECS 10 is fun >>> myString - "ECS " Traceback (most recent call last): File "", line 1, in myString - "ECS " TypeError: unsupported operand type(s) for -: 'str' and 'str'
We already learned that variables can be used to store values of certain type. It is sometimes necessary to convert variables from one type into another. For example, if you need to change some of the characters of a string, you will have to transform the string in a mutable list (Prof. Ludaecher will cover the concept mutable vs. immutable later in the quarter). The following table provides a list of possible type conversions.
Function Description int(x) Converts x to an integer float(x) Converts x to a floating-point number
str(x) Converts x to a string representation long(x) Converts x to a long integer chr(x) Converts integer x to a character
Disc. # 04-11-
Let’s see a few examples:
There are six sequence types in Python, and list is one of them. Sequence can be thought of as a data structure where you can store large and related data for future reference. List is a mutable sequence; by mutable I mean you can alter the content of a list. You will see immutable sequences in Prof. Ludaescher’s lecture later.
Lists are constructed with square brackets [ ], separating items with commas: [a, b, c] is a list of three characters. We can do lots of operations on list, and here are some interesting ones:
Disc. # 04-11-
So, you need to give a variable name, x in this example, a list of things, (1, 2, 3, 4, 5) in this example, and some stuff to do once for each item in the list. The “stuff” here is called the loop body. The list doesn’t have to be written as (1, 2, 3, 4, 5), you can also use [ ]. See the following example:
>>> for x in [1,2,3,4,5]: print x,
1 2 3 4 5 >>>
Why both () and [] work? We will look at this later in the quarter, for now it is sufficient to remember that you can use both to write out a list (although we cannot use the term list for both notations)
Often you just want to do something 5 times, so you could say for x in 1,2,3,4,5: but what if you wanted 2000 repetitions? Use your friend range()
>>> print range(5) [0, 1, 2, 3, 4] >>> >>> for x in range(20): print x,
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 >>>
So range(n) will give a list of n numbers starting from 0, ending with n-1. With the help of range(), we can have our for loop to repeat executing its body for as many times as required.
Disc. # 04-11-