Python Variables in GRC

This tutorial describes how Python data types are used in GRC and how the variables are displayed.

The previous tutorial, Your First Flowgraph, shows how to build a simple flowgraph. The next tutorial, Variables in Flowgraphs, describes how to use and modify variables in a more sophisticated flowgraph.

Floats and Integers in GRC
GNU Radio Companion (GRC) uses Python data types to represent variables. The simplest data types describe numbers. Numbers in Python can be floating point or integers:

Integers can be converted to floating point using float, and floating point numbers can be converted to integers using int:

Type conversion can be done within the variable blocks:



The value is displayed as an integer:



GRC displays numbers differently than Python. For example, the samp_rate block is added to every new flowgraph.



Double-click the samp_rate variable to edit the properties:



The value of samp_rate is 32000 yet GRC displays the value 32k. GRC converts all numbers into SI Units. Note that GRC may display a number in a different format than it is represented in Python.

For another example, drag and drop a new variable into the workspace. Double-click to edit the properties:


 * Id: floatNumber
 * Value: 0.25



GRC now displays the value 0.25 as 250m because it has been converted to SI units:



Strings in GRC
Python uses both single quotes ' and double quotes " to contain strings:

Strings can be used as variables in GRC:



The string is displayed in GRC:



Lists and Tuples in GRC
Variables in GRC can use Python lists:



The list is displayed in GRC:



Variables in GRC can use Python tuples:



The tuple is displayed in GRC:



List Comprehension
Each Variable is a single line in Python:

Id = Value

List comprehension can be used to write functions in a Variable. For example, list comprehension is used to loop through a list, add +1 to all entries, and then multiply each entry by 2:

This list comprehension example is used in GNU Radio by using two variables, listVariable and listComprehensionExample, and entering their associated values:





The lists are displayed in GRC:



The next tutorial, Variables in Flowgraphs, describes how to use and modify variables in a more sophisticated flowgraph.