-
Notifications
You must be signed in to change notification settings - Fork 1
/
Python.html
27 lines (27 loc) · 19.7 KB
/
Python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!--This file was converted to xhtml by LibreOffice - see http://cgit.freedesktop.org/libreoffice/core/tree/filter/source/xslt for the code.--><head profile="http://dublincore.org/documents/dcmi-terms/"><meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/><title xml:lang="en-US">- no title specified</title><meta name="DCTERMS.title" content="" xml:lang="en-US"/><meta name="DCTERMS.language" content="en-US" scheme="DCTERMS.RFC4646"/><meta name="DCTERMS.source" content="http://xml.openoffice.org/odf2xhtml"/><meta name="DCTERMS.creator" content="Balint Seeber"/><meta name="DCTERMS.issued" content="2014-03-11T00:57:11" scheme="DCTERMS.W3CDTF"/><meta name="DCTERMS.contributor" content="Balint Seeber"/><meta name="DCTERMS.modified" content="2014-04-18T10:27:23.364839303" scheme="DCTERMS.W3CDTF"/><meta name="DCTERMS.provenance" content="" xml:lang="en-US"/><meta name="DCTERMS.subject" content="," xml:lang="en-US"/><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" hreflang="en"/><link rel="schema.DCTERMS" href="http://purl.org/dc/terms/" hreflang="en"/><link rel="schema.DCTYPE" href="http://purl.org/dc/dcmitype/" hreflang="en"/><link rel="schema.DCAM" href="http://purl.org/dc/dcam/" hreflang="en"/><style type="text/css">
@page { }
table { border-collapse:collapse; border-spacing:0; empty-cells:show }
td, th { vertical-align:top; font-size:12pt;}
h1, h2, h3, h4, h5, h6 { clear:both }
ol, ul { margin:0; padding:0;}
li { list-style: none; margin:0; padding:0;}
<!-- "li span.odfLiEnd" - IE 7 issue-->
li span. { clear: both; line-height:0; width:0; height:0; margin:0; padding:0; }
span.footnodeNumber { padding-right:1em; }
span.annotation_style_by_filter { font-size:95%; font-family:Arial; background-color:#fff000; margin:0; border:0; padding:0; }
* { margin:0;}
.P1 { font-size:12pt; font-family:Courier New; writing-mode:lr-tb; }
.P2 { font-size:12pt; font-family:Arial; writing-mode:lr-tb; }
.P3 { font-size:40pt; font-family:Arial; writing-mode:lr-tb; text-align:center ! important; }
.P4 { font-size:24pt; font-family:Arial; writing-mode:lr-tb; text-align:center ! important; }
.P5 { font-size:12pt; font-family:Arial; writing-mode:lr-tb; }
.P7 { font-size:20pt; font-family:Arial; writing-mode:lr-tb; text-align:center ! important; }
.P8 { font-size:24pt; font-style:normal; font-weight:normal; margin-left:0cm; margin-right:0cm; text-align:center ! important; text-indent:0cm; text-shadow:none; font-family:Arial; text-decoration:none ! important; writing-mode:lr-tb; }
.T1 { font-family:Courier New; }
.T3 { vertical-align:super; font-size:58%;}
.T5 { font-family:Arial; font-size:16pt; }
.T6 { font-size:16pt; }
<!-- ODF styles with no properties representable as CSS -->
.T2 { }
</style></head><body dir="ltr" style="max-width:21.001cm;margin-top:2cm; margin-bottom:2cm; margin-left:2cm; margin-right:2cm; writing-mode:lr-tb; "><p class="P3"> </p><p class="P3"> </p><p class="P3"> </p><p class="P3"> </p><p class="P3">An Introduction to Python</p><p class="P3">for use with GNU Radio</p><p class="P4"> </p><p class="P7">Version 1.0 <span class="T2">(18</span><span class="T3">th</span><span class="T2"> April 2014)</span></p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4"> </p><p class="P4">Balint Seeber</p><p class="P4">Ettus Research</p><p class="P4"> </p><p class="P8"><span class="T5">Comments & suggetions welcome:</span></p><p class="P8"><a href="mailto:balint@ettus.com"><span class="T6">balint@ettus.com</span></a></p><p class="P8"><span class="T6">@spenchdotnet</span></p><p class="P5">At the shell prompt, enter the Python interpreter by typing:</p><p class="P1">python</p><p class="P1"> </p><p class="P2">You will see:</p><p class="P1">Python 2.7.6 (default, Jan 11 2014, 14:34:26) </p><p class="P1">[GCC 4.8.2] on linux2</p><p class="P1">Type "help", "copyright", "credits" or "license" for more information.</p><p class="P1">>>> </p><p class="P2">You are now in the interpreter and can type in Python expressions after the prompt <span class="T1">>>></span>.</p><p class="P1"> </p><p class="P2">Print strings to the screen with <span class="T1">print</span>:</p><p class="P1">>>> print "Hello World"</p><p class="P1">Hello World</p><p class="P1"> </p><p class="P2">Python will evaluate mathematic expressions:</p><p class="P1">>>> 1 + 1</p><p class="P1">2</p><p class="P1"> </p><p class="P2">If only integers are used, then results are rounded:</p><p class="P1">>>> 1 / 2</p><p class="P1">0</p><p class="P1"> </p><p class="P2">To indicate a floating-point variable, use a decimal point. This will avoid rounding because subsequent operations will use floating-point calculations:</p><p class="P1">>>> 1. / 2</p><p class="P1">0.5</p><p class="P1"> </p><p class="P2">To perform integer divisions, regardless of whether the operands are integers or floating-point values, you can use the double slash <span class="T1">//</span>:</p><p class="P1">>>> 1. // 2</p><p class="P1">0.0</p><p class="P1"> </p><p class="P2">For more advanced mathematical operations, <span class="T1">import</span> the math module. Modules are external libraries that can be pulled in for use in your program.</p><p class="P1">>>> import math</p><p class="P1">>>> math.log10(1000)</p><p class="P1">3.0</p><p class="P1"> </p><p class="P2">To list what is available in a module, use <span class="T1">dir</span>:</p><p class="P1">>>> dir(math)</p><p class="P1">['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']</p><p class="P1"> </p><p class="P2">When an expression is evaluated, the result is printed to the screen:</p><p class="P1">>>> math.sin(0.4)</p><p class="P1">0.3894183423086505</p><p class="P1"/><p class="P2">Don't forget to prepend the module name (here: <span class="T1">math.</span>) when calling a function inside a module imported as above:</p><p class="P1">>>> 5 * sin(5)</p><p class="P1">Traceback (most recent call last):</p><p class="P1"> File "<stdin>", line 1, in <module></p><p class="P1">NameError: name 'sin' is not defined</p><p class="P1"> </p><p class="P2">Assign the result of an expression to a variable. In this case the result is not printed.</p><p class="P1">>>> my_number = 5 * math.sin(5)</p><p class="P1"> </p><p class="P1">To show the result, treat it as a simple expression:</p><p class="P1">>>> my_number</p><p class="P1">-4.794621373315692</p><p class="P1"> </p><p class="P2">Or <span class="T1">print</span> it out:</p><p class="P1">>>> print my_number</p><p class="P1">-4.79462137332</p><p class="P1"> </p><p class="P2">Lists are denoted by square brackets. Assign an empty list to a variable:</p><p class="P1">>>> my_list = []</p><p class="P1"> </p><p class="P2">Populate a list by separating items with commas:</p><p class="P1">>>> my_list = [1,2,3,4,5]</p><p class="P1"> </p><p class="P2">Print the contents of the list:</p><p class="P1">>>> my_list</p><p class="P1">[1, 2, 3, 4, 5]</p><p class="P1"> </p><p class="P2">Lists can contain items of any type (e.g. integers and strings – strings can be denoted by a matched single or double quote):</p><p class="P1">>>> my_list = [1,2,3,4,5,'a','b',"Hello World",my_number]</p><p class="P1">>>> my_list</p><p class="P1">[1, 2, 3, 4, 5, 'a', 'b', 'Hello World', -4.794621373315692]</p><p class="P1"> </p><p class="P2">Obtain the type of a variable with the <span class="T1">type</span> function:</p><p class="P1">>>> type(my_list)</p><p class="P1"><type 'list'></p><p class="P1"> </p><p class="P2">To select one element from a list, use square brackets with the item's zero-based index:</p><p class="P1">>>> my_list[4]</p><p class="P1">5</p><p class="P1">>>> my_list[7]</p><p class="P1">'Hello World'</p><p class="P1"> </p><p class="P2">A negative index can be used to select items counting back from the end of the list:</p><p class="P1">>>> my_list[-1]</p><p class="P1">-4.794621373315692</p><p class="P1"> </p><p class="P2">The len function returns the length of a list:</p><p class="P1">>>> len(my_list)</p><p class="P1">9</p><p class="P1"> </p><p class="P2">A range of items can be selected from the list by using <span class="T1">[</span>(start index)<span class="T1">:</span>(one greater than the end index)<span class="T1">]</span>:</p><p class="P1">>>> my_list[1:3]</p><p class="P1">[2, 3]</p><p class="P1"> </p><p class="P2">Leaving off the start or end parts will return the rest of list before/after the given index. If the end is omitted, the rest of the list is returned beginning at the start index:</p><p class="P1">>>> my_list[1:]</p><p class="P1">[2, 3, 4, 5, 'a', 'b', 'Heloo World', -4.794621373315692]</p><p class="P1"> </p><p class="P2">If an index is used that is greater than the list's length, an empty list is returned:</p><p class="P1">>>> my_list[9:]</p><p class="P1">[]</p><p class="P1">>>> my_list[10:]</p><p class="P1">[]</p><p class="P1"> </p><p class="P2">If the start index is omitted, list elements up to the end index are returned:</p><p class="P1">>>> my_list[:3]</p><p class="P1">[1, 2, 3]</p><p class="P1"> </p><p class="P2">In addition to the start and end indices, a third optional argument can be passed after the end index to set the index step size. Here the start and end indices are omitted so all items are considered, but the step of -1 means item indices will be counted in reverse. This effectively reverses the list:</p><p class="P1">>>> my_list[::-1]</p><p class="P1">[-4.794621373315692, 'Hello World', 'b', 'a', 5, 4, 3, 2, 1]</p><p class="P1"> </p><p class="P2">To set a new item at an existing index, perform a norma variable assignment:</p><p class="P1">>>> my_list[0]</p><p class="P1">1</p><p class="P1">>>> my_list[0] = "Good day:"</p><p class="P1">>>> my_list</p><p class="P1">['Good day:', 2, 3, 4, 5, 'a', 'b', 'Hello World', -4.794621373315692]</p><p class="P1"> </p><p class="P2">To append a list to an existing one, use the addition operator. It is not possible to append a single item on its own to a list, so you must wrap the item in a new list first:</p><p class="P1">>>> my_list += ["A new item"]</p><p class="P1">>>> my_list</p><p class="P1">['Good day:', 2, 3, 4, 5, 'a', 'b', 'Hello World', -4.794621373315692, 'A new item']</p><p class="P1"> </p><p class="P2">A list instance is an object with functions. For example, <span class="T1">remove</span> will delete the first instance an item from a list (e.g. <span class="T1">2</span> here is not an index – it is the actual item):</p><p class="P1">>>> my_list.remove(2)</p><p class="P1">>>> my_list</p><p class="P1">['Good day:', 3, 4, 5, 'a', 'b', 'Hello World', -4.794621373315692, 'A new item']</p><p class="P1">>>> my_list.remove('A new item')</p><p class="P1">>>> my_list</p><p class="P1">['Good day:', 3, 4, 5, 'a', 'b', 'Hello World', -4.794621373315692]</p><p class="P1"> </p><p class="P2">A Python dictionary is a mapping of keys to values, and is indicated by curly braces. This is an empty dictionary:</p><p class="P1">>>> b = {}</p><p class="P1">>>> b</p><p class="P1">{}</p><p class="P1"> </p><p class="P2">Initialise a dictionary with some keys and values. Keys and values are paired with a colon, and key-value pairs are separated by a comma:</p><p class="P1">>>> b = {1:2, 3:4, 5:6}</p><p class="P1">>>> b</p><p class="P1">{1: 2, 3: 4, 5: 6}</p><p class="P1"> </p><p class="P2">To retrieve the value of a given key, supply the key (note this is the same syntax as with a list, however here the value is a key, not an index):</p><p class="P1">>>> b[1]</p><p class="P1">2</p><p class="P1">>>> b[5]</p><p class="P1">6</p><p class="P1"> </p><p class="P2">To assign a new value to a key (which may, or may not, exist):</p><p class="P1">>>> b[5] = 10</p><p class="P1">>>> b[5]</p><p class="P1">10</p><p class="P1">>>> b</p><p class="P1">{1: 2, 3: 4, 5: 10}</p><p class="P1"> </p><p class="P2">Dictionaries can also store arbitrary types:</p><p class="P1">>>> b['hello'] = 'bye'</p><p class="P1">>>> b</p><p class="P1">{1: 2, 3: 4, 5: 10, 'hello': 'bye'}</p><p class="P1">>>> b['hello']</p><p class="P1">'bye'</p><p class="P1">>>> b[5]</p><p class="P1">10</p><p class="P1"> </p><p class="P2">If a key does not exist, an exception is thrown:</p><p class="P1">>>> b[11]</p><p class="P1">Traceback (most recent call last):</p><p class="P1"> File "<stdin>", line 1, in <module></p><p class="P1">KeyError: 11</p><p class="P1"> </p><p class="P2">A dictionary is also an object with functions. To retrieve a dictionary's keys as a list:</p><p class="P1">>>> b.keys()</p><p class="P1">[1, 3, 5, 'hello']</p><p class="P1"> </p><p class="P2">The <span class="T1">in</span> keyword can be used to test if an item is in a list:</p><p class="P1">>>> 11 in b.keys()</p><p class="P1">False</p><p class="P1">>>> 1 in b.keys()</p><p class="P1">True</p><p class="P1"> </p><p class="P2">The <span class="T1">if</span> construct evaluates an expression and executes the following code if the evaluation is true. Here it is done in a one-liner (usually it occupies multiple lines, and the interpretter allows you to enter more code by prompting you with <span class="T1">…</span> . To indicate the last line, press the Enter key and the code will be evaluated).</p><p class="P1">>>> if 1 in b.keys(): print "All good"</p><p class="P1">... </p><p class="P1">All good</p><p class="P1"> </p><p class="P1"> </p><p class="P2">Python has many built-in functions. For example, <span class="T1">sum</span> will add together all the elements of a list (provided they support being added together). They are listed here:</p><p class="P2"><a href="http://docs.python.org/2.7/library/functions.html">http://docs.python.org/2.7/library/functions.html</a></p><p class="P1">>>> nums = [1,2,3,4,5,6]</p><p class="P1">>>> sum(nums)</p><p class="P1">21</p><p class="P1"> </p><p class="P2">Another is <span class="T1">sorted</span>, which will sort a list. Typing a function on its own doesn't call the function – it returns the function as an object.</p><p class="P1">>>> sorted</p><p class="P1"><built-in function sorted></p><p class="P1"> </p><p class="P2">Since <span class="T1">nums</span> is already in ascending order, we reverse it first, and then sort it to demonstrate <span class="T1">sorted</span>:</p><p class="P1">>>> sorted(nums[::-1])</p><p class="P1">[1, 2, 3, 4, 5, 6]</p><p class="P1"> </p><p class="P2">The sorted list can be ordered in a descending manner by re-using the list step-size trick:</p><p class="P1">>>> sorted(nums[::-1])[::-1]</p><p class="P1">[6, 5, 4, 3, 2, 1]</p><p class="P1"> </p><p class="P2">A list can be reversed using the <span class="T1">reverse</span> function, but this modifies the existing list in-place (as opposed to creating a copy of the list, reversing that and returning that copy as has been done previously with <span class="T1">[::-1]</span>). This is shown here as there is no objected returned and printed after evaluating the expression:</p><p class="P1">>>> sorted(nums[::-1]).reverse()</p><p class="P1"> </p><p class="P2">Instead we operate on a variable that we can print out ourselves:</p><p class="P1">>>> the_list = sorted(nums[::-1])</p><p class="P1">>>> the_list</p><p class="P1">[1, 2, 3, 4, 5, 6]</p><p class="P1">>>> the_list.reverse()</p><p class="P1">>>> the_list</p><p class="P1">[6, 5, 4, 3, 2, 1]</p><p class="P1"> </p><p class="P2">Strings can be treated like a list of characters:</p><p class="P1">>>> a_string = "ICTP"</p><p class="P1">>>> "ICTP"[1:2]</p><p class="P1">'C'</p><p class="P1">>>> "ICTP"[1:]</p><p class="P1">'CTP'</p><p class="P1">>>> a_string</p><p class="P1">'ICTP'</p><p class="P1"> </p><p class="P2">The built-in function <span class="T1">ord</span> returns the ASCII character code for a single character:</p><p class="P1">>>> ord('a')</p><p class="P1">97</p><p class="P1"> </p><p class="P2">The built-in <span class="T1">map</span> applies a function (the first argument) to a list of items (the second argument), and returns a new list with the result of each evaluation of the function with each item. Here we transform a string into a list of the constituent characters as ASCII codes by having <span class="T1">map</span> call <span class="T1">ord</span> for each element in the supplied string:</p><p class="P1">>>> map(ord, "ICTP")</p><p class="P1">[73, 67, 84, 80]</p><p class="P1"> </p><p class="P2">We can then perform another map using the built-in function <span class="T1">chr</span>, which converts ASCII character codes back into the actual character, to restore the original letters. Note this produces a list, not a string:</p><p class="P1">>>> map(chr, map(ord, "ICTP"))</p><p class="P1">['I', 'C', 'T', 'P']</p><p class="P1"> </p><p class="P2">To create a single string from a list of strings (or single characters, which are a string of length one), the <span class="T1">join</span> function can be used. <span class="T1">join</span> is a member function of the string object. Here we use it on an empty string, since the given string is inserted between each item in the supplied list of strings to be joined.</p><p class="P1">>>> "".join(map(chr, map(ord, "ICTP")))</p><p class="P1">'ICTP'</p><p class="P1"> </p><p class="P2">Lambda functions are anonymous functions that can be used as arguments to other functions, such as <span class="T1">map</span>. This is a short-hand way of describing a simple expression without having to write a proper Python function. Here we will use it to perform a simplistic tranposition cipher. A lambda function is denoted as <span class="T1">lambda <arguments' variables>: <expression></span></p><p class="P1">>>> step1 = map(ord, "ICTP")</p><p class="P1">>>> step1</p><p class="P1">[73, 67, 84, 80]</p><p class="P1">>>> step2 = map(lambda x: x+1, step1)</p><p class="P1">>>> step2</p><p class="P1">[74, 68, 85, 81]</p><p class="P2">Note that <span class="T1">x</span> is the arbitrarily-chosen variable name to represent the single item to be evaluated by the lambda expression (<span class="T1">x+1</span>). For each ASCII character code, <span class="T1">map</span> applies the lambda function to the code, resulting in it being incremented by one. A new list is returned where each code is one greater than the original code.</p><p class="P1"> </p><p class="P2">We can then use <span class="T1">join</span> to create a new string:</p><p class="P1">>>> "".join(map(chr,step2))</p><p class="P1">'JDUQ'</p><p class="P1"> </p><p class="P2">If we need to store the result, a variable can be used:</p><p class="P1">>>> step3 = map(chr,step2)</p><p class="P1">>>> step3</p><p class="P1">['J', 'D', 'U', 'Q']</p><p class="P1">>>> "".join(step3)</p><p class="P1">'JDUQ'</p><p class="P1"> </p></body></html>