'jason'
0b: Python Basics I
As a start, we’ll learn to read, run, modify, and reason about small pieces of Python.
This segment is about basic data types and built-in data structures. There’s no need to memorize syntax; you’ll find that you retain the stuff you use often and can look up the rest.
Object types
Strings, integers, floats, booleans, None, and dates.
Data structures
Lists and dictionaries.
Learning habit
Run small examples, edit them, and observe the changes (if any).
Everything in Python is an object.
Objects have attributes and methods that allow us to work with them.
Variables in Python can be thought of as names we give to objects.
Names point to objects, but they are distinct. As we’ll see, an object can have no name, one name, or many names. In general, names let us refer to objects without recreating them.
You have likely worked with data type issues in existing stats software.
destring to change a number represented as text into a numeric datatype.Same as math: counting numbers, zero, and negatives.
Integer literals are numbers without decimals.
Addition: 5
Subtraction: -1
Multiplication: 6
Exponentiation: 9
Convert with int().
Any number with a decimal.
Stored as an approximation, but it is rarely an issue for our use.
19.99
123.456
6.02
Scientific notation works, too. Convert with float().
Ints can be added, subtracted, multiplied, and exponentiated.
Anything with a float, or int division, returns a float.
One Zero or more characters.
That includes special characters like newlines.
Convert with str().
None
A deliberate null value.
Example: result = None
Datetime
Dates, times, and differences between them.
Example: datetime.datetime.now()
Package types
Packages add many more object types. We can also make our own, though that is mostly beyond our scope.
Lists are data structures that contain a sequence of objects.
They can contain many types of objects, though usually we use one main type at a time.
Dictionaries are data structures with key-value pairs.
Keys must be unique and immutable.
msft
{'ticker': 'msft', 'founding_year': 1975}
We often use dictionaries for lookups or as a lightweight representation of a row of data.
Open notebooks/0b_python1.ipynb.