Understanding the Use of Indentation and Capitalization in IronPython

Must Read

School Consolidation Loan a Good Option For Your Student Debts

Understanding and discovering if school consolidation loan is good for your student debts is one of those most sought...

What Exactly Are Low Cost Home Equity Loans?

Low cost home equity loans are a type of loan through which the equity in a borrower's home is...

A Guide to Choose Digital Camera and Camcorder

Significance of the topic As computer and web become popular in human’s life, digital images play an important role to...

Promoting Your Blog

Just like your Web site, once you have a blog, you want to maximize its exposure; you want to...

Step 5. Populate Each Page with the Assigned Keyword

When you have allocated your keywords to the various pages on your site, you will populate or include the...

Will There Be A Business “Black Friday”?

In the retail world, "Black Friday" has become something of an American institution, opening the floodgates to what shops...

Most application programming languages have rules that help the compiler or interpreter understand what you mean. For example, when working with C, C++, Java, C#, and a number of other languages, you use opening and closing braces to indicate the code that belongs within a structure such as a function or loop. Without these opening and closing braces, the compiler or interpreter for the target language would never be able to understand what you mean — these braces add structure to your application code. Likewise, IronPython relies on rules, indentation and capitalization, to help the interpreter understand your code.

The interpreter does help you with the indentation. The amount you indent a line doesn’t seem to matter. Using tabs instead of spaces doesn’t seem to matter either, but using tabs will ensure that you don’t run into problems seeing the indentation in code properly. Open a copy of IPY to follow along with the discussion in this section. Try the following steps and you’ll discover how the interpreter helps you discover when to indent.

  1. Type SomeText = ‘Hello‘ and press Enter. You’ll see that the interpreter adds three greater-than signs (>>>). The >>> is a primary prompt and tells you that you don’t need to add any indentation.
  2. Type if SomeText == ‘Hello‘: and press Enter. Now you’ll see that the interpreter adds three dots (…) to the next line. The … is the secondary prompt. It tells you that you’ve entered a structure.
  3. Type print SomeText and press Enter. The interpreter displays an error message like the one shown in Figure 2-12. The interpreter expected an indented block, but you didn’t provide any indentation, so the block failed.
  4. Repeat Step 2 again. Press Tab. Type print SomeText and press Enter. This time, the entry succeeds, as shown in Figure 2-13. Notice that the interpreter has displayed … again so that you can continue the block if desired. In fact, the block continues until you end it by not indenting an entry.
  5. Press Enter. The interpreter displays the word Hello. The interpreter will keep track of a block until you finish entering the last line of code. It then evaluates the block and performs all of the tasks it contains.

Not indenting at a secondary prompt produces an error.

A properly indented entry succeeds.

Now that you have the basic idea about indentation, it’s time to consider capitalization. IronPython is case sensitive. So, if you type:

If SomeText == ‘Hello’:

and press Enter, the code will fail. Figure 2-14 shows that the error information you receive isn’t straightforward. All that the interpreter tells you is that the token it received is unexpected (a token is a single word that the interpreter parses to discover what you want to do). The interpreter will provide more precise information for some errors. Type:

if SomeText == ‘Hello’:
print sometext

and press Enter. This time you get precise error information, as shown in Figure 2-15. The interpreter tells you that the error lies in line 2 of the block and that the error occurred because SomeText isn’t defined.

Some error messages are ambiguous.

The interpreter can provide precise error information for certain classes of error.

The point of this exercise is that the interpreter will catch indentation and capitalization errors. In some cases, you might scratch your head for a while trying to figure out what went wrong, but the interpreter is accurate about locating such problems for you.

It doesn’t matter whether you use tabs or spaces for indentation. Try the example out using spaces instead of a tab. You’ll discover that it works just fine. However, using tabs provides uniform spacing that is easier to see when you’re reading the code. If you’re not careful, you might use one space in one location and four in the next, making it hard to tell when something really is indented.


Please enter your comment!
Please enter your name here

Latest News

Digital Marketing for Beginners

Digital marketing for starter, Let to basic learning about connecting with your audience in the right place...

What are 7 things poor people do that the rich don’t?

1. poor people watch TV in which people read books how many hours you spend in front of the TV and when...

Top 18 best small business ideas for beginners starting

A small business can be frightening and requires plenty of careful planning there are many small business ideas which can be beneficial...

Summer that makes you happy

We saw were already here I've been thinking about some of the things. I used to do with my husband even though...

4 Point to helpful tips specifically for caregivers

What you need to take a vacation. I know it sounds impossible creative and try to make it work for you almost...

More Articles Like This