Indentation and line breaks, Python Programming

 

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:

 

if (s == 1){

s = s + 1;

a = a - 10;

} else {

s = s + 10;

a = a + 10;

}

 

The braces  specify  what  statements are executed in the  if case.  It is suppose good  method  to indent your  code to agree  with  the brace structure, but it is not needed. In addition, the semi colons are used  to show the end of a instruction, independent of the position of the line divided in the file. So, the following code segment has the same meaning as the previous one, although it is much typical to read  and understand.

 

if (s == 1){

s = s

+ 1;      a = a - 10;

} else {

 a = a + 10;

}

 

In Python, on the  other  hand, there  are no braces  for grouping or semicolons for ending. Indentation shows grouping and line breaks  indicate statement ending. So, in Python, we would give the previous example as

 

if s == 1:

s = s + 1

a = a - 10

else:

s = s + 10

a = a + 10

 

There is no way to put  more  than  one statement on a one line.3  If you have  a instruction that  is too long for a line, you can break it with a backslash:

 

aReallyLongVariableNameThatMakesMyLinesLong = \

aReallyLongVariableNameThatMakesMyLinesLong + 1

 

It is easy for Java programmers to get confused about  colons and semi colons in Python. Here is the method:  (1) Python does  not use semi-colons; (2) Colons  are used  to start  an indented part, so they  seems on the ?rst  line of a method definition, when  starting  for loop or while loop,  and after the situation in an if, elif, or else.

Is one method better  than  the other?  No.  It is purely a function of taste.  The Python function is pretty unusual. But if you are using Python, you have to remember that indentation and line breaks are significant.

 

Posted Date: 8/9/2012 6:58:32 AM | Location : United States







Related Discussions:- Indentation and line breaks, Assignment Help, Ask Question on Indentation and line breaks, Get Answer, Expert's Help, Indentation and line breaks Discussions

Write discussion on Indentation and line breaks
Your posts are moderated
Related Questions
from urllib2 import urlopen var= ('' response">http://placekitten.com'') response = kittens.read(body) body = response[559:1000] print ''Kittens:'', response

De?nition   A method de?nition has the abstract form: def ( , ..., ): ...   There are essentially three parts: is a name for

Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry an

What are squared matrices?


draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

Ask question #Minimum 100 worIn this project, create the pseudo code from the following request. The program must have some sort of menu that allows users to make selections to

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

Primitives, Composition, Abstraction, and Patterns   We will  start  by thinking about  how  the  PCAP  framework applies to computer programs, in general. We may do that by