160 likes | 315 Views
Python. Programming Languages. Computers cannot understand English. Human languages are too expressive – there are too many ways to say something. At least for giving complex sequences of instructions (programs). . Programming Languages.
E N D
Programming Languages • Computers cannot understand English. • Human languages are too expressive – there are too many ways to say something. • At least for giving complex sequences of instructions (programs).
Programming Languages • The set of instructions that the computer can understand is very small. • On most modern processors about 100. • The types of instructions are very simple: • Move a number from one place to another • Perform arithmetic on some numbers • Test to see whether a value is equal to some other value • Humans can program at this level, but don’t like to.
Programming Languages • Programming languages provide a bridge between human languages and what the computer can understand. • Programming languages are less expressive than human languages, so the computer can translate to it’s own language. • We have to do some translation ourselves, from human language to the programming language.
Interpretation vs. Compilation • This translation from a programming language to machine language can happen in a couple of different ways.
Compilation • One way to do this translation is by compilation. • A program, called a compiler, translates the whole program to machine language and saves the translated version. • When we run the program, we’re running the saved machine language version. • Think of translating a book to another language.
Interpretation • Another way to do this is by interpretation. • A program, called an interpreter, translates one command to machine language and the computer does it. • Then the interpreter does it again with the next command, and so on. • No translated copy is produced. • Think of simultaneous translation (like at the U.N.)
Interpretation vs. Compilation • Interpreted programs are slower. • Although compilation takes a while, it doesn’t happen every time you want to run your program.
Compiling http://xkcd.com/303/
Interpretation vs. Compilation • Interpretation is more flexible – you can experiment more easily. • Interpretation allows for interactive programming – typing one command at a time and seeing the result.
IDLE Environment • IDLE is an interactive development environment for the Python interpreter. • Lets you try out individual commands. • Also lets you write full programs.
Interaction in the Interpreter • Whenever you type something at the prompt, you get back a response: >>> 5 5 • What you got back was the value of what you typed. • The interpreter evaluated what you typed.
Evaluation • Numbers have pretty obvious value. • Expressions have a pretty obvious value as well. >>> 5+2 7
Strings • Sometimes we want to work with text. • Strings are used to specify text. >>> ‘Hello’ ‘Hello’ • A string has a value too.
String Delimiters • We need to tell the interpreter where strings begin and end. • Otherwise it can tell the difference between text and commands. • And variables – we’ll get to those later. • Strings can be delimited with either single or double quotes. • ‘Hello’ • “Hello”
String Delimiters • You can use single and double quotes together because sometimes we want to put quotes inside a string. • ‘He said “Hello” to her’ • Just be sure you match your quotes or you will get errors.