Download Lecture 4 Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Vincent's theorem wikipedia , lookup

Transcript
Strings and Lists
Week 4 Lecture
Important Definitions
 Comments: #  to describe parts of code to help human reading code
understand why code exists
 Methods: a kind of function attached to a particular type  int methods, str
methods, bool methods (First argument is an object of the type :
‘brown’.capitalize(), however functions are like: str(17) or math.sin(x) or
math.sqrt(x)
 Methods such as __add__ (beginning and ending with two underscores are
considered special by python, never by programmers
 Ex: ‘TTA’ + ‘GGG’ same as ‘TTA’.__add__(‘GGG’)  ‘TTAGGG’
 Module: a kind of object that contains functions and other variables
 Class: a kind of object similar to module except contains methods for a
type
String Methods
 Class: str represents str type (strings)
 Python has builtin methods for str only
 <<expression>>.<<method_name>>(<<arguments>>)
String Methods
 Chaining Method Calls:
 ‘Computer Science’.swapcase().endswith(‘ENCE’)
Worksheet 1 + Worksheet 2: String
Methods
Looping in Strings
 Looping in strings will loop over every single character of the string.
 General form:
 For <<variable>> in <<str>>:
 <<block>>
 Loop variable gets assigned a single character every time:
>>> country = ‘North America Canada’
>>>for ch in country:
if ch.isupper():
print(ch)
Worksheet 3: For Loops over Strings
Lists
 A list is an object , assigned to a variable, contains many items referred to
as elements
 Ex:
We Can create a list to keep track of 14 day whale counts:
>>> whales = [5,4,7,3,2,3,2,6,4,2,1,7,1,3]
Lists
 Lists are objects but also contain memory addresses of the elements they
hold
 The elements are ordered and can be accessed using index indicating
their position in list , just like strings
 First item  index 0..so on up to max length of list, also can use negative
index to access last item
More on Lists
 Empty list [] contains no elements
 Lists can contain any type of data  heterogeneous
 Ex: krypton = [‘krypton’, ‘Kr’, -157.2, -153.4]
 List of information about krypton : name, symbol, melting and boiling points
Operations on Lists
Some operations modify lists, some do not.
“IN” operator used on lists as well to check if element is in list
Slicing list[i:j]  start at index i up to but not including index j
Lists …..
 Alias: alternative name for something  2 variables are said to be aliases
when they contain same memory address
 Used when using list parameters, it creates a new list that refers to
argument passed
 EX: def remove_last_item
List Methods
 All lists modify list instead of creating a new list
Looping over numbers : range function
 Same as we did with strings but ints and floats
 Allows us to perform tasks a certain number of times
Looping until Condition Reached
 While <<expression>>:
 <<block>>
 Python executes while loop , it evaluates expression/condition, if expression is true ,
executes body of loop once then goes back to the top and re evaluates expression ,
if expression is false, ends execution of loop… keep repeating
 rabbits = 3
 While rabbits > 0:
 Print(rabbits)
 rabbits = rabbits -1
 Infinite Loop: never stop , to kill them in idle must either restart shell or CTRL-C
Worksheet 4: List operations and
Methods, Function Range
Worksheet 5: While Loops
Worksheet 6: Parallel Strings and Lists
Difference between Return and Print
 See code print_return.py
Worksheet 7: For Loops over Lists