# ESSENTIAL COMPUTATIONAL THINKING CHAPTER 03 Dr Ricky J

- Slides: 41

ESSENTIAL COMPUTATIONAL THINKING: CHAPTER 03 Dr. Ricky J. Sethi Essential Computational Thinking

Chapter 3 Computational Thinking and Structured Programming

Review of Computation A task can be said to be computable if we can enumerate a sequence of instructions in order to complete the task The sequence of instructions, called an algorithm, is carried out by some computing agent Turing Machines represent a formal, mathematical notion of computation � In 1936, Turing showed that his hypothetical mathematical machines could solve every mathematical problem as long as there was an algorithm for it. � Very simple devices which only had a tape head an infinitely long tape to read or write symbols � Tape head is controlled by

Digital Computers and Programs All modern digital computers are Turingcomplete and can do everything a Turing Machine can do. Modern computers are multi-purpose machines that use changeable programs to accomplish certain tasks. A changeable program is an algorithm, the detailed, step-by-step ordered set of instructions that tell a computer exactly what to do.

Summary: Modern Digital Computers Three main aspects of digital electronic computers: � Computers are devices for manipulating information derived from the input data. � Programs are the instructions that a computer carries out or executes. These instructions are also called the algorithm, which is the computational solution to some computable problem. � Computers operate under the control of a changeable program. They follow the von Neumann architecture and store both the data and the programs in the same memory space.

Evolution of Computers: Ancient French merchant Joseph-Marie Jacquard invented the programmable loom in 1804 Jacquard’s programmable loom could be operated using interchangeable metal punched cards for memory to represent different thread patterns These patterns of holes and non-holes are the same kind of binary representation that can be utilized in Boolean logic In 1833, Babbage expanded upon the Difference Engine and designed the Analytical Engine, a programmable computer that also used metal punched cards for input and printed output on paper. The Analytical Engine was a general purpose computer that could perform any mathematical operation � � � Like modern computers, the Analytical Engine contained an arithmetic processing control unit for fetching and executing instructions (called the mill) Also contained a readable/writeable memory for holding data and programs (called the store) and input/output devices (the punched metal cards) It also used stored programs to modify its behaviour by modifying punched metal cards. It even had a conditional branching statement, thus making it Turing complete Popularized by the writings of the English mathematician Lady Ada Lovelace (daughter of Lord Byron), which also contained a step-by-step sequence of instructions (an algorithm!) that could be carried out by the machine in order to compute Bernoulli numbers Ada programming language

Evolution of Computers: Digital Konrad Zuse built general-purpose programmable computer, similar to the Analytical Engine, that was supposed to use vacuum tubes instead of electromechanical relays Z 1, Z 2, Z 3, etc. Next generation of computers used vacuum tubes: � First electronic, general-purpose digital computer was ENIAC (Electronic Numerical Integrator and Computer) Alan Turing helped design the COLOSSUS at roughly the same time in England John von Neumann, also involved with ENIAC, formalized the idea of storing programs in the same memory along with data � � � Helped simplify tedious task of programming via switches and cables von Neumann also recommended representing memory in binary and using the conditional control transfer machine instruction to invoke subprograms by storing the machine’s current state in memory von Neumann architecture still used today was first used in

Modern Digital Computers Modern digital computers use transistors instead of vacuum tubes Programs are the software that control the hardware, the physical machine � � � The central processing unit (CPU) is the brain of a computer and carries out all the basic operations on the input data. CPU is made of switches and gates using transistors Does very simple operations like arithmetic operations that test to see if two numbers are equal using the Arithmetic Logic Unit (ALU) The power of a computer derives from its ability to do these simple operations very fast and very accurately The Fetch-Execute Cycle in the CPU consists of � � � Fetching the next Instruction Decoding that Instruction and Getting the corresponding Data Executing that Instruction

What is Binary? Decimal Positional System Binary Positional System � Smallest representation that’s complete mathematically Can use for a game of 20 Questions, which gives insight into the quantitative idea of Information and the nature of the Universe!

Binary Digit vs BIT CPU speaks binary � Logic gates made of switches which are either ON or OFF Binary Digits are either 0 or 1 � Whole universe can be described in terms of these values of 0 and 1 BITS are a unit of measure and can be fractional � They measure the amount of information

Computer Programs 11 What is a computer program? �A detailed, step-by-step set of instructions telling a computer what to do. � If we change the program, the computer performs a different set of actions or a different task. � The machine stays the same, but the program changes! Programs are the instructions that are executed, or carried out, by the CPU All program instructions are translated to binary � Binary is all the CPU really understands since it

