6.2. Basic Elements of Computer Programming#
Computer programming, at its core, is the task of combining simple ideas to form more complex ideas.
To that end, every programming language supports the following three basic features:
Primitive elements, which represent the simplest building blocks that the language provides
For example: variables, input, output, math operations, if-else and loops.
Means of combination, by which compound elements are built from simpler ones
For example: nested loops using loops, nested lists using lists.
Means of abstraction, by which compound elements can be named and manipulated as units
For example: functions, classes
The most essential element is Data: the quantities, characters, or symbols, stored and transmitted as electrical signals, on which operations are performed by a computer.
The atomic unit of data is a value
Python supports the following atomic data types representing single values:
Numeric values:
int
,float
,complex
Boolean type:
bool
String data types:
str
Python supports the following collection data types representing a collection of values:
String data types:
str
Sequence types:
list
,tuple
Mapping data type:
dict
Set data types:
set
You can check a variable’s data type using the built-in function type
Data Structures are abstractions built on top of these data types.
Each data structure corresponds to a particular organization of data.
This organization imposes constraints and affordances on the retrieval and processing of data.
For example,
6.2.1. Strings#
txt = "RAINBOW"
6.2.2. Lists / Tuples#
rainbow = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]
rainbow = ("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet")
6.2.3. Sets#
rainbow = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}
6.2.4. Dictionaries#
rainbow = {"Red": 12, "Orange": 4.0, "Yellow":72.0, "Green":"Trees", "Blue":"Sad", "Indigo":"56", "Violet":None}
6.2.5. Native Data Types and Data Structures in Python#
Type |
Collection |
Syntax |
Ordered |
Indexed |
Mutable |
Passed By |
Duplicates Allowed |
---|---|---|---|---|---|---|---|
|
characters |
|
✓ |
✓ |
✗ |
value |
✓ |
|
any data type |
|
✓ |
✓ |
✓ |
reference |
✓ |
|
any data type |
|
✓ |
✓ |
✗ |
value |
✓ |
|
immutable types |
|
✗ |
✗ |
✗ |
value |
✗ |
|
any data type * |
|
✗ |
✓ |
✓ |
reference |
✗** |
* keys can only be immutable type; values can be any data type
** keys can not be duplicate; values can be duplicate
Four basic operations (CRUD):
Create (aka initialization)
Read (aka get element)
Update (aka append, insert, change, push)
Delete (aka remove, pop)
Type |
Create |
Read |
Update |
Delete |
---|---|---|---|---|
|
|
|
N/A |
N/A |
|
|
|
|
|
|
|
|
N/A |
N/A |
|
|
N/A |
N/A |
|
|
|
|
|
|
Type |
Create |
Read |
Update |
Delete |
---|---|---|---|---|
|
|
|
N/A |
N/A |
|
|
|
|
|
|
|
|
N/A |
N/A |
|
|
N/A |
N/A |
|
|
|
|
|
|
Type |
Create |
Insert |
Read |
Update |
Delete |
---|---|---|---|---|---|
|
|
|
|
N/A |
N/A |
|
|
|
|
|
|
|
|
N/A |
|
N/A |
N/A |
|
|
|
N/A |
N/A |
|
|
|
|
|
|
|