Python Programming: An Introduction to Computer Science



Similar documents
Python Programming: An Introduction to Computer Science

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Computers and Programs

Primary Memory. Input Units CPU (Central Processing Unit)

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Python Programming: An Introduction to Computer Science. John M. Zelle, Ph.D.

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Chapter 3: Computer Hardware Components: CPU, Memory, and I/O

Introduction to the course, Eclipse and Python

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

CHAPTER 2: HARDWARE BASICS: INSIDE THE BOX

Management Challenge. Managing Hardware Assets. Central Processing Unit. What is a Computer System?

Chapter 1. The largest computers, used mainly for research, are called a. microcomputers. b. maxicomputers. c. supercomputers. d. mainframe computers.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

3 SOFTWARE AND PROGRAMMING LANGUAGES

Chapter 2 Basic Structure of Computers. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

Parts of a Computer. Preparation. Objectives. Standards. Materials Micron Technology Foundation, Inc. All Rights Reserved

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

CHAPTER 1 ENGINEERING PROBLEM SOLVING. Copyright 2013 Pearson Education, Inc.

Introduction to Computers and Programming

Module 1 Introduction to Information and Communication Technologies

Obj: Sec 1.0, to describe the relationship between hardware and software HW: Read p.2 9. Do Now: Name 3 parts of the computer.

