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

assignment 10, #question.Program 10 Assignment (Banking Objects – Savings/...

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named 10.py that performs the following: Create a class named ChkAcct, and a class n

Conditionals-for and while, For and While   If we want  to do some o...

For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

Non-local references, Non-local references So far, whenever we needed ...

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

#1 - Pseudo Code, Ask question #Minimum 100 worIn this project, create the ...

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

Analytical models, Analytical models Analytical tools  are very importa...

Analytical models Analytical tools  are very important.  It may  be hard  to check  the  correctness of a machine by trying it in several possible  initial  conditions with  al

Programs and data , Programs and Data Object-oriented programming is a...

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

#cmdprobs, #Why can''t I cd my Desktop? A minute ago I would open up cmd an...

#Why can''t I cd my Desktop? A minute ago I would open up cmd and it starts in C:\Users\myname but now it''s starting in C:\WINDOWS\system> and I don''t where I am or how to get ou

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