Indentation and line breaks, Python Programming

Assignment Help:

 

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.

 


Related Discussions:- Indentation and line breaks

Coding examples of python, Coding examples Following are some  attempts...

Coding examples Following are some  attempts at defining a function isSubset,  which  takes  two  arguments, a and  b, and  returns True if a is a subset  of b, assuming that

Euler method, python program of motion of a particle in viscus medium

python program of motion of a particle in viscus medium

List Assignment, Task This problem creates a list containing names. The li...

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

Simple expressions, Simple expressions A cornerstone of a programming ...

Simple expressions A cornerstone of a programming language is the  ability  to compute expressions.  We will start here  with  arithmetic expressions, just to take the  concep

Program that shows the use of all 6 math functions, Write an algorithm 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

Normalize a vector, Normalize a vector Let's imagine we want  to norma...

Normalize a vector Let's imagine we want  to normalize a vector  of three  variables;  that  is to compute a new  vector  of three values,  such that its size  is 1. Here is o

Synthetic models, Synthetic models One  goal  of various  people in a ...

Synthetic models One  goal  of various  people in a variety of sub-disciplines of  electrical  engineering and  computer science is automatic synthesis of machine from  formal

Program to calculate area function, Rewrite the area.py program (shown bel...

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

Python programs, Python programs Every  general-purpose computer has a...

Python programs Every  general-purpose computer has a various detailed design, which  defines  that  the way  its program requires  to be specified is different. The "machine

Lost, Import the sample code below into the Python IDLE and enhance it, run...

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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd