Data Structures
Bruno Vieira
Overview
- Introduction to operators
- What are variables and data structures?
- What are the types available?
- What are the methods availabe for each type?
- What purpose each has?
- Application examples?
Arithmetic operators
+
Addition
-
Subtraction
*
Multiplication
**
Exponent
/
Division
//
Floor Division
%
Modulus
Arithmetic examples: + - * /
Arithmetic examples: / //
Variables
"...a variable is a storage location and an associated symbolic name..." in Wikipedia
Assignment statement
This is different from the equal operator ==
Variables forbidden names
The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers.
and del from not while
as elif global or with
assert else if pass yield
break except import print None
class exec in raise _*
continue finally is return __*__
def for lambda try __*
More information
Python Documentation: Identifiers
Assignment operators
=
Simple assignment operator
+=
Add AND assignment operator
-=
Subtract AND assignment operator
*=
Multiply AND assignment operator
/=
Divide AND assignment operator
//=
Floor Dividion and assigns a value
%=
Modulus AND assignment operator
**=
Exponent AND assignment operator
Data types
- Numbers (integers and floats)
- Strings
- Lists
- Tuples
- Sets
- Dictionaries
More information
Python Documentation
Integers and floats
- Numbers are created by numeric literals or as the result of built-in functions and operators
- Numeric literals containing a decimal point or an exponent sign yield floating point numbers
- Python fully supports mixed arithmetic
Integers and floats assignment
Strings
- String literals are written in single or double quotes
- In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained)
- Strings are immutable sequence types: such objects cannot be modified once created
Strings assignment
Please try the following lines in the editor below
print "hello world"
print 'hello world'
print 'hello" "world'
print "hello' 'world"
print 'hello"
print "world'
Strings assignment multiline
""" or '''
Strings slices
variable = "string"
variable[start:stop:step]
Strings slices with negative indices
Strings slices with steps
Strings slices out of range
Strings methods
capitalize center count decode encode endswith expandtabs find format index isalnum isalpha isdigit islower isspace istitle isupper join ljust lower lstrip partition replace rfind rindex rjust rpartition rsplit rstrip split splitlines startswith strip swapcase title translate upper zfill
Total: 38

Strings methods
count endswith find islower isupper join lower lstrip replace rfind rsplit rstrip split splitlines startswith strip swapcase translate upper
Total: 19

Strings methods
- count
- find
- replace
- startswith and endswith
- islower and isupper
- lower, upper and swapcase
- join
- strip
- translate
- split and splitlines ← later with Lists
Total: 10
String count
str.count(sub[, start[, end]])
String replace
str.replace(old, new[, count])
String translate
str.translate(table[, deletechars])
string.maketrans(from, to)
String startswith and endswith
str.startswith(suffix[, start[, end]])
String find and rfind
str.find(sub[, start[, end]])
String islower and isupper
str.islower()
String lower, upper and swapcase
str.lower()
String join
str.join(iterable)
String strip, lstrip and rstrip
str.strip([chars])
Data Structures
- Numbers
- Strings
- Lists
- Tuples
- Sets
- Dicts
Lists assignment
[a, b, c]
Lists assignment
str.split([sep[, maxsplit]])
Lists assignment
str.splitlines([keepends])
Lists assignment
str.splitlines([keepends])
Lists are a mutable sequence type
Lists methods
- count
- index
- append
- insert
- remove
- pop
- reverse
- sort
More information
Python Documentation
Lists count, index, append and insert
Data Structures
- Numbers
- Strings
- Lists
- Tuples
- Sets
- Dicts
Tuples
- Lists that are immutable (like strings)
- Useful for storing heterogeneous data in which order has semantic value (like coordinates)
- Fast!!!
More information
Python Docs: Tuples and Sequences
Data Structures
- Numbers
- Strings
- Lists
- Tuples
- Sets
- Dicts
Sets
- Unordered collection with no duplicate elements
- Uses include membership testing and eliminating duplicate entries
- Also support mathematical operations like union, intersection, difference, and symmetric difference.
More information
Python Docs: Sets
Data Structures
- Numbers
- Strings
- Lists
- Tuples
- Sets
- Dicts
Dictionaries
- Unordered set of "key: value" pairs, with the requirement that the keys are unique
- Known in other languages as "associative memories" or "associative arrays"
- Indexed by keys (unlike sequences, which are indexed by a range of numbers)
- Indices can be any immutable type (strings, numbers, or tuples of immutable objects)
- Usefull for storing, extracting or deleting values with a key
Dictionaries assignment
{'a': 1, 'b': 2, 'c': 3}
{'key1': "Value", 'key2': "Value", 1: "Another Value", 'd': 42}
Wrap up
- Arithmetic operators
+ - * / // % **
- Assignment operators
+= -= *= /= //= %= **=
- Numbers and Strings
a = 1; b = "Hello World"
- String methods
count, find, join, translate, split, etc
- Lists and methods
a = [1, 2]; append, pop, reverse, sort, etc
- Some Built-in functions
range, len, min, max
- Tuples
a = 1, 2, 3; b = (1, 2, 3)
- Sets
a = set([1, 2, 3])
- Dictionaries
a = {a: 1, b: 2, c: 3}
←→/
#