Computational Thinking Basics Computational thinking can be thought of as involving three steps that are used iteratively to help solve the problem at hand: � Problem Specification: analyze the problem using abstraction, decomposition, and pattern recognition; state the problem precisely; and establish the criteria for its solution � Algorithmic Expression: solve the problem using algorithmic thinking, which involves design- ing an algorithm with an appropriate data representation � Solution Implementation & Evaluation: implement the algorithmic solution and systematically test it for correctness and efficiency

Getting Started with Python is three things: � Programming Language � Integrated Development Environment (IDE) � Virtual Machine that executes the program

Python Development Environment Python’s default IDE is IDLE, which bundles together the software tools needed for program development: � An editor, for creating and modifying programs � A translator, for executing programs � A debugger, for taking control of the execution of a program to aid in finding program errors

Behind the Scenes: How Python Works Python has a virtual machine so doesn’t need linker/loader in the same way For compilers, they use a Tokenizer and a Parser (check grammar) Python Language Instructions: Source Code � CPU: Machine Code

Why Python? Simpler Language Python is a simpler language than Java, etc. Simpler means (somewhat subjective): � Easy syntax that doesn’t get in your way � Easy mechanics to let you start writing programs � Lots of modules that allow you to do many things simply and reliably other languages like Java have these, as well This allows us to focus less on the language and more on problem solving � Computer scientists are a language-agnostic bunch: pick the best language for the particular problem

Python Includes Batteries! Python is often described as "batteries included" Many of the best parts of other languages are included in Python � data structures (lists, dictionaries) � control (iteration, exceptions) � many packages/modules for common tasks Python has many modules (libraries) to do a variety of tasks

Running Python Shell Python can be executed interactively in the Python shell. In this mode, executing Python is similar to using a calculator. The >>> symbol is the shell prompt. � Here, typing 2 + 3 at prompt outputs the result 5, again displaying the prompt in wait of another instruction.

The Magic of Python 19 When you start Python, you will see something like: Python 3. 1. 2 (r 312: 79149, Mar 21 2010, 00: 41: 52) [MSC v. 1500 32 bit (Intel)] on win 32 Type "copyright", "credits" or "license()" for more information. >>> print(”Hello World”) Interpreter is like an over-eager doggy: takes instructions, translates to machine language, has microprocessor execute it, displays result, and waits for next command Can also use repl. it or tutorialspoint. com Uses a Parser, Tokenizer Virtual Machine Binary for CPU!

Python Standard Library The Python Standard Library is a collection of built-in modules Each module provides functionality beyond what is included in the “core” part of Python. For example, the math module provides additional mathematical functions. � The random module provides the ability to generate random numbers.

Syntax, Semantic, or Logic Errors Make a single quote in print(“Hello, world!’) and discuss Errors: Syntax Caught by translator � Logical Test driven development (beta users) � Semantic (Yoda) some exceptions are caught by translator � Assigning wrong type of value to a variable Using variable without assigning it a value or in python doing 3/’foo’ (Type. Error) Nested loops with missing braces (Java) or incorrect indentation (Python) Compile time (syntax or static semantic like variable without value) or Run time (dynamic semantic like array bounds and logical) Examples: Ths sntnce hs th vwls mssng � print(“Hello’): A dog. Bit a man. Syntax

The Magic of Python 22 The “>>>” is a Python prompt indicating that Python is ready for us to give it a command. Make a single quote and These commands are called statements. >>> print("Hello, world!“) Hello, world >>> print(2+3) 5 >>> print("2+3=", 2+3) 2+3= 5 >>> discuss Errors: • Syntax • Semantic (Yoda) • Logical Compile time (syntax or static semantic like variable without value) or Run time (dynamic semantic like array bounds and logical)

Formal Model of Computation In 1936, Turing was interested in the question of what it means for a task to be computable Intuitively, a task is computable if it is possible to specify a sequence of instructions which will result in the completion of the task Turing proposed a class of devices to develop a formal notion of computation � Originally called a-machines (automatic machine) now called Turing Machines � Turing reformulated Kurt Gödel's 1931 results on the limits of proof and computation � He replaced Gödel's universal arithmetic-based formal language with these formal and simple hypothetical devices

The Turing Machine 24 Programs are executed, or carried out. � All computers have the same power, with suitable programming I. e. each computer can do the things any other computer can do. A hypothetical Turing Machine can do three things: � Write/Delete a symbol � Move the tape head Left/Right � Go to a new or same state CONCLUSION: a machine with the correct minimal set of operations can calculate anything that is computable

Minimal instruction set All computer programs can be written using just those five instructions. All programs, ever! 1. LOAD A: load contents at RAM address A into accumulator 2. STORE A: store accumulator contents into address A in RAM 3. CLR: clear accumulator 4. INC: increment accumulator 5. BRZ X: branch to address X in RAM if accumulator is zero

Hardware Basics 26 Fetch-Execute Cycle � First instruction retrieved from memory � Decode the instruction to see what it represents � Appropriate action carried out. � Next instruction fetched, decoded, and executed. � Lather, rinse, repeat!

Natural vs Formal Languages 27 Natural languages have ambiguity and imprecision problems when used to describe complex algorithms. � Programs need to be expressed in an unambiguous, precise way using programming languages. � Every structure in a programming language has a precise form, called its syntax � Every structure in a programming language has a precise meaning, called its semantics.

Coding 28 A Programming language is like a special code for writing the instructions the computer will follow. Programmers will often refer to their program as computer code. Process of writing an algorithm in a programming language is often called coding.

Programming Languages 29 High-level computer languages � Designed to be used and understood by humans Low-level language � Computer hardware can only understand a very low level language known as machine language 1 st Generation Language: Machine Language 2 nd Generation Language: Assembly 3 rd Generation Language: interpreted/compiled languages

30 Translating to Machine Language Suppose we have these instructions for the CPU to add two numbers: � Load the number from memory location 2001 into the CPU � Load the number from memory location 2002 into the CPU � Add the two numbers in the CPU � Store the result into location 2003 � Etc. In reality, these low-level instructions are represented in binary (1’s and 0’s) This is also called machine language

Compilers 31 You can express your computer in a high-level language; for example, something like this: c=a+b This code needs to be translated into machine language so that the computer can execute it �A batch, or collection, of such instructions/statemeents is your program Compilers convert programs written in a highlevel language into the machine language for some particular CPU

Interpreters 32 Interpreters simulate a computer that understands a high-level language. The source program is not translated into machine language all at once. An interpreter analyzes and executes the source code instruction by instruction.

Advantages to each… 33 Interpreted languages are part of a more flexible programming environment since they can be developed and run interactively Interpreted programs are more portable � This is different from compiled programs since the executable code produced from a compiler for a Windows machine won’t run on a Mac without recompiling If a suitable interpreter already exists, the interpreted code can be run with no modifications.

Python and Mathematics All computer programs behave like a function in Mathematics • Input Data • Process Data • Output Results Like an Algebraic Function! Computer Science is a proper subset of Mathematics so it’s no surprise Let’s start by looking at the notion of a variable, how to perform some simple arithmetic calculations, and how to do simple input and output

Basics Print() went from being a statement to being a function Python already understands some keywords or commands � Also, some values like numbers, strings, etc. Functional Notation, just like in Algebra

Variables, Values, and the Assignment Operator Programming is very similar to Algebra � In Algebra, an assignment statement might consist of: Assignment Operator x Variable = 5 Value

Functions are also just like in Algebra A function has a name (identifier) and arguments or parameters: Assignment Operator y Variable = f(x) Function name: f Function argument/parameter: x

Variables A name assigned to a bit of information One of the most fundamental concepts in programming is that of a variable. A variable is “a name that is assigned to a value, ” as such: n = 5 variable n is assigned the value 5 Thus, whenever variable n appears in a calculation, it is the current value of n is that is used, as in: n + 20 (5 + 20) � If variable n is assigned a new value, then the same expression will produce a different result: n = 10 n + 20 (10 + 20)

Pythonic Calculators Python also includes basic arithmetic operators The common arithmetic operators in Python are, + (addition) - (subtraction) * (multiplication) ** (exponentiation) / (division) Addition, subtraction, and division use standard mathematical notation, 10 + 20 25 - 15 (Also, // for truncated division) 20 / 10 For multiplication and exponentiation, the asterisk (*) is used, 5 * 10 (5 times 10) 2 ** 4 (2 to the 4 th power) Multiplication is never denoted by the use of parentheses, 10 * (20 + 5) CORRECT 10(20 + 5) INCORRECT But parentheses can be used to denote subexpressions

Basic Input is usually provided by the user In Python, the input function is used for this purpose, name = input('What is your name? : ') Characters within quotes are called strings. This particular use of a string, for requesting input from the user, is called a prompt. The input() function displays the prompt string on the screen to prompt the user for input as below: What is your name? : Ricky The underline is used here to indicate the user’s input.

Basic Output We can also display information to the user’s screen The print function is used to display information on the screen in Python. This may be used to display a message (string) as shown below: >>> print('Welcome to My First Program!') Welcome to My First Program! or used to output the value of a variable: >>> n = 10 >>> print(n) 10

- ESSENTIAL COMPUTATIONAL THINKING CHAPTER 05 Dr Ricky J
- EKONOMI PERTANIAN Ricky Herdiyansyah Sp MSi Ricky Herdiyansyah
- ESG Competitive noncompetitive ranges By Ricky Choy ricky
- A LOOK INTO COMPUTATIONAL THINKING Computational Thinking Desktop
- What is Computational Thinking Computational Thinking Characteristics From