Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)

Similar presentations


Presentation on theme: "1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)"— Presentation transcript:

1 1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)

2 Objectives To understand the basic design of a modern computer. To understand the form and function of computer programming languages. To begin using the Python programming language.

3 Hardware and software: which is more important? Source: http://www.cloudproviderusa.com/whats-more- important-hardware-software/abr0137l/

4 0 and 1 conquer the world! Bit: binary digit Byte Binary number system Binary arithmetic (Gottfried Wilhelm von LEIBNIZ (1646-1716))

5 Why only two values? Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

6 The binary number system Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

7 Raspberry Pi

8 Hardware basics

9 Hardware Basics The central processing unit (CPU) is the “brain” of a computer. The CPU carries out all the basic operations on the data. Examples: simple arithmetic operations, testing to see if two numbers are equal.

10 Hardware Basics Memory stores programs and data. CPU can only directly access information stored in main memory (RAM or Random Access Memory). Main memory is fast, but volatile, i.e. when the power is interrupted, the contents of memory are lost. Secondary memory provides more permanent storage: magnetic (hard drive, floppy), optical (CD, DVD)

11 The memory hierarchy Source: http://www.bit-tech.net/hardware/memory/2007/11/15/the_secrets_of_pc_memory_part_1/3

12 Hardware Basics Input devices Information is passed to the computer through keyboards, mouse, etc. Output devices Processed information is presented to the user through the monitor, printer, etc.

13 Hardware Basics 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.

14 Programming Languages Natural language has ambiguity and imprecision problems. Programs expressed in an unambiguous, precise way using programming languages. Every structure in programming language has a precise form, called its syntax Every structure in programming language has a precise meaning, called its semantics.

15 Programming Languages Programming language like a 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 often called coding.

16 Programming Languages 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

17 Source: https://sherifelmahdi.wordpress.com/2012/02/

18 An example Source: https://sherifelmahdi.wordpress.com/2012/02/

19 Compiling vs interpreting Compiling the source code to machine code Source: http://www.aboutdebian.com/compile.htm

20 Compiling vs interpreting Source: http://www.circuitstoday.com/compilers-vs-interpreters- an-overview-of-the-differences

21 Compiling vs. Interpreting Once program is compiled, it can be executed over and over without the source code or compiler. If it is interpreted, the source code and interpreter are needed each time the program runs. Compiled programs generally run faster since the translation of the source code happens only once.

22 For Python Source: http://trizpug.org/Members/cbc/wyntkap/img/interpreter.png

23 Let’s start using Python!

24 EXERCISE 1.1 Open your Python IDLE Type print(“Hello, World!”). Type print(2+3). Type print(“2+3”, 2+3). Type print("2 + 3", "3+2"). Type print.

25 print() is a built-in function. IDLE is a program development environment for Python. The interactive environment is called a (command) shell. A complete command is a statement. A function takes in one or more parameters (or arguments) which could be Character strings (i.e., text) Number Arithmetic expression

26 EXERCISE 1.2 Type >>> def hello(): print("Hello") print(“CS is fun!") After entering a new line and get the prompt, type hello().

27 Defining a new function hello() def is a Python keyword for defining a new function. The first line tells Python we are defining a new function called hello. The following lines are indented to show that they are part of the hello function. The blank line (hit enter twice) lets Python know the definition is finished. Type the function name (including the parentheses) to invoke (or call) the function.

28 EXERCISE 1.3 Modify the previous hello() to hello(name) so that when hello(“Rocky”) is called, the output will be Hello, Rocky, CS is fun! (Note that all the outputs will be on the same line.)

29 Saving the program in a.py file. Programs are usually composed of functions, modules, or scripts that are saved on disk so that they can be used again and again. A module file is a text file created in text editing software (saved as “plain text”) that contains function definitions. We’ll use filename.py when we save our work to indicate it’s a Python program.

30 EXERCISE 1.4 Get a Python program called chaos.py from the course website and save it in your machine and run it with different input values to observe the “chaotic behavior.” Ref: Watch the double rod pendulum animation in http://en.wikipedia.org/wiki/Chaos_theory

31 The chaos program Lines that start with # are called comments. Python skips text from # to end of line. Using main() for the function. x is an example of a variable. A variable is used to assign a name to a value so that we can refer to it later. The quoted information is displayed, and the number typed in response is stored in x. for (another Python keyword) is a loop construct A loop tells Python to repeat the same thing over and over. In this example, the code is repeated 10 times.

32 The math stuff x = 3.9 * x * (1 - x) is called an assignment statement. The part on the right-hand side (RHS) of the “=“ is a mathematical expression. * is used to indicate multiplication. Once the value on the RHS is computed, it is stored back into (assigned) into x.

33 END


Download ppt "1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)"

Similar presentations


Ads by Google