python:about_python:about_python_i
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | python:about_python:about_python_i [2013/07/05 06:13] (current) – created mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== About Python I ====== | ||
+ | ===== Python is interpreted ===== | ||
+ | |||
+ | ==== Interpretation ==== | ||
+ | |||
+ | In an interpreted system, program instructions are executed inside a program called an **interpreter**. The interpreter examines and executes a program' | ||
+ | |||
+ | Without the right interpreter installed on your system, you cannot run interpreted language programs. | ||
+ | |||
+ | ==== Compilation ==== | ||
+ | |||
+ | In a compiled system, the translation from source code into machine code is done //all at once// by a program called a **compiler**. Often times the translated code must be connected to other modules in system libraries and such. This connection to library modules is done by a program called a **linker**. The result of a successful compile-and-link process is an executable machine-code module that you can run directly as a standalone program. | ||
+ | |||
+ | If there are syntax errors in the source code, the compiler will fail to produce a machine-code translation of the program. This means that it will be impossible for you to link and then execute it. Almost all compilers will give you information about the nature of the error to help you debug the program. | ||
+ | |||
+ | Once the executable module is made, you do not need a compiler or linker to run the program. | ||
+ | |||
+ | ==== Exercise: Python in interactive mode ==== | ||
+ | |||
+ | The name of the command to start the Python interpreter is '' | ||
+ | |||
+ | - Open a shell (i.e., a " | ||
+ | - At the command prompt, type '' | ||
+ | - If Python was installed correctly on your system, you should see something that looks like this: \\ {{python-interactive.png|}} \\ The '' | ||
+ | - At the '' | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | - Enter: < | ||
+ | |||
+ | To exit the Python interpreter, | ||
+ | |||
+ | ===== Python is scriptable ===== | ||
+ | |||
+ | Using Python in interactive mode is great if all you want is a super-calculator.((It also turns out to be very useful for debugging and testing ideas.)) However, the true power of Python is realized when you use it to run Python // | ||
+ | |||
+ | An example of such a script appears below. | ||
+ | |||
+ | <code python> | ||
+ | # A sample Python script | ||
+ | print " | ||
+ | print "(2012 - 13) / 3 is", (2012 - 13) / 3 | ||
+ | print " | ||
+ | </ | ||
+ | |||
+ | If you provide an argument to the '' | ||
+ | |||
+ | ==== Exercise: Running a Python script ==== | ||
+ | |||
+ | - Create a text file called '' | ||
+ | - Open a shell (i.e., " | ||
+ | - Navigate to the directory where you saved '' | ||
+ | - At the command prompt, type '' | ||
+ | - Cower in fear. | ||
+ | |||
+ | ===== Python is typed ===== | ||
+ | |||
+ | Values in Python are typed. Common simple types include '' | ||
+ | |||
+ | ==== Exercise: Types (with literals) ==== | ||
+ | |||
+ | - Start Python in interactive mode. | ||
+ | - Enter the following commands and observe the results:< | ||
+ | type(66) | ||
+ | type(3.15189) | ||
+ | type(3 + 5j) | ||
+ | type(" | ||
+ | type(' | ||
+ | type(" | ||
+ | type(' | ||
+ | type(False) | ||
+ | type(True) | ||
+ | type(true) | ||
+ | </ | ||
+ | |||
+ | You should notice a few important things: | ||
+ | |||
+ | * You can wrap string literals in either '' | ||
+ | * Single characters are simply strings with one character in them. There is no Python equivalent to C and Java's '' | ||
+ | * Python' | ||
+ | * Python is case sensitive. | ||
+ | |||
+ | ==== Variables ==== | ||
+ | |||
+ | Variables are created in Python automatically when they are first used (i.e., when they are first assigned a value). Python' | ||
+ | |||
+ | Identifiers can begin with any lowercase letter, uppercase letter, or the underscore character and be followed with any number of uppercase letters, lowercase letters, digits, or underscore characters. Traditionally, | ||
+ | |||
+ | ==== Exercise: Variable creation and assignment ==== | ||
+ | |||
+ | Execute the following statements in an interactive session and observe the results. | ||
+ | |||
+ | <code python> | ||
+ | a = 66 | ||
+ | print a | ||
+ | type(a) | ||
+ | |||
+ | b = 1.5708 * 2.0 | ||
+ | print b | ||
+ | type(b) | ||
+ | |||
+ | c = 3 + 5j | ||
+ | print c | ||
+ | type(c) | ||
+ | |||
+ | foo = "Hello world" | ||
+ | print foo | ||
+ | type(foo) | ||
+ | |||
+ | goo = 'Hello world' | ||
+ | print goo | ||
+ | type(goo) | ||
+ | |||
+ | bar = " | ||
+ | print bar | ||
+ | type(bar) | ||
+ | |||
+ | baz = ' | ||
+ | print baz | ||
+ | type(baz) | ||
+ | |||
+ | is_it = False | ||
+ | print is_it | ||
+ | type(is_it) | ||
+ | |||
+ | to_be = True | ||
+ | print to_be | ||
+ | type(to_be) | ||
+ | </ | ||
+ | |||
+ | ===== Python' | ||
+ | |||
+ | The types of Python variables change as needed to permit the assignment. | ||
+ | |||
+ | ==== Exercise: Dynamic typing ==== | ||
+ | |||
+ | Execute the following statements in an interactive session and observe the results. | ||
+ | |||
+ | <code python> | ||
+ | x = 3 | ||
+ | type(x) | ||
+ | |||
+ | x = 3.0 | ||
+ | type(x) | ||
+ | |||
+ | x = " | ||
+ | type(x) | ||
+ | </ | ||
+ | |||
+ | Copyright © 2011 Mithat Konar. All rights reserved |
python/about_python/about_python_i.txt · Last modified: 2013/07/05 06:13 by mithat