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



Similar documents
Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science

Computers and Programs

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.

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

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

Introduction to Computers and Programming

CS 40 Computing for the Web

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

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

3 SOFTWARE AND PROGRAMMING LANGUAGES

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

Primary Memory. Input Units CPU (Central Processing Unit)

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

Introduction to the course, Eclipse and Python

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

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

Introduction to Python

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

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

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

Module 1 Introduction to Information and Communication Technologies

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

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

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)

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

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.

CHAPTER 2: HARDWARE BASICS: INSIDE THE BOX

LOCKSS on LINUX. Installation Manual and the OpenBSD Transition 02/17/2011

Programming in Access VBA

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

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Writing Simple Programs

Tech Application Chapter 3 STUDY GUIDE

Computer Layers. Hardware BOOT. Operating System. Applications

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

1 PERSONAL COMPUTERS

LOCKSS on LINUX. CentOS6 Installation Manual 08/22/2013

Introduction. What is an Operating System?

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.

Computer Literacy. Hardware & Software Classification

Welcome to Introduction to Computers and Programming Course using Python

Overview and History of Operating Systems

Chapter 1 Fundamentals of Java Programming

Chapter 3. Operating Systems

Chapter 13: Program Development and Programming Languages

Chapter 12 Programming Concepts and Languages

COMPUTER BASICS. Seema Sirpal Delhi University Computer Centre

Figure 1: Graphical example of a mergesort 1.

Computer Basics: Chapters 1 & 2

#820 Computer Programming 1A

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

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

lesson 1 An Overview of the Computer System

COMPUTER SCIENCE AND ENGINEERING - Microprocessor Systems - Mitchell Aaron Thornton

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

Windows 7 for beginners

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

Hypercosm. Studio.

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

Course MS10975A Introduction to Programming. Length: 5 Days

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

Fall 2012 Q530. Programming for Cognitive Science

Chap-02, Hardware and Software. Hardware Model

Software: Systems and Application Software

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

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

An Introduction to MPLAB Integrated Development Environment

Computer Science 217

Lesson 06: Basics of Software Development (W02D2

Computer Systems Structure Main Memory Organization

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

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

CAs and Turing Machines. The Basis for Universal Computation

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Repetition Using the End of File Condition

Test instructions & HW/SW specifications Contents

Introduction to Python

ZIMBABWE SCHOOL EXAMINATIONS COUNCIL. COMPUTER STUDIES 7014/01 PAPER 1 Multiple Choice SPECIMEN PAPER

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Memory Management Simulation Interactive Lab

2. Advance Certificate Course in Information Technology

OPERATING SYSTEM SERVICES

Basics of Computer 1.1 INTRODUCTION 1.2 OBJECTIVES

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

CONTROL DATA" 3200 Computer system / ~eal Time Applications

High level code and machine code

A Java Crib Sheet. First: Find the Command Line

Python Loops and String Manipulation

Chapter 3: Operating-System Structures. Common System Components

Goal: Practice writing pseudocode and understand how pseudocode translates to real code.

Week 2 Practical Objects and Turtles

Using Remote Desktop to access your Office Computer or Faculty Remote Desktop Server August, 2005 This document consists of two main parts and an

Practical Programming, 2nd Edition

Computer Organization

CS3600 SYSTEMS AND NETWORKS

Transcription:

Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs Objectives Introduction to the class Why we program and what that means Introduction to the Python programming language Modified by Dan Fleck Coming up: What we ll learn in this class 2 What we ll learn in this class How to solve problems using computers How computer programs work How to write computer programs in Python How to debug (fix problems with) computer programs How to test your programs Lab 01 Logon to Blackboard: courses.gmu.edu Go to CS112 Click on assignments View the assignment and complete it Also, activate your GMU Email! You can easily forward it to Yahoo or Gmail. Make sure you check the account daily! https://mail.gmu.edu Coming up: Lab 01 3 Coming up: The Universal Machine 4 1

The Universal Machine A modern computer can be defined as a machine that stores and manipulates information under the control of a changeable program. Two key elements: Computers are devices for manipulating information. Computers operate under the control of a changeable program. 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! The program is executed or carried out by the machine Coming up: What is a computer program 5 Coming up: Exercise 6 Exercise Write the step-by-step instructions for creating a peanut butter and jelly sandwich Exercise Write the step-by-step instructions for creating a peanut butter and jelly sandwich Did you write: Spread peanut-butter on bread Spread jelly on bread Put pieces of bread together Coming up: Exercise 7 Coming up: Exercise 8 2

Exercise A computer cannot understand anything more than you tell it. Computer programming is the art of knowing what you want to do, and being able to specify it in enough detail that the computer understands. Get the jelly from the refrigerator Get the peanut-butter from the cupboard Open the jelly and the peanut-butter Open the knife-drawer and retrieve the knife You will spend half of the semester trying to break the problem into small enough steps for the computer to understand Coming up: Exercise 9 Exercise Why can t we just say make me a sandwich? English is MUCH to ambiguous for a computer to understand. Did you mean, create a sandwich or turn the user into a sandwich? How does the computer know the difference between that and make me a millionaire? You must use a computer language which is a very structured and specific language. We ll use Python. You will spend the other half of the semester trying to find the right words (syntax) to get the computer to understand you. Coming up: Program Power 10 Program Power Software (programs) rule the hardware (the physical machine). The process of creating this software is called programming. Why learn to program? Fundamental part of computer science Having an understanding of programming helps you have an understanding of the strengths and limitations of computers. Program Power Helps you become a more intelligent user of computers It can be fun! Form of expression Helps the development of problem solving skills, especially in analyzing complex systems by reducing them to interactions between simpler systems. Programmers are in great demand! Coming up: Program Power 11 Coming up: What is Computer Science? 12 3

What is Computer Science? It is not the study of computers! Computers are to computer science what telescopes are to astronomy. E. Dijkstra The question becomes What processes can be described [by a computer program]? Or more simply What can be computed? Answering: What can be computed? Design One way to show a particular problem can be solved is to actually design a solution. This is done by developing an algorithm, a step-by-step process for achieving the desired result. One problem it can only answer in the positive. You can t prove a negative! Coming up: Answering: What can be computed? 13 Coming up: Answering: What can be computed? 14 Answering: What can be computed? Analysis Analysis is the process of examining algorithms and problems mathematically. Some seemingly simple problems are not solvable by any algorithm. These problems are said to be unsolvable. (See 13.4.2 for example) Problems can be intractable if they would take too long or take too much memory to be of practical value. Example: chess Answering: What can be computed? Experimentation Some problems are too complex for analysis. Implement a system and then study its behaviour. Coming up: Answering: What can be computed? 15 Coming up: But what will I do? 16 4

But what will I do? Generally, as a Computer Science graduate you will Write computer programs Drink soda and eat pizza a lot Solve problems! Work with user/clients to design programs Hardware Basics : CPU 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. Have fun! Get paid well! Lead other computer scientists Test computer programs Coming up: Hardware Basics : CPU 17 Coming up: Hardware Basics : Memory 18 Hardware Basics : Memory 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) Hardware Basics: I/O Input devices Information is passed to the computer through keyboards, mice, etc. Output devices Processed information is presented to the user through the monitor, printer, etc. Coming up: Hardware Basics: I/O 19 Coming up: Programming Languages 20 5

Programming Languages High-level computer languages Designed to be used and understood by humans (C, Ada, Python, Java,.Net, etc ) Low-level language Computer hardware can only understand a very low level language known as machine language (binary, assembly which directly converts to binary) Programming Languages Low level version of 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 In reality, these low-level instructions are represented in binary (1 s and 0 s) Coming up: Programming Languages 21 Coming up: Conversion from high level to low level 22 Conversion from high level to low level High-level language c = a + b This needs to be translated into machine language that the computer can execute. Compilers and Interpreters convert programs written in a high-level language into the machine language of some computer. Compilers and Interpreters Interpreted Language Compiled Language Coming up: Compilers and Interpreters 23 Coming up: Interpreters 24 6

Interpreters 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 translates the source code instruction by instruction. Python has both options, but we ll use it in an interpreted way in CS112! Coming up: The Magic of Python 25 The Magic of Python IDLE is a programming environment for Python. Starting it you will see something like: Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.0.2 Coming up: Python Prompt 26 Python Prompt The is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements. print "Hello, world" Hello, world print 2+3 5 print "2+3=", 2+3 2+3= 5 Defining a Python Function Usually we want to execute several statements together that solve a common problem. One way to do this is to use a function. def hello(): print "Hello" print "Computers are Fun" Coming up: Defining a Python Function 27 Coming up: Defining a Python Function 28 7

Defining a Python Function def hello(): print "Hello" print "Computers are Fun" 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. Invoking a Function def hello(): print "Hello" print "Computers are Fun" Notice that nothing has happened yet! We ve defined the function, but we haven t told Python to perform the function! A function is invoked by typing its name. hello() Hello Computers are Fun Coming up: Invoking a Function 29 Coming up: Parameters 30 Parameters What s the deal with the () s? Commands can have changeable parts called parameters that are placed between the () s. def greet(person): print "Hello",person print "How are you? Parameters Example greet("terry") Hello Terry How are you? greet("paula") Hello Paula How are you? When we use parameters, we can customize the output of our function. Coming up: Parameters Example 31 Coming up: Python Notes 32 8

Python Notes When we exit the Python prompt, the functions we ve defined cease to exist! 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. A programming environment is designed to help programmers write programs and usually includes automatic indenting, highlighting, etc. Coming up: Complete Python Program 33 Complete Python Program # File: chaos.py # A simple program illustrating chaotic behavior def main(): print "This program illustrates a chaotic function" x = input("enter a number between 0 and 1: ") for i in range(10): main() We ll use filename.py when we save our work to indicate it s a Python program. In this code we re defining a new function called main. The main() at the end tells Python to run the code. Coming up: Chaos output 34 Chaos 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 Comments # File: chaos.py # A simple program illustrating chaotic behavior Lines that start with # are called comments Intended for human readers and ignored by Python Python skips text from # to end of line Coming up: Comments 35 Coming up: Inside a Python Program 36 9

Inside a Python Program def main(): Python Print print "This program illustrates a chaotic function" Beginning of the definition of a function called main Since our program has only this one module, it could have been written without the main function. The use of main is customary, however. This line causes Python to print a message introducing the program. Coming up: Python Print 37 Coming up: Python Variable 38 Python Variable x = input("enter a number between 0 and 1: ") 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 whatever the user types in response is stored in x. Python for loop for i in range(10): For is a loop construct A loop tells Python to repeat the same thing over and over. In this example, the following code will be repeated 10 times. Coming up: Python for loop 39 Coming up: Python Loop (cont.) 40 10

Python Loop (cont.) Python loop (cont.) These lines are the body of the loop. The body of the loop is what gets repeated each time through the loop. The body of the loop is identified through indentation. The effect of the loop is the same as repeating this two lines 10 times! for i in range(10): These are equivalent! Coming up: Python loop (cont.) 41 Coming up: Python Assignment 42 Python Assignment This 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 main() Python Main This last line tells Python to execute the code in the function main Coming up: Python Main 43 Coming up: Any questions? 44 11

Any questions? References http://openbookproject.net//thinkcspy/c hap01.html Coming up: References 45 Coming up: References 46 12