Week 2 Learning Summary
Week 2 subject 1: Strings
- String is a Python built-in data type.
- There are string variables and string literals.
- string literals are a sequence of characters between a pair of quotation marks.
- Use of different quotation marks:
- triple quotes for multi-line string literals and,
- Modify string literals with embedded values and f-strings.
- Adding and multiplying strings: note the difference between concatenating strings and printing multiple strings.
Week 2 subject 2: Lists
- List is a Python advance data type.
- There are list variables and list literals.
- List is a set of objects, can be of different types.
- List elements can be accessed with subscripts (which makes Lists iterables), subscript ranges start at 0.
- You can have lists as elements of lists. Note one data element is different from one list with only one data element. Example: “abc” is different from [“abc”].
- Operating Lists: .append() method, del() function (note they have different syntax).
- Slicing: [begin:end:step].
- Splitting strings into lists, joining lists into strings.
- Adding and multiplying lists.
1 comment:
Pay special attention to how negative subscripts are used:
>>> str = "do you have money?"
>>> len(str)
18
>>> str[1:10]
'o you hav'
>>> str[:10]
'do you hav'
>>> str[-1:10]
''
>>> str[-1:]
'?'
>>> str[0:-1]
'do you have money'
>>> str[0:-3]
'do you have mon'
Post a Comment