Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python From the book “Think Python”

Similar presentations


Presentation on theme: "Python From the book “Think Python”"— Presentation transcript:

1 Python From the book “Think Python”
How to Think Like a Computer Scientist

2 Python Home Installation
We will be using Python for this class. There is a newer version Python but it is a little different in syntax. You can use it but you will be responsible for the syntax conversions. On your home machine go to Download the version you need, I picked Python Windows X86-64 Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source) Note there is a Mac version if you prefer. We may well add additional libraries ( such as Swampy ) as we go on. for other downloads check out

3 Computer Languages High Level Languages (Python, Java, C++…)
Easy to program in and maintain portable Low Level Languages Machine language ( binary , runs on machine) Assembly language ( Natural language version of Machine

4 Translation Interpreters
A program that reads a high level language and executes it, often one line at a time. Compilers Reads the entire program (source) and translates it to a machine program (binary) that is directly executable. Hybrid Java is translated to an intermediate code( byte code) and that code is interpreted.

5 Python Interactive mode ( for testing and learning)
Here you can type in a single instruction and have the Python interpreter execute it immediately. >>> <- input <- output Script mode (normally what you do) In this case you write a program and store it in a file name.py The file name.py is then executed by Python in its entirety

6 What is a program? A program is a sequence of instruction that specifies how to perform a computation. Most languages have instructions that perform Input ( reads data from the keyboard, file, … Output ( Displays data on screen or writes it to a file, …. Math ( performs mathematical operations) Conditional Execution ( based on certain conditions appropriate code sequences are executed. Repetition ( perform sections of code repeatedly.)

7 Errors Syntax errors Semantic errors Runtime errors Debugging
Instructions are written incorrectly prnt (5) # did not spell print correctly Semantic errors Instruction runs but the output is not what you thought would happen. These are the hardest errors to find and fix. Runtime errors Here an error occurs while the program is running ( after syntax checking) These include divide by 0 errors as well as many others IE something bad happens when running after translation. Debugging The different things you do to find and repair your errors. This is the detective work in software development.

8 Languages Natural languages (English, Spanish, French)
Spoken languages Ambiguous Full of idiom and metaphors Formal Languages Special purpose languages (Chemistry, Math, Programming) Strict rules and syntax Unambiguous Mean exactly what is said

9 First Program The first program many people run when learning a new language is often referred to as the Hello World! Program In Python it is a single liner print(‘Hello World!’) Running within the interactive mode we have >>> print(‘Hello World!’) Hello World! Mistype the above command in a variety of ways and note the interpreters comments.

10 Lets Do it (method 1) Run the Python (IDLE) Gui app and type in the print command. You should see something like Python (default, Nov , 19:24:24) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> print "Hello World!" Hello World! >>>

11 Script (Method 2) Here we create a file named hello.py and run it from the Python editor. Click File then New File and type-in print ‘Hello World!’ Save the file under name hello.py Position the edit window and the interactive window side by side Now in the edit window click Run and then run module You should see the output within the interactive window Add a new print instruction of your choosing and run again Do this several times.

12 Run Script from DOS (MEthOD 3)
The hello.py file can also be run from the DOS command line. Go to the directory that contains the file hello.py Click shift-right click. Then select option ‘open command window here’ You should see a DOS window pop up. Type DIR and check that hello.py is there Now type python hello.py at the DOS command prompt You should see the output Hello World!

13 Study the Glossary On page 7 there is a list of words that you need to know. Most of these we will use often so they will become second nature. These include: HLL,LLL, portability, interpret, compile, source, objectcode, Executable, prompt, script , interactive mode, script mode, program, algorithm, bug, debugging, syntax, syntax error, semantics, exception (runtime error), natural language, formal language, token, parse, print statement


Download ppt "Python From the book “Think Python”"

Similar presentations


Ads by Google