Lesson 06: Basics of Software Development (W02D2

Computer Basics: Chapters 1 & 2

Computer Organization

Chapter 3. Operating Systems

Tech Application Chapter 3 STUDY GUIDE

Chapter 13: Program Development and Programming Languages

1 PERSONAL COMPUTERS

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

lesson 1 An Overview of the Computer System

Chap-02, Hardware and Software. Hardware Model

CSCA0102 IT & Business Applications. Foundation in Business Information Technology School of Engineering & Computing Sciences FTMS College Global

CS 40 Computing for the Web

Computer Literacy. Hardware & Software Classification

Writing Assignment #2 due Today (5:00pm) - Post on your CSC101 webpage - Ask if you have questions! Lab #2 Today. Quiz #1 Tomorrow (Lectures 1-7)

Computer Performance. Topic 3. Contents. Prerequisite knowledge Before studying this topic you should be able to:

Computer Systems Structure Main Memory Organization

CONTROL DATA" 3200 Computer system / ~eal Time Applications

Chapter 1 Fundamentals of Java Programming

Writing Simple Programs

COMPUTER SCIENCE AND ENGINEERING - Microprocessor Systems - Mitchell Aaron Thornton

Practical Programming, 2nd Edition

Computer Layers. Hardware BOOT. Operating System. Applications

Levels of Programming Languages. Gerald Penn CSC 324

Chapter 12 Programming Concepts and Languages

Today s Topics... Intro to Computer Science (cont.) Python exercise. Reading Assignment. Ch 1: 1.5 (through 1.5.2) Lecture Notes CPSC 121 (Fall 2011)

A Computer Glossary. For the New York Farm Viability Institute Computer Training Courses

A Java Crib Sheet. First: Find the Command Line

Introduction. What is an Operating System?

CS 51 Intro to CS. Art Lee. September 2, 2014

1.1 Electronic Computers Then and Now

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Introduction to Python

Introduction to Python

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

Chapter 8 Memory Units

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

New Mexico Broadband Program. Basic Computer Skills. Module 1 Types of Personal Computers Computer Hardware and Software

THREE YEAR DEGREE (HONS.) COURSE BACHELOR OF COMPUTER APPLICATION (BCA) First Year Paper I Computer Fundamentals

Unit A451: Computer systems and programming. Section 2: Computing Hardware 1/5: Central Processing Unit

What is a programming language?

Basics of Computer 1.1 INTRODUCTION 1.2 OBJECTIVES

Chapter 8: Installing Linux The Complete Guide To Linux System Administration Modified by M. L. Malone, 11/05

Course MS10975A Introduction to Programming. Length: 5 Days

COMPUTER BASICS. Seema Sirpal Delhi University Computer Centre

Overview of MIS Professor Merrill Warkentin

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

PYTHON Basics

I/O. Input/Output. Types of devices. Interface. Computer hardware

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

CHAPTER 7: The CPU and Memory

Chapter 6. Inside the System Unit. What You Will Learn... Computers Are Your Future. What You Will Learn... Describing Hardware Performance

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Computer Programming I & II*

Introduction to programming

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

An Introduction to MPLAB Integrated Development Environment

Chapter 3 Operating-System Structures

Introduction to Information System Layers and Hardware. Introduction to Information System Components Chapter 1 Part 1 of 4 CA M S Mehta, FCA

Software: Systems and Application Software

From Control Loops to Software

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

Computer Hardware HARDWARE. Computer Hardware. Mainboard (Motherboard) Instructor Özgür ZEYDAN

================================================================

High level code and machine code

OPERATING SYSTEM SERVICES

The Central Processing Unit:

Chapter 2: Computer-System Structures. Computer System Operation Storage Structure Storage Hierarchy Hardware Protection General System Architecture

COMPUTER HARDWARE. Input- Output and Communication Memory Systems

#820 Computer Programming 1A

Fall Lecture 1. Operating Systems: Configuration & Use CIS345. Introduction to Operating Systems. Mostafa Z. Ali. mzali@just.edu.

Transcription:

Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs 1

The Universal Machine n A computer -- a machine that stores and manipulates information under the control of a changeable program. n Unlike a (simple) calculator or car computer, which do not involve changeable programs. 2

Computer Program n Algorithm = a detailed, step-by-step set of instructions telling a computer what to do. n Program = implementation of an algorithm = algorithm written in a particular language n Programs are executed. 3

Computer Power n Any computer (with suitable programming), can computer the same things any other computer can can have the same input-output behaviour n They may differ n in efficiency. n in how much information they store. n in peripheral devices, network access 4

Programming n Software = programs. n Hardware = physical machine. n Software rules the hardware. n Creating software -- programming. 5

What is Computer Science? n It is not the study of computers! Computers are to computer science what telescopes are to astronomy E. Dijkstra n It is the study of algorithms: Design, implementation and analysis of algorithms. n Programming is an essential part. 6

What is Computer Science? n Computer science does not belong to natural science unlike physics, chemistry, biology it does not use the scientific method. n It is in part mathematics, in part engineering: n mathematical theory of algorithms n software and hardware engineering 7

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

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

Hardware I/O devices n Input devices n Information is passed to the computer through keyboards, mice, scanners, etc. n Output devices n Processed information is presented to the user through the monitor, printer, etc. 10

Executing programs n Fetch-Execute Cycle n First instruction retrieved from memory n Decode the instruction to see what it represents n Appropriate action carried out. n Next instruction fetched, decoded, and executed. n Lather, rinse, repeat! 11

Syntax vs. semantics n Natural language has ambiguity and imprecision problems when used to describe complex algorithms. n Programs expressed in an unambiguous, precise programming languages. n Every structure in programming language has a precise form, called its syntax n Every structure in programming language has a precise meaning, called its semantics. 12

Jargon n Programmers will often refer to their program as computer code. n Process of writing an algorithm in a programming language often called coding. 13

Language Level n High-level computer languages n Designed to be used and understood by humans n Low-level language n Computer hardware can only understand a very low level language known as machine language 14

Low Level Language n Add two numbers: n Load the number from memory location 2001 into the CPU n Load the number from memory location 2002 into the CPU n Add the two numbers in the CPU n Store the result into location 2003 n In machine language, these instructions are represented in binary (1 s and 0 s) 15

High Level Language n c = a + b n This needs to be translated into machine language that the computer can execute. n Compilers convert programs written in a high-level language into the machine language of some computer. 16

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

Compiling vs. Interpreting n 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. n Compiled programs generally run faster since the translation of the source code happens only once. 18

Interpreter advantages n Interpreted languages are part of a more flexible programming environment since they can be developed and run interactively n Interpreted programs are more portable, meaning the executable code produced from a compiler for a Pentium won t run on a Mac, without recompiling. If a suitable interpreter already exists, the interpreted code can be run with no modifications. 19

Python We will use Python 3 (Python 3.x) It is not compatible with Python 2.x Use Integrated Development Environment (IDE) idle3. 20

Python n The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements. n >>> print("hello, world") Hello, world >>> print(2+3) 5 >>> print("2+3=", 2+3) 2+3= 5 >>> 21

Functions n Usually we want to execute several statements together that solve a common problem. One way to do this is to use a function. n >>> def hello(): print("hello") print("computers are Fun") >>> 22

Function definition n >>> def hello(): print("hello") print("computers are Fun") >>> n The first line tells Python we are defining a new function called hello. n The following lines are indented to show that they are part of the hello function. n The blank line (hit enter twice) lets Python know the definition is finished. 23

Function call n >>> def hello(): print("hello") print("computers are Fun") >>> n Notice that nothing has happened yet! We ve defined the function, but we haven t told Python to perform the function! n A function is invoked by typing its name. n >>> hello() Hello Computers are Fun >>> 24

Parameter lists n What s the deal with the () s? n Commands can have changeable parts called parameters that are placed between the () s. n >>> def greet(person): print("hello", person) print ("How are you?") >>> 25

Calling functions n >>> greet("terry") Hello Terry How are you? >>> greet("paula") Hello Paula How are you? >>> n When we use parameters, we can customize the behaviour of our function. 26

Module files n n n n When we exit idle3, the functions we have defined cease to exist! Programs are usually composed of functions, in modules 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. An integrated development environment IDE, such as idle3, is contains an editor (with automatic indenting, highlighting keywords) and a windoe to run the program (with I/O) 27

Program chaos.py # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("this program illustrates a chaotic function") x = eval(input("enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main() 28

n We ll use filename.py when we save our work to indicate it s a Python program. n In this code we re defining a new function called main. n The call main() tells Python to run the code. 29

Output This program illustrates a chaotic function Enter a number between 0 and 1:.5 0.975 0.0950625 0.335499922266 0.869464925259 0.442633109113 0.962165255337 0.141972779362 0.4750843862 0.972578927537 0.104009713267 >>> 30

Comments # File: chaos.py # A simple program illustrating chaotic behavior n Lines that start with # are called comments n Intended for human readers and ignored by Python n Python skips text from # to end of line 31

Inside a Python Program def main(): n Beginning of the definition of a function called main n Since our program has only this one module, it could have been written without the main function. n The use of main is customary, however. 32

Print print("this program illustrates a chaotic function") n Print a message introducing the program. 33

Input x = eval(input("enter a number between 0 and 1: ")) n x is a variable n A variable is used to give a name to a value, so that we can refer to it later. n The quoted information is displayed, and the number typed in response is stored in x. 34

For-Loop for i in range(10): n For is a loop construct n A loop tells Python to repeat the same thing over and over. n In this example, the following code will be repeated 10 times. 35

x = 3.9 * x * (1 - x) print(x) n These lines are the body of the loop. n They repeated each time through the loop. n The body of the loop is identified by indentation. n The effect of the loop is the same as repeating this two lines 10 times! 36

for i in range(10): x = 3.9 * x * (1 - x) print(x) n These are equivalent! x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 x) print(x) x = 3.9 * x * (1 - x) print(x) 37

Assignment statement x = 3.9 * x * (1 - x) n assignment statement n The part on the right-hand side (RHS) of the = is an expression. n * stands for multiplication n Once the value on the RHS is computed, it is stored back into (assigned) into x 38

Inside a Python Program main() n a function call - tells Python to execute the code in the function main 39

Chaos and Computers n The chaos.py program: def main(): main() print("this program illustrates a chaotic function") x = eval(input("enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) n For any given input, returns 10 seemingly random numbers between 0 and 1 n It appears that the value of x is chaotic 40

Chaos and Computers n The function computed by program has the general form kx ( )(1 x) where k is 3.9 n This type of function is known as a logistic function. n It models certain kinds of unstable electronic circuits. n Very small differences in initial value can have large differences in the output. 41

Chaos and Computers n Input: 0.25 0.73125 0.76644140625 0.698135010439 0.82189581879 0.570894019197 0.955398748364 0.166186721954 0.540417912062 0.9686289303 0.118509010176 n Input: 0.26 0.75036 0.73054749456 0.767706625733 0.6954993339 0.825942040734 0.560670965721 0.960644232282 0.147446875935 0.490254549376 0.974629602149 42