Download presentation
Presentation is loading. Please wait.
Published byLionel Bradley Modified over 8 years ago
1
Python Programming Module 1 Computers and Programs Python Programming, 2/e1
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
Why Not Python? Relatively slow. Doesn’t have all the feature of more complex languages. Python Programming, 2/e14
15
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. (precise)henry@localhost:~S idle3 Python Programming, 2/e15
16
The Magic of Python When you start Idle, you will see something like: Python 3.2.3 (default, Jun 23 2015, 21:46:58) [GCC 4.6.3] on linux2 Type "copyright", "credits" or "license()" for more information. >>> Python Programming, 2/e16
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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: 0.0 -2.0 Python Programming, 2/e26
27
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
28
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
29
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
30
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
31
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.