Python Data Types (With Animations)
Python Data Types (With Animations)
Algebraic expressions
The Python interactive shell can be used
to evaluate algebraic expressions
5/2 - returns 2 in Python 2 >>> 2 + 3
- returns 2.5 in Python 3 >>>
5 2 + 3
(from __future__ import division) 5
>>> 7 - 5
>>>
2 2
7 +
- 3
5
5
2
>>> 2*(3+1)
2**3 is 2 to the 3rd power 8
>>> 7 - 5
2*(3+1)
8
>>>
2 5/2
2.5
>>> 5/2
2*(3+1)
abs(), min(), and max() are functions 8
2.5
>>> 5/2
2.5
• abs() takes a number as input and >>> 2**3
returns its absolute value 8
>>> abs(-3.2)
3.2
• min() (resp., max()) take an arbitrary >>> min(23,41,15,24)
number of inputs and return the 15
>>> max(23,41,15,24)
“smallest” (resp., “largest”) among them 41
2
Introduction to Computing Using Python
Algebraic expressions
14//3 is the quotient when 14 is divided
by 3 and 14%3 is the remainder
Boolean expressions
6
Introduction to Computing Using Python
7
Introduction to Computing Using Python
"Hello, World!'
'Hello, World!"
A string value is represented as a sequence
of characters enclosed within (single or
double) quotes
10
Introduction to Computing Using Python
12
Introduction to Computing Using Python
Negative index
A negative index is used to specify a position with respect to the “end”
• The last item has index -1,
• The second to last item has index -2,
• The third to last item has index -3, …
-5 -4 -3 -2 -1
s = 'A p p l e'
0 1 2 3 4
s[-1] = 'e'
>>> s = 'Apple'
s[-2] = 'l' >>> s[-1]
'e'
s[-5] = 'A' >>> s[-2]
'l'
>>> s[-5]
'A'
15
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Python Data Types
16
Introduction to Computing Using Python
String s is defined to be
'abcdefgh'
following strings:
a) 'a'
b) 'c'
c) 'h'
d) 'f'
17
Introduction to Computing Using Python
Lists
['ant',
[0, 1, 'two',
2,
'bat',
3, 4,'three',
'cod',
5, 6, 'dog',
7,
[4,8,'five']]
9,
'elk']
10]
A comma-separated sequence of items enclosed within square brackets
18
Introduction to Computing Using Python
There are also functions that are called on a list; >>> lst = [1, 2, 3]
such functions are called list methods >>> len(lst)
3
>>> sum(lst)
lst.append(7) 6
>>> lst.append(7)
>>> lst
[1, 2, 3, 7]
variable lst input argument 7 `
>>>
refers to a
list object
list method
append() Method append() can’t be called
independently; it must be called on
some list object
21
Introduction to Computing Using Python
Lists methods
>>> lst = [1, 2, 3]
Usage Explanation >>> lst.append(7)
lst.append(item) Adds item to the end of lst >>> lst.append(3)
>>> lst
lst.count(item) Returns the number of times item [1, 2, 3, 7, 3]
occurs in lst >>> lst.count(3)
2
lst.index(item) Returns index of (first occurrence of) >>> lst.remove(2)
item in lst >>> lst
[1, 3, 7, 3]
lst.pop() Removes and returns the last item in lst >>> lst.reverse()
>>> lst
lst.remove(item) Removes (the first occurrence of) item [3, 7, 3, 1]
from lst >>> lst.index(3)
0
lst.reverse() Reverses the order of items in lst >>> lst.sort()
>>> lst
lst.sort() Sorts the items of lst in increasing [1, 3, 3, 7]
order >>> lst.remove(3)
>>> lst
Methods append(), remove(), reverse(), [1, 3, 7]
and sort() do not return any value; they, along >>> lst.pop()
7
with method pop(), modify list lst >>> lst 22
[1, 3]
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Python Data Types
23
Introduction to Computing Using Python
List lst is a list of prices for a pair of boots at different online retailers
24
Introduction to Computing Using Python
An object’s type determines what values it can have and how it can be manipulated
Terminology: object X is of type int = object X belongs to class int 26
Introduction to Computing Using Python
We saw the operations that can be performed on classes int and float
>>> pets
fish = ['goldfish',
['goldfish'] 'cat', 'dog']
>>> pets.append('guinea
myPets = ['cat', 'dog']
pig')
The list class supports: >>> pets.append('dog')
fish * 3
>>>
['goldfish',
pets 'goldfish', 'goldfish']
• operators such as +, *, in, ['goldfish',
>>> pets = fish
'cat',
+ myPets
'dog', 'guinea pig', 'dog']
[], etc. >>> pets.count('dog')
pets
• methods such as 2
['goldfish', 'cat', 'dog']
append(), count(), >>> pets.remove('dog')
'frog' in pets
>>>
False
pets
remove(), reverse(), ['goldfish',
>>> pets[-1] 'cat', 'guinea pig', 'dog']
etc. >>>
'dog'
pets.reverse()
>>> pets
31
['dog', 'guinea pig', 'cat', 'goldfish']
Introduction to Computing Using Python
Many more functions and classes are defined in the Python Standard Library
to support
• Mathematical functions: math, numpy, scipy, random
• Data Science/Machine Learning: pandas, sklearn, tensorflow, keras
• Data visualization: matplotlib, seaborn, bokeh
• Text mining: nltk, gensim, textblob, wordcloud, word2vec
• Big Data / Database programming: sqlalchemy, pymongo, pyspark
• System/Network programming: os, sys
• Web scraping: beautifulsoup, selenium, requests, scrapy
• Graphical user interface (GUI) development: tkinter, pyqt
The Python Standard Library functions and classes are organized into
components called modules.
32
Introduction to Computing Using Python
The core Python language does not have a square root function
34
Introduction to Computing Using Python
35