Python Data Types Flashcards
Numeric Types
Int, Float, Complex
Int
Ints are considered a primitive scalar type
Ints allow for unlimited precision (as many digits as you need)
Integers are always rounded towards zero
int(3.9) = 3
int(-3.9)=-3
float
float = floating point number
any number containing a decimal is interpreted as a float in python
None
None = null object (represents the absence of a value)
We can assign a value as None using the assignment operator: a = None
We can test if something is None using the “is” operator
a is none // True
bool
bool = boolean objects (True / False)
represents logical states and play an important role in Python control flow structures
str
str = string
this is a collection type
strings are sequences of chars
strings are IMMUTABLE meaning once you have constructed a string, you cannot modify its contents
tuple
( )
a tuple is a collection
a tuple is:
- ordered
- immutable (we cannot add or remove items)
- allows duplicates
similar syntax to lists, but they use parentheses
set
{ }
A set is a collection
sets are
- unordered
- unindexed
- mutable
- no duplicates
items within a set are unchangeable, but you can remove items and add new ones
A common use for a set is to remove duplicate items from a series of objects
list
[ ]
A list is a collection
Lists are:
- ordered
- mutable
- allow duplicate values
- indexed
range
range = a sequence representing an arithmetic progression of integers, generated by calls to the range constructor
range determines what it’s arguments mean by counting them
one arg = range(stop)
two arg = range(start, stop)
three args = range(start, stop, step)
dict
{ }
dicts are a mapping type
store values as key-value pairs
- iterable
- keys are immutable, unique (strings, numbers, tuples)
- values are mutable
as of Python 3.7, dicts are ordered
Primitive
Non-Primitive
Immutable
Sequence
Type of Collection (there are three: sequence, set, mapping)
Ordered
It’s elements can be accessed using a numeric index
examples: lists, tuples, ranges