Python Programming Essentials: Input, Conditionals, Data Structures, and 2D Lists

User Input and Data Type Conversion

  • input() Function: Displays an optional prompt string on the console, pauses program execution, and waits for user input. Execution resumes when the user presses Enter.

  • Default Data Type: The input() function always returns user input as a string (String).

  • Typecasting: Converts input data types using built-in functions such as int(), float(), or str() to perform arithmetic operations.

    • height = input("Enter the giraffe's height: ") stores input as a string.

    • height = int(input("Enter the giraffe's height: ")) converts and stores input as an integer.

Conditional Logic

  • Branching Statements: Uses if, elif, and else blocks to direct program execution paths based on evaluated conditions.

  • elif Clause: Allows testing of multiple sequential conditions if preceding if or elif statements evaluate to false.


if-elif-else conditional structure syntax

Data Structures Comparison

  • Lists:

    • Enclosed in square brackets [] (e.g., Class_8A = ["Choenjur", "Choki", "Tshering", "Tshewang"]).

    • Ordered and mutable collections; elements can be modified, added, or removed.

  • Tuples:

    • Enclosed in parentheses () (e.g., new_colors = ("blue", "yellow", "brown", "red")).

    • Ordered and immutable collections; elements cannot be modified, added, or deleted after creation.

  • Sets:

    • Enclosed in curly braces {}.

    • Unordered collections of unique elements; duplicate values are automatically ignored.

    • Set methods:

    • add(): Inserts an element into the set if it is not already present.

    • pop(): Removes and returns an arbitrary element from the set.

    • remove(): Removes a specified element from the set.


Balloons stored in a set data structure
  • Dictionaries:

    • Enclosed in curly braces {} containing key : value pairs.

    • Keys must be unique; duplicate keys are not permitted.

Two-Dimensional Lists

  • Definition & Access: A 2D list is a list of lists representing a grid. Elements are accessed using row and column coordinates in the format list[row][column].

  • Memory Representation: Variables act as pointers in computer memory. Indexing a 2D list follows pointers from the outer list to the targeted inner list.

  • Chained Assignment: Multiple variable locations can be updated simultaneously using continuous assignment operators.

  • The None Keyword: Represents a missing value or empty state in memory (distinct from numerical 00 or empty lists).


Chained assignment syntax with None and Cloud objects
  • Row Iteration: Iterating over elements in a single row is accomplished using a loop over range(len(list[0])).