Py4e Data Structure
Py4e Data Structure
STRINGS
A string is a sequence of charaters
A string literal uses quotes “hello”
For strings, + means “concatenate”
When a string contains numbers, it is still a string
We can convert nubers in a string into a number using int()
Zot=”abc”
print(zot[5])
error
Len function
A function is come stored code that we use. A function takes some input and produces
an output
SLICING STRINGS
We can also look at any continuous section of a string using a colon operator
The second number is one beyond the end of the slice – “up to bout not including”
If the second number is beyond the end of the string, it stops at the end
MANIPULATING STRINGS
STRING CONCATENATION
When the + operator is applied to strings, it means “concatenation”
STRING LIBRARY
Pythin has a number of string functions which are in the string library
These functions are already built into every string – we invoke them by appending the
function to the string variable
These functions do not modify the original string, instead they return a new string that
has been altered
SEARCHING A STRING
We use the find() function to search for a substring within another string
Find() finds the first occurrence of the substring
If the substring is not found, find() returns -1
Remember that string position starts at zero
STRIPPING WHITESPACE
Sometimes we want to take a string and remove whitespace at the beginning and/or
end
lstrip() and rstrip() remove whitespace at the left or right
strip() removes both beginning and ending whitespace
FILES
OPENING A FILE
before we can read the contents of the file, we must tell python which file we are
going to work with and what we will be doing with the file
this is donde with the open() function
open() return a “file handle” – a variable used to perform operations on the file
similar to “file -> open” in a word processor
USING OPEN()
handle= open(filename,mode), ejemplo fhand=open(“mbox.txt”,“r”)
returns a handle use to manipulate the file
filename is a string
mode is optional and should be “r” if we are planning to read the file and “w” if we are
going to write to the file
PROCESSING FILES
a file handle open for read can be trated as a sequence of strings where each line in
the file is a string in the sequence
we can use the for statement to iterate through a sequence
remember – a sequence is an ordered set
HANDLE=OPEN(“MBOX.TXT”)
count=0
for line in handle:
count=count+1
print(“line count:”,count)
fhand= open(“mbox-short.txt”)
inp=fhand.read()
print(len(inp))
print(inp[:20])
fhand= open(“mbox-short.txt”)
for line in fhand:
if line.startswith(“From: “)
print(line)
fhand= open(“mbox-short.txt”)
for line in fhand:
line= line.rstrip()
if not line.startswith(“From: “):
continue
print(line)
fhand= open(“mbox-short.txt”)
for line in fhand:
line= line.rstrip()
if not “@uct.ac.za” in a line:
continue
print(line)
LISTS
PROGRAMING
Algorithms: A set of rules or steps used to solve a problem
X=2
x=4
print(x)
4
LIST CONSTANTS
List constants are sirrounded by square brackets and the elements in the list are
separated by commas
A list element can be any python object – even another list
A list can be empty
MANIPULATING LISTS
CONCATENATING LIST USING (+)
We can create a new list by adding two existing lists together
IS SOMETHING IN A LIST?
Python provides two operators that let you check if an item is in a list
These are logical operators that return true or false
They do not modify the list
LIST ARE IN ORDER
A list can hold many items and keeps those items in the order until we do soething to
change the order
A list can be sorted – change its order
The sort method –unlike in strings- means “sort yourself”
Words=line.split()
email=words[1]
pieces=email.split(“@”)
DICTIONARIES
WHAT IS A COLLECTION
A collection is nice because we can put more than one value in it and carry them all
around in one convenient package
We have a bunch of values in a single variable
We do this by having more than one place in the variable
We have ways of finding the different places in the variable
DICTIONARIES
List index their entries based on the position in the list
Dictionaries are like bags – no order
So we index the things we put in the dictionary with a lookup tag
DICTIONARY TRACEBACKS
It is an error to reference a key which is not in the dictionary
We can use the in operator to see if a key is in the dictionary
The general patter to count the words in a line of text is to split the line into words, then loop
through the words and use a dictionary to track the count of each word independently
Even though dictionaries are not stored in order, we can write a for loop that goes through all
the entries in a dictionary, actually it goes through all of the keys in the dictionary and loopks
up the value