Basic Strings List Tuples
Basic Strings List Tuples
Basics of Python
Python Identifiers:
• A Python identifier is a name used to identify a variable, function, class, module, or
other object. An identifier starts with a letter A to Z or a to z or an underscore (_)
followed by zero or more letters, underscores, and digits (0 to 9).
• Python accepts single ('), double (") and triple (''' or """) quotes to
denote string literals, as long as the same type of quote starts and
ends the string.
• The triple quotes can be used to span the string across multiple
lines. For example, all the following are legal:
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made
up of multiple lines and sentences."""
Comments in Python:
A hash sign (#) that is not inside a string literal begins a comment. All characters after the #
and up to the physical line end are part of the comment, and the Python interpreter
ignores them.
Using Blank Lines:
• A line containing only whitespace, possibly with a comment, is known as a blank line,
and Python totally ignores it.
• In an interactive interpreter session, you must enter an empty physical line to terminate
a multiline statement.
Variables
• Are not declared, just assigned
• The variable is created the first time you assign it a value
• Are references to objects
• Type information is with the object, not the reference
• Everything in Python is an object
• Everything means everything, including functions and classes (more on
this later!)
• Examples: x = 5
gpa = 3.14
x 5 gpa 3.14
• A variable that has been given a value can be used in expressions.
x + 4 is 9
Comments
• Start comments with # – the rest of line is ignored.
• Can include a “documentation string” as the first line of any new function or class
that you define.
• The development environment, debugger, and other tools use it: it’s good style to
include one.
def my_function(x, y):
“““This is the docstring. This
function does blah blah blah.”””
# The code would go here...
Type and Help
type() returns the type of variable passed
>>> type(2)
<class 'int'>
The single and double quotation mark string objects, which are collections of
texts surrounded by quotes.
Printing: Examples
>>> print ("Hello, World! ")
Hello, World!
>>> "hello"
'hello'
>>> "world"
'world'
>>> "hello"+"world"
'helloworld'
Some integer operations
Operation Result
x+y sum of x and y
x-y difference of x and y
x*y product of x and y
x/y
x // y Some integer operations
quotient of x and y
floored quotient of x and y
x%y remainder of x / y
-x x negated
+x x unchanged
abs(x) absolute value or magnitude of x
int(x) x converted to integer
float(x) x converted to floating point
a complex number with real part re, imaginary
complex(re, im)
part im. im defaults to zero.
pow(x, y) x to the power y
x ** y x to the power y
Strings
Strings are immutable – you cannot update the value of an existing string
object.
Operation Result
b a n a n a
The built-in function len gives 0 1 2 3 4 5
us the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
'banana' len() 6
(a string) function (a number)
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
for x in y:
(a string) blah
(a number)
blah
Looping Through Strings
print(letter)
The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
More String Operations
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a string
using a colon operator >>> s = 'Monty Python'
>>> print(s[0:4])
• The second number is one Mont
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
>>> print(s[6:20])
• If the second number is
Python
beyond the end of the string,
it stops at the end
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
https://docs.python.org/3/library/stdtypes.html#string-methods
String Library
str.capitalize() str.replace(old, new[, count])
str.center(width[, fillchar]) str.lower()
str.endswith(suffix[, start[, end]]) str.rstrip([chars])
str.find(sub[, start[, end]]) str.strip([chars])
str.lstrip([chars]) str.upper()
Searching a String
b a n a n a
• We use the find() function to search
for a substring within another string
0 1 2 3 4 5
• It replaces all
>>> nstr = greet.replace('o','X')
>>> print(nstr)
occurrences of the HellX BXb
search string with the >>>
replacement string
Stripping Whitespace
• Sometimes we want to take
a string and remove
whitespace at the beginning >>> greet = ' Hello Bob '
>>> greet.lstrip()
and/or end
'Hello Bob '
• lstrip() and rstrip() remove
>>> greet.rstrip()
' Hello Bob'
whitespace at the left or right >>> greet.strip()
'Hello Bob'
• strip() removes both >>>
beginning and ending
whitespace
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
Parsing and
21 31 Extracting
From [email protected] Sat Jan 5 09:14:16 2008
Data Structure
- A particular way of organizing data in a computer
https://en.wikipedia.org/wiki/Algorithm
https://en.wikipedia.org/wiki/Data_structure
What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable, the old value is overwritten
$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of
Collection
• A collection allows us to put many values in a single “variable”
http://docs.python.org/tutorial/datastructures.html
Building a List from Scratch >>> stuff = list()
We can create an empty list and >>> stuff.append('book')
then add elements using the >>> stuff.append(99)
append method >>> print(stuff)
['book', 99]
The list stays in order and new >>> stuff.append('cookie')
>>> print(stuff)
elements are added at the
['book', 99, 'cookie']
end of the list
Is Something in a List?
Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
True
in a list
>>> 15 in some
False
These are logical operators that >>> 20 not in some
return True or False True
>>>
They do not modify the list
Lists are in Order
• A list can hold many
items and keeps
those items in the
order until we do >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> friends.sort()
something to change >>> print(friends)
the order ['Glenn', 'Joseph', 'Sally']
>>> print(friends[1])
• A list can be sorted Joseph
(i.e., change its order) >>>
Split breaks a string into parts and produces a list of strings. We think of these
as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces'] ● When you do not specify a
>>>
>>> line = 'first;second;third' delimiter, multiple spaces are
>>> thing = line.split()
>>> print(thing) treated like one delimiter
['first;second;third']
>>> print(len(thing))
1 ● You can specify what delimiter
>>> thing = line.split(';')
>>> print(thing) character to use in the splitting
['first', 'second', 'third']
>>> print(len(thing))
3
>>>
From [email protected] Sat Jan 5 09:14:16 2008
words = line.split()
email = words[1]
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] [email protected]
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] [email protected]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print(pieces[1]) 'uct.ac.za'
List Summary
• Concept of a collection • Slicing lists
>>> t = tuple()
>>> dir(t)
['count', 'index']
Tuples are More Efficient
• Since Python does not have to build tuple structures to be
modifiable, they are simpler and more efficient in terms of
memory use and performance than lists
• So in our program when we are making “temporary variables”
we prefer tuples over lists
Tuples and Assignment
• We can also put a tuple on the left-hand side of an assignment
statement
• We can even omit the parentheses
>>> (x, y) = (4, 'fred')
>>> print(y)
fred
>>> (a, b) = (99, 98)
>>> print(a)
99
Tuples and Dictionaries
>>> d = dict()
>>> d['csev'] = 2
>>> d['cwen'] = 4
The items() method >>> for (k,v) in d.items():
in dictionaries ... print(k, v)
returns a list of (key, ...
csev 2
value) tuples
cwen 4
>>> tups = d.items()
>>> print(tups)
dict_items([('csev', 2), ('cwen', 4)])
The comparison operators work with tuples and other
sequences. If the first item is equal, Python goes on to the next
element, and so on, until it finds elements that differ.
lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)