Python Programming Module 1 Computers and Programs Python Programming, 2/e1.

Slides:



Advertisements
Similar presentations
Lecture 1: Intro to Computers Yoni Fridman 6/28/01 6/28/01.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Introduction to Computer Administration Introduction.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Adapted from John Zelle’s Book Slides1 CS177 Python Programming Chapter 1 Computers and Programs.
Topics Introduction Hardware and Software How Computers Store Data
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Python Programming: An Introduction to Computer Science
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Introduction to Computers
Computers Parts/Types. Topics Definition Types of Computers Parts of Computer System Impact on Society.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Intro to Computers Computer Apps 1.
Python From the book “Think Python”
Intro to Computers Computer Applications. What is a Computer? Initially the term computer referred to an individual whose job it was to perform mathematical.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Parts of the Computer System
CS 127 Introduction to Computer Science. What is a computer?  “A machine that stores and manipulates information under the control of a changeable program”
Looking Inside the Computer System
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CSC 1010 Programming for All Lecture 1 Some material courtesy of Python for Informatics: Exploring Information (
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
COMPUTER HARDWARE & SOFTWARE INTRODUCTION TO LIBRARY & INFORMATION SCIENCES (5501) WORKSHOP SPRING 2013 By: Huma Malik Librarian, Preston University, Islamabad.
INTRODUCTION TO COMPUTERS. A computer system is an electronic device used to input data, process data, store data for later use and produce output in.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 1: Introduction to Computers and Programming.
MET4750 Techniques for Earth System Modeling MET 5990 Techniques for Earth System Modeling and Research (
And now for something completely different…
Chapter 1: Introduction to Computers and Programming
Computer Architecture and Number Systems
Fundamentals of Programming I Overview of Programming
BASIC PROGRAMMING C SCP1103 (02)
Development Environment
A Python Tour: Just a Brief Introduction
An Overview of the Computer System
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Visual Basic 2008 Programming
BASIC PROGRAMMING C SCP1103 (02)
McGraw-Hill Technology Education
Looking Inside the machine (Types of hardware, CPU, Memory)
Chapter 1: Introduction to Computers and Programming
An Overview of the Computer System
CS190/295 Programming in Python for Life Sciences: Lecture 1
Interaction of Hardware
Topics Introduction Hardware and Software How Computers Store Data
Logical Computer System
15-110: Principles of Computing
15-110: Principles of Computing
General Computer Science for Engineers CISC 106 Lecture 03
Hardware is… Software is…
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Python Programming Module 1 Computers and Programs Python Programming, 2/e1

The Universal Machine What is a computer? A modern computer can be defined as “a machine that stores and manipulates information under the control of a changeable program.” Two key elements: – Manipulating information – Changeable program Python Programming, 2/e2

The Universal Machine 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! Python Programming, 2/e3

The Universal Machine Programs are executed, or carried out. With suitable programming, all computers have the same power, i.e. each computer can do the things any other computer can do. – Some computers are just slower. – This statement only applies to “computation”, not to add-on devices like touch screens, printers, etc. Python Programming, 2/e4

Programming 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. Python Programming, 2/e5

Programming Power – Can be useful — you may need to solve a problem that doesn’t yet have a program. – Helps you become a more intelligent user of computers – It can be fun! – Form of expression – Helps the development of problem solving skills. – Programmers are in great demand! Python Programming, 2/e6

What is Computer Science? 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?” This question is really, “What can be computed?” Python Programming, 2/e7

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. Python Programming, 2/e8

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), solid state drive (SSD), SD Card, USB “thumb” drive, etc. Python Programming, 2/e9

Hardware Basics Input devices – Information is passed to the computer through keyboards, track pads, sensors, etc. Output devices – Processed information is presented to the user through the monitor, printer, etc. – Output can also be through the activation of devices like motors, speakers, etc. Python Programming, 2/e10

Programming Languages Natural language has ambiguity and this imprecision creates problems when used to describe complex algorithms. – Programs are 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. Python Programming, 2/e11

Programming Languages Programming languages are like a code for writing the instructions the computer will follow. – Programmers will often refer to their program as computer code. – The process of writing an algorithm in a programming language is often called coding. Python Programming, 2/e12

Why Python? Flexible and portable. Relatively easy to learn as a first language. Interactive mode allows quick testing of concepts. Commonly used in industry. Python Programming, 2/e13

Why Not Python? Relatively slow. Doesn’t have all the feature of more complex languages. Python Programming, 2/e14

IDLE: Integrated Development Environment Python comes with the IDLE Integrated Development Environment where we will write and run our code. Run the IDLE program that is part of the Python package. idle3 Python Programming, 2/e15

The Magic of Python When you start Idle, you will see something like: Python (default, Jun , 21:46:58) [GCC 4.6.3] on linux2 Type "copyright", "credits" or "license()" for more information. >>> Python Programming, 2/e16

The Magic of Python 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 >>> Python Programming, 2/e17

The Magic of Python 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(”Robots are Fun”) >>> Python Programming, 2/e18

The Magic of Python >>> def hello(): print("Hello”) print(”Robots 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. Python Programming, 2/e19

The Magic of Python >>> def hello(): print("Hello”) print(”Robots are Fun”) >>> Indenting shows the function’s beginning and end. 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 followed by a pair of parentheses. >>> hello() Hello Robots are Fun >>> Python Programming, 2/e20

The Magic of Python 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?”) >>> Python Programming, 2/e21

The Magic of Python >>> greet(”Ya") Hello Ya How are you? >>> greet(”Eric") Hello Eric How are you? >>> When we use parameters, we can customize the output of our function. Python Programming, 2/e22

The Magic of Python 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 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. Python Programming, 2/e23

The Magic of Python Download the file quad.py from the workshop Wikispaces site. Save the file and open it in IDLE. Python Programming, 2/e24

The Magic of Python from math import sqrt def main(): print("This program solves a quadratic equation") print(”in the form ax^2 + bx + c = 0.") a = float(input("Enter the coefficient a: ")) b = float(input("Enter the coefficeint b: ")) c = float(input("Enter the coefficient c: ")) disc = b**2 - 4 * a * c if disc < 0: print("There is no solution!") else: rt = sqrt(disc) x1 = (-b + rt)/(2 * a) x2 = (-b - rt)/(2 * a) print("The two solutions are: ", x1, x2) main() Python Programming, 2/e25

The Magic of Python Get the window with quad.py in the foreground. From the Run menu in IDLE select Run Module. >>> This program solves a quadratic equation in the form ax^2 + bx + c = 0. Enter the coefficient a: 1 Enter the coefficient b: 2 Enter the coefficient c: 0 The two solutions are: Python Programming, 2/e26

Inside a Python Program from math import sqrt This line import the square root function from the math library. Next we define a new function called main. The main() at the end tells Python to run the function. a, b, c, disc, and rt are examples of variables. Python Programming, 2/e27

Inside a Python Program a = float(input("Enter the coefficient a: ")) The input statements do four things: – The quoted information is displayed. – The number typed in response is read into the computer as a string of characters. – This value is converted to a floating point number (decimal number). – The result stored in the specified variable. – Notice we using composition of functions! Python Programming, 2/e28

Inside a Python Program disc = b**2 - 4 * a * c This line does a math computation and stores the result in a new variable. Note that * is multiplication and ** is exponentiation. The usual order of operations apply. Python Programming, 2/e29

Inside a Python Program if disc < 0: print("There is no solution!") This is an IF structure. disc < 0 is a condition and is either True or False. The print is the body of the statement and will be executed only if the condition is True. Indentation shows which statements are in the body. Python Programming, 2/e30

Inside a Python Program else: rt = sqrt(disc) x1 = (-b + rt)/(2 * a) x2 = (-b - rt)/(2 * a) print("The two solutions are: ", x1, x2) The body of the Else will only be executed when the If condition is false. Notice the beginning and ending of the the body are indicated by indentation. Python Programming, 2/e31