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
Write a Python program to accomplish the following. Use modular design. Include at least 3 functions: one that returns zero values, one that returns one value, and one that retu

how do you make an if and else statment work in pytho?

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

Write an algorithm for a program that shows the use of all 6 math functions. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program infor

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t

Non-local references in procedures There  is an important subtlety in the  way  names  are handled in the  environment created by a function call. When  a value  that  is not

Task This problem creates a list containing names. The list of names will be printed, sorted, printed again in the new sorted order, written to a new output file, and searched. Ca

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

Python Programs You must submit the source code and samples of output for each program.Please do not provide python file (eg. Filename.py). Copy all source code to one word fi

Write a program that requests a password after the author/program information is displayed. Make the password "hello". The program should then ask the user for their name: if the n