programming languages
Download
Report
Transcript programming languages
1. COMPUTERS AND
PROGRAMS
Rocky K. C. Chang
September 12, 2016
(Based on Zelle and Dierbach)
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 and
IDLE.
Hardware and software: which is more
important?
Source: http://www.cloudproviderusa.com/whats-moreimportant-hardware-software/abr0137l/
0 and 1 conquer the
world!
Bit: binary digit
Byte
Binary number system
Binary arithmetic (Gottfried Wilhelm von
LEIBNIZ (1646-1716))
Why only two values?
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
The binary number system
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
Raspberry Pi
Hardware basics
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.
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)
The memory hierarchy
Source: http://www.bit-tech.net/hardware/memory/2007/11/15/the_secrets_of_pc_memory_part_1/3
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.
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.
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.
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.
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
Source: https://sherifelmahdi.wordpress.com/2012/02/
An example
Source: https://sherifelmahdi.wordpress.com/2012/02/
Compiling vs interpreting
• Compiling the source code to machine code
Source: http://www.aboutdebian.com/compile.htm
Compiling vs interpreting
Source: http://www.circuitstoday.com/compilers-vs-interpretersan-overview-of-the-differences
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.
For Python
Source: http://trizpug.org/Members/cbc/wyntkap/img/interpreter.png
Let’s start using Python!
EXERCISE 1.1
Open your Python IDLE
Type 2
Type 2+3
Type 2*3
Type 2**3
Type "Rocky"
Type Rocky
EXERCISE 1.2
Open your Python IDLE
Type name = input("What is your name? ")
Type print(name)
Type print("Hello, World!").
Type print(2+3).
Type print.
Type print().
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
EXERCISE 1.3
Type
>>> def hello():
print("Hello")
print("CS is fun!")
After entering a new line and get the prompt, type
hello().
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.
EXERCISE 1.4
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.)
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.
EXERCISE 1.5
Find a simple Python program from the
Internet and save it in your machine and run it
on IDLE.
END