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:

  1. Numeric values: int, float, complex

  2. Boolean type: bool

  3. String data types: str

Python supports the following collection data types representing a collection of values:

  1. String data types: str

  2. Sequence types: list, tuple

  3. Mapping data type: dict

  4. 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"

img1

6.2.2. Lists / Tuples#

rainbow = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]
rainbow = ("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet")

img1

6.2.3. Sets#

rainbow = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}

img1

6.2.4. Dictionaries#

rainbow = {"Red": 12, "Orange": 4.0, "Yellow":72.0, "Green":"Trees", "Blue":"Sad", "Indigo":"56", "Violet":None}

img1

6.2.5. Native Data Types and Data Structures in Python#

Type

Collection

Syntax

Ordered

Indexed

Mutable

Passed By

Duplicates Allowed

strings

characters

"c1c2c3"

value

list

any data type

[v1, v2.. vn]

reference

tuple

any data type

(v1, v2.. vn)

value

set

immutable types

{v1, v2.. vn}

value

dictionaries

any data type *

{k1:v1,k2:v2.. kn:vn}

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):

  1. Create (aka initialization)

  2. Read (aka get element)

  3. Update (aka append, insert, change, push)

  4. Delete (aka remove, pop)

Type

Create

Read

Update

Delete

strings

foo = "c1c2c3"

foo[idx]

N/A
foo.replace(val1, val2)

N/A
foo.replace(val, "")

list

foo = [v1, v2.. vn]

foo[idx]

foo[idx] = val

foo.remove(val) or foo.pop(idx)

tuple

foo = (v1, v2.. vn)

foo[idx]

N/A
Convert to list

N/A
Convert to list

set

foo = {v1, v2.. vn}

N/A
val in foo

N/A

foo.pop() or foo.remove()

dictionaries

foo = {k1:v1,.. kn:vn}

foo[key]

foo[key] = val

foo.pop(key)

Type

Create

Read

Update

Delete

strings

"c1c2c3"

foo[idx]

N/A

foo.replace(v1, v2)

N/A

foo.replace(val, "")

list

[v1,.. vn]

foo[idx]

foo[idx] = val

foo.remove(val) or foo.pop(idx)

tuple

(v1,.. vn)

foo[idx]

N/A

Convert to list

N/A

Convert to list

set

{v1,.. vn}

N/A

val in foo

N/A

foo.pop() or foo.remove()

dictionaries

{k1:v1,.. kn:vn}

foo[key]

foo[key] = val

foo.pop(key)

Type

Create

Insert

Read

Update

Delete

strings

"c1c2c3"

foo + "a"

foo[idx]

N/A

N/A

list

[v1,.. vn]

foo.append(val)

foo[idx]

foo[idx] = val

foo.remove(val)

tuple

(v1,.. vn)

N/A

foo[idx]

N/A

N/A

set

{v1,.. vn}

foo.add(val)

N/A

N/A

foo.remove(val)

dictionaries

{k1:v1,.. kn:vn}

foo[key] = val

foo[key]

foo[key] = val

foo.pop(key)