I've managed to get the sequencing correct, however I'm unsure how to have it print on the same line. I've got this: Show
and have tried this:
Pier Paolo 8681 gold badge14 silver badges22 bronze badges asked Mar 24, 2016 at 13:49
Josh AlexandreJosh Alexandre 1172 gold badges3 silver badges11 bronze badges 1 Judging by your first (working) code, you are probably using Python 2. To use
Alternatively, use the old Python 2
Or use
answered Mar 24, 2016 at 13:55
1 You can use a range using print as a function and specifying the sep arg and unpack with
Output:
You are also using python 2 not python 3 in your first code or your print would cause a syntax error so use raw_input and cast to int. For python 3 just cast input to int and use the same logic:
answered Mar 24, 2016 at 14:01
1 You can use a temporary string like so:
answered Mar 24, 2016 at 13:56
Trev DaviesTrev Davies 3821 silver badge10 bronze badges 1 this is coaded in java. the two nested loop are just to print the pattern and stop varialbe is used to terminate loop when we get the required sequence of integer.
answered Jul 7, 2020 at 22:28
1 Modern computers can do millions or even billions of instructions a second. With the techniques discussed so far, it would be hard to get a program that would run by itself for more than a fraction of a second. Practically, we cannot write millions of instructions to keep the computer busy. To keep a computer doing useful work we need repetition, looping back over the same block of code again and again. There are two Python statement
types to do that: the simpler Two preliminaries:
Then we put this all together. This is a long section. Go slowly and carefully. 1.13.1. Updating Variables¶The programs so far have defined and used
variables, but other than in early shell examples we have not changed the value of existing variables. For now consider a particularly simple example, just chosen as an illustration, in the example file
Can you predict the result? Run the program and check. Particularly if you did not guess right, it is important to understand what happens, one step at a time. That means keeping track of what changes to variables are made by each statement. In the table below, statements are referred to by the numbers labeling the lines in the code above. We can track the state of each variable after each line is executed. A dash is shown where a variable is not defined. For instance after line 1 is executed, a value is given to x, but y is still undefined. Then y gets a value in line 2. The comment on the right summarizes what is happening. Since x has the value 3 when line 2 starts, x+2 is the same as 3+2. In line three we use the fact that the right side of an assignment statement uses the values of variables when the line starts executing (what is left after the previous line of the table executed), but the assignment to the variable y on the left causes a change to y, and hence the updated value of y, 10, is shown in the table. Line 4 then changes x, using the latest value of y (10, not the initial value 5!). The result from line 5 confirms the values of x and y.
When we create such a table, the order of execution will always be the order of the lines in the table. In this simple sequential code, that also follows the textual order of the program. Following each line of execution of a program in the proper order of execution, carefully, keeping track of the current values of variables, will be called playing computer. A table like the one above is an organized way to keep track. 1.13.2. The list Type¶Lists are ordered sequences of arbitrary data. Lists are the first kind of data discussed so far that are mutable: the length of the sequence can be changed and elements substituted. We will delay the discussion of changes to lists until a further introduction to objects. Lists can be written explicitly. Read the following examples ['red', 'green', 'blue'] [1, 3, 5, 7, 9, 11] ['silly', 57, 'mixed', -23, 'example'] [] # the empty list [ [7, 11], [1], [] ] # list containing three elements; each a list The basic format is a square-bracket-enclosed, comma-separated list of arbitrary data. 1.13.3. The range Function, Part 1¶There is a built-in function list(range(4)) list(range(10)) The general pattern for use is
This syntax will generate the integers, one at a time, as needed [1]. If you want to see all the results at once as a list, you can convert to a With more parameters, the
1.13.4. Basic for Loops¶Try the following in the Shell. You get a sequence of continuation lines before the Shell responds. After seeing the colon at the end of the first line, the Shell knows later lines are to be indented. Be sure to enter another empty line.
(Just press
This is a
indented statements to repeat; may use item The block of lines is repeated once for each element of the sequence, so in this example the two lines in the indented block are repeated three times. Furthermore the variable in the heading (
When executing step by step, note that the
Note When playing
computer with a loop, the same line numbers can reappear over and over, because the A When you used the Shell to enter a loop, there was a reason that the interpreter waited to respond until after you entered an empty line: The interpreter did not know how long the loop block was going to be! The empty line is a signal to the interpreter that you are done with the loop block, and hence ready to execute the complete compound loop statement. Look at the following example program for count in [1, 2, 3]: print(count) print('Yes' * count) print('Done counting.') for color in ['red', 'blue', 'green']: print(color) In a file, where the interpreter does not need to respond immediately, the blank line is not necessary.
Instead, as with a function definition or any other format with an indented block, you indicate being past the indented block by dedenting. Here the following As with the indented block in a function, it is important to get the indentation right. Alter the code above, so the fourth line is indented: for count in [1, 2, 3]: print(count) print('Yes' * count) print('Done counting.') # changed so indented for color in ['red', 'blue', 'green']: print(color) Predict the change, and run the code again to test. Loops are one of the most important features in programming. While the
do something with the current item (It would be even more like English if In the We can use a for-each loop to revise our first example in the Tutorial. Recall the code from madlib.py: addPick('animal', userPicks) addPick('food', userPicks) addPick('city', userPicks) Each line is doing exactly the same thing, except varying the string used as the cue, while repeating the rest of the line. This is the for-each pattern, but we need to list the sequence that the cues come from. Read the alternative: for cue in ['animal', 'food', 'city']: # heading addPick(cue, userPicks) # body Seeing this feature requires the ability to abstract the general pattern from the group of examples. This is essential for using loops effectively. If you wish to see or run the
whole program with this small modification, see the example Note the logic of the transformation between the two program versions: The alternative pieces of data are collected in the list in the It is important to understand the sequence of operations, how execution goes back and forth between the heading and the body. Here are the details:
In this example the data values are just a few given literals, and there is only one line in the repeated pattern. Hence the use of a 1.13.4.1. Pattern Loop Exercise¶Write a two-line for-each loop in a file print(2, type(2)) print(3.5, type(3.5)) print([], type([])) print(True, type(True)) print(None, type(None)) Execute both versions to check yourself. Hint 1: [2] Hint 2: [3] 1.13.4.2. Triple Exercise¶Complete the following function. This starting code is in def tripleAll(nums): ''' print triple each of the numbers in the list nums. >>> tripleAll([2, 4, 1, 5]) 6 12 3 15 >>> tripleAll([-6]) -18 '''
1.13.5. Simple Repeat Loop¶The examples above all used the value of the variable in the ''' A simple repeat loop''' for i in range(10): print('Hello') In this situation, the variable The user could choose the number of times to repeat. Read and run the example program '''The number of repetitions is specified by the user.''' n = int(input('Enter the number of times to repeat: ')) for i in range(n): print('This is repetitious!') When you are reading code, you look at
variable names as they are introduced, and see where they are used later. In the simple repeat loops above, the loop variable One convention to indicate the simple repeat loop variable is never used again, is to use the special variable name for _ in range(10): print('Hello') 1.13.6. Successive Modification Loops¶Suppose I have a list of items called 1 red 2 orange 3 yellow 4 green Read about the following thought process for developing this: If I allow myself to omit the
numbers, it is easy: For any and I just go through the list and do it for each one. (Copy and run if you like.) items = ['red', 'orange', 'yellow', 'green'] for item in items: print(item) Clearly the more elaborate version with numbers has a pattern with some consistency, each line is at least in the form:
but the number changes each time, and the numbers do not come straight from the list of items. A variable can
change, so it makes sense to have a variable '''In this version number does not change.''' items = ['red', 'orange', 'yellow', 'green'] number = 1 for item in items: print(number, item) Of course this is still not completely correct, since the idea was to count. After the first time number is printed, it needs to be changed to 2, to be right the next time through the loop, as in the following code: Read and run
the example program '''prints poorly numbered entries from the list''' items = ['red', 'orange', 'yellow', 'green'] number = 1 for item in items: print(number, item) number = 2 # will change to 2 after printing 1 This is closer, but still not completely correct, since we never get to 3! We need a way to change the value of number that will work each time through the loop. The pattern of counting is simple, so simple in fact that you probably do not think consciously about how you go from one number to the next: You can describe the pattern by saying each successive number is one more than the previous number. We need to be able to
change
It is important to understand the step-by-step changes during execution. Below is another table showing the results of playing computer. The line numbers are much more important here to keep track of the flow of control, because of the jumping around at the end of the loop. Again note that the program line numbers in the Line column of the playing computer table are not all listed sequentially, because the For compactness, the variable
The final value of number is never used, but that is OK. What we want gets printed. Go through carefully and be sure you understand the meaning of each entry in the table, and the reason for the sequencing and the reason for the exact position of each entry in each step where it changes! In particular see how and why the line number for each successive row is not always one more than the previous row. In particular, see how the same sequence of numbered lines may be repeated in multiple places in the table. Without this understanding you will not be able to play computer yourself and really understand loops. This short example illustrates a lot of ideas important to loops:
There is a general pattern to loops with successive modification of a variable like
This information can be put in a code outline: Initialize variables to be modified Loop heading controlling the repetition: Do the desired action with the current variables Modify variables to be ready for the action the next time If you compare this pattern to the for-each and simple repeat loops in Basic for Loops, you see that the examples there were simpler. There was no explicit variable modification needed to prepare for the next time though the loop. We will refer to the latest, more general pattern as a successive modification loop. Functions are handy for encapsulating an idea for use and reuse in a program, and also for testing. We can write a function to number a list, and easily test it with different data. Read and run the example program ''' use a function to number the entries in any list''' def numberList(items): '''Print each item in a list items, numbered in order.''' number = 1 for item in items: print(number, item) number = number + 1 def main(): numberList(['red', 'orange', 'yellow', 'green']) print() numberList(['apples', 'pears', 'bananas']) main() Make sure you can follow the whole sequence, step by step! This program has the most complicated flow of control so far, changing both for function calls and loops.
1.13.7. Accumulation Loops¶Suppose
you want to add up all the numbers in a list, def sumList(nums): '''Return the sum of the numbers in nums.''' If you do not see what to do right away, a useful thing to do is write down a concrete case, and think how you would solve it, in complete detail. If
Since the list may be arbitrarily long, you need a loop. Hence you must find a pattern so that you can keep reusing the same statements in the loop. Obviously you are using each number in the sequence in order. You also generate a sum in each step, which you reuse in the next step. The pattern is different, however, in the first line, 2+6 is 8: there is no previous sum, and you use two elements from the list. The 2 is not added to a previous sum. Although it is not the shortest way to do the calculation by hand, 2 is a sum of 0 + 2: We can make the pattern consistent and calculate:
Then the second part of each sum is a number from the list, The trick is to use the same line of code the next time through the loop. That means what was sum = 0 for num in nums: nextSum = sum + num sum = nextSum Do you see the pattern? Again it is
Sometimes the two general loop steps can be combined. This is such a
case. Since sum = 0 for num in nums: sum = sum + num so the whole function, with the
The example program print(sumList([5, 2, 4, 7])) The pattern used here is certainly successive modification (of the initialize the accumulation to include none of the
sequence (
new value of accumulation = result of combining item with last value of accumulation This pattern will work in many other situations besides adding numbers. English loop terminology: Of course you need to be able to go from an English description of a problem to a plan and then implement it in Python. In particular, it will be very important to realize when you will need your program to have a loop through a sequence. What are some common words or phrases that suggest a loop? After thinking for yourself, compare: [4] Once you see this need for a loop, you need to plan your code. There are a bunch of questions you can routinely ask yourself about fleshing out the outline for the loop part of your code:
1.13.7.1. Play Computer sumList Exercise¶Suppose the function Make sure there is a row in the table for each line executed in the program, with a separate line entry for each time a line is executed. In each row enter which program line is being executed, and show all changes caused to variables by the execution of that one line. Display line numbers as shown in the margin beside the example code in the Tutorial. (The separate Python files themselves do not show the line numbers.) A table is started for you below. The final row that you enter in your your table should be for an execution of line numbered 6 in the code, and your comment can be, “return 18”. If the same variable value in one column repeats through several rows, it is more convenient just leave the later entries blank, rather than keep copying. With this convention, the current value of a variable is the last value recorded in a previous line in the table. This is the first “Play Computer” exercise with a loop. Be sure to look back at the earlier play computer examples. The lines in the loop (and hence their line numbers) repeat multiple times as rows in the table, as you follow the loop one time through after another! The original parameter, which does not change, does not have a column in the table, for compactness. The start of the table is shown below. As
shown in the first comment, throughout the function call,
1.13.7.2. Test sumList Exercise¶Write a program 1.13.7.3. Join All Exercise¶* Complete the following function. This starting code is in def joinStrings(stringList): '''Join all the strings in stringList into one string, and return the result, NOT printing it. For example: >>> s = joinStrings(['very', 'hot', 'day']) # returns string >>> print(s) veryhotday ''' First Hint: [5] Second Hint: [6] 1.13.8. More Playing Computer¶Testing code by running it is fine, but looking at the results does not mean you really understand what is going on, particularly if there is an error! People who do not understand what is happening are likely to make random changes to their code in an attempt to fix errors. This is a very bad, increasingly self-defeating practice, since you are likely to never learn where the real problem lies, and the same problem is likely to come back to bite you. It is important to be able to predict accurately what code will do. We have illustrated playing computer on a variety of small chunks of code. Playing computer can help you find bugs (errors in your code). Some errors are syntax errors caught by the interpreter in translation. Some errors are only caught by the interpreter during execution, like failing to have a value for a variable you use. Other errors are not caught by the interpreter at all - you just get the wrong answer. These are called logical errors. Earlier logical errors can also trigger an execution error later. This is when playing computer is particularly useful. A common error in trying to write the
You can run this code and see that it produces the wrong answer. If you play computer on the call to
If you go step by step you should see where the incorrect 1 came from: the initialization is repeated each time in the loop at line 4, undoing the incrementing of Warning Always be careful that your one-time initialization for a loop goes before the loop, not in it! Functions can also return values. Consider the Python for this mathematical sequence: define the function m(x) = 5x, let y = 3; find m(y) + m(2y-1):
This code is in example
Thus far most of the code given has been motivated first, so you are likely to have an idea what to expect. You may need to read code written by someone else (or even yourself a while back!) where you are not sure what is intended. Also you might make a mistake and accidental write code that does something unintended! If you really understand how Python works, one line at a time, you should be able to play computer and follow at least short code sequences that have not been explained before. It is useful to read another person’s code and try to follow it. The next exercises also provides code that has not been explained first, or has a mistake. 1.13.8.1. Play Computer Odd Loop Exercise¶* Work in a word processor (not Idle!), starting from example
Play computer on the following code:
Reality check: 31 is printed when line 6 finally executes. The start of the table for this exercise is shown below.
1.13.8.2. Play Computer Error Exercise¶* In a word processor add to the file The following code is supposed to compute the product of the numbers in a list. For instance
The code for this exercise appears in the example file A major use of playing computer is to see exactly where the data that you expect gets messed up. Play computer on a call to The table headings and the first row of the table for this exercise are shown below.
Then you can stop and fix it: First copy 1.13.8.3. Play Computer Functions Exercise¶* In a word processor once again add to the file Play computer on the following code:
Reality check: 70 is printed. The table headings and the first row of the table for this exercise are shown below.
You will revisit line 3 several times, with table lines for function 1.13.9. The print function keyword end¶By
default the print function adds a newline to the end of the string being printed. This can be overridden by including the keyword parameter print('all', 'on', 'same', 'line') print('different line') is equivalent to print('all', 'on' , end=' ') print('same', end=' ') print('line') print('different line') This does not work directly in the shell (where you are always forced to a new line at the end). It does work in a program, but it is not very useful except in a loop! Suppose I want to print a line with all the elements of a list, separated by spaces, but not on separate lines. I can use the def listOnOneLine(items): for item in items: print(item, end=' ') listOnOneLine(['apple', 'banana', 'pear']) print('This may not be what you expected!') If you still want to go on
to a new line at the end of the loop, you must include a print function that does advance to the next line, once, after the loop. Try this variation, def listOnOneLine(items): for item in items: print(item, end=' ') print() listOnOneLine(['apple', 'banana', 'pear']) print('This is probably better!')
How do I print a sequence of data in Python?Code -. n = int(input("Enter the number of rows: ")). m = (2 * n) - 2.. for i in range(0, n):. for j in range(0, m):. print(end=" "). m = m - 1 # decrementing m after each loop.. for j in range(0, i + 1):. # printing full Triangle pyramid using stars.. How do you print a sequence on one line in Python?To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
How do you represent a sequence in Python?Introduction to Python sequences
And you can refer to any item in the sequence by using its index number e.g., s[0] and s[1] . In Python, the sequence index starts at 0, not 1. So the first element is s[0] and the second element is s[1] . If the sequence s has n items, the last item is s[n-1] .
How do I print 12345 in Python?how to print 12345 in one row without using string. +8. print(*(n for n in range(1,6)), sep="") or just print(12345) as suggested by Diego. ... . +6. Language - Python This might be cheating but try it: num = 12345; print(num) And next time please specify the programming language. ... . +5. ... . +3. ... . +2. ... . +2. ... . +1. ... . |