Considering Data Types in IronPython

Must Read

Internal security risks

If your business replaces its desktop software with webbased applications, or its internal firewall-protected servers with externally hosted systems,...

Testing Your Landing Page

There is always something you can do a bit better to maximize your landing page results. There are any...

DUI Attorneys Southern California

DUI attorneys in southern California have dramatically grown in numbers over the last 5 years to an all time...

You might have noticed an appalling lack of data types in the example so far. When an application needs a string variable, it simply assigns the string to a variable without ever specifying a data type. Likewise, numbers are simply assigned to a variable; it doesn’t apparently matter  whether the number is an integer or a floating point value. IronPython seemingly doesn’t care. For example, the code in Listing 2-1 works just fine.

Listin g 2-1: Working with variables of various types

String = “Hello”
Integer = 1
Float = 1.5
print “String =”, String, “nInteger =”, Integer, “nFloat =”, Float
raw_input(‘Press any key to continue…’)

This example shows that you can create a string, integer, or floating point value without actually declaring them. IronPython does track the data type, but it does so in the background, without your knowledge. When you try to print the values, IronPython automatically converts the values in the background for you to standard strings. Figure 2-16 shows the output of this example.

You may also notice that this example makes use of escape characters, the n in this case. The n simply tells IronPython to add a newline character to the output. In fact, if you’ve worked with C, C++, C#, or any of a number of languages that rely on escape codes, you’ll find yourself right at home when working with IronPython. Table 2-1 shows the common escape codes that IronPython recognizes.

IronPython tracks data type and performs conversions automatically as needed.

IronPython Common Escape Codes

The raw_input() function is also new. If you’re working in Visual Studio, the console screen appears and disappears so quickly in many cases that you can’t see the output of your code. Using the raw_input() function as shown simply adds a pause to your code. The string included with the function call provides a prompt. You’ll discover more about input and output functionality as the book progresses. For right now, all you really need to know is that you can obtain a value from the console using raw_input().

Don’t get the idea that you won’t have any access to native data types. It’s possible to tell IronPython to react in a certain way to your data. For example, you can use the str() and repr() functions to interpret a string in multiple ways. In addition, you can specifically convert  numbers to a specific type, as shown in Listing 2-2.

Listin g 2-2: Specifying data types

# Create a string variable.
String = ‘Hello and Goodbye’
# Display using str() and repr()
print ‘A string and its representation.’
print ‘str()‘, str(String)
print ‘repr()‘, repr(String)
# Create the numeric variable.
Number = 5.1
# Display the numeric types.
print ‘nThe numeric data types.’
print ‘int’, int(Number)
print ‘long’, long(Number)
print ‘float’, float(Number)
print ‘complex’, complex(Number, 3.5)
# Pause the display
raw_input(‘Press any key…’)

This example begins by showing you a comment. Yes, IronPython, like most computer languages, supports comments and you should use them often.

The first part of the code works with a string. When you use the str() function, you tell IronPython to take any data within the function, the expression, and convert it to a string. IronPython performs this task for you automatically, but you can use the str() function for explicit conversion. The output of the str() function is always in human-readable form. The repr() function returns a representation of the object you pass to it. In most cases, you use the repr() function to create output that the interpreter understands. Figure 2-17 shows the difference in output between the two functions. Notice that repr() outputs single quotes so you could pass this information directly to the interpreter.

The second part of the code works with a number. In this case, the number contains floating point data. However, you can convert it directly to an int or long using the int() or long() functions. The float() function will convert any numeric data into a floating point number and you can even create complex numbers using the complex() function. Figure 2-17 shows the output from the numeric conversions as well. IronPython also supports base conversions using the oct() and hex() functions. In fact, you can see the entire list of built-in functions at

Use str() or repr() as appropriate to create output for your application.

You’ll see a wealth of data types as the book progresses. For example, Chapter 4 discusses arrays and collections, Chapter 5 tells you all about objects, and Chapter 6 begins a discussion of working with the standard library. There are a number of online resources for working with data types as well. The two best sources — the ones used for this book — are at:




Please enter your comment!
Please enter your name here

Latest News

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits,...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

High Key Lighting Techniques for Professional Photographers

I’ve written many times about high key lighting techniques and how to achieve them. The term “high key” is a bit misleading. As I’ve...

More Articles Like This