# How to print numbers in python using for loop

## Problem Definition

Create a Python program to print numbers from 1 to 10 using a for loop.

## Solution

In programming, Loops are used to repeat a block of code until a specific condition is met. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Also, we are going to use one of Python’s built-in function `range()`. This function is extensively used in loops to control the number of times the loop has to run. In simple words range is used to generate a sequence between the given values.

For a better understanding of these Python, concepts it is recommended to read the following articles.

• Python’s range() Function Explained
• How To Construct For Loops In Python

## Program

``````for i in range(1, 11):
print(i)
``````

### Output

``````1
2
3
4
5
6
7
8
9
10``````

## Explanation

The for loop prints the number from 1 to 10 using the `range()` function here `i` is a temporary variable that is iterating over numbers from 1 to 10.

It’s worth mentioning that similar to list indexing in range starts from 0 which means `range( j )`will print sequence till `( j-1)` hence the output doesn’t include 6.

PROGRAMS

Iterate for loop with the given number to print n numbers in Python using for loop.

Simple example code print first n numbers in Python. First, initialize a variable “numbers” with the number of numbers you want to print. Then use for loop and print the values.

``````numbers = 10

for num in range(1, numbers + 1):
print(num, end=' ')
``````

Output:

Print numbers from 1 to N using the input function

With the input() function you can take e user input value.

``````n = int(input("Please Enter any Number: "))

print("The List of Natural Numbers from 1", "to", n)

for i in range(1, n + 1):
print(i, end='  ')``````

Output:

The List of Natural Numbers from 1 to 5
1 2 3 4 5

Do comment if you have any doubts or suggestions on this Python number topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

## 1. For loop with range

In the previous lessons we dealt with sequential programs and conditions. Often the program needs to repeat some block several times. That's where the loops come in handy. There are `for` and `while` loop operators in Python, in this lesson we cover `for`.

`for` loop iterates over any sequence. For instance, any string in Python is a sequence of its characters, so we can iterate over them using `for`:

```for character in 'hello':
print(character)
```

Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order. Such a sequence of integer can be created using the function `range(min_value, max_value)`:

```for i in range(5, 8):
print(i, i ** 2)
print('end of loop')
# 5 25
# 6 36
# 7 49
# end of loop
```

Function `range(min_value, max_value)` generates a sequence with numbers `min_value`, `min_value + 1`, ..., `max_value - 1`. The last number is not included.

There's a reduced form of range() - `range(max_value)`, in which case min_value is implicitly set to zero:

```for i in range(3):
print(i)
# 0
# 1
# 2
```

This way we can repeat some action several times:

```for i in range(2 ** 2):
print('Hello, world!')
```

Same as with if-else, indentation is what specifies which instructions are controlled by `for` and which aren't.

Range() can define an empty sequence, like `range(-5)` or `range(7, 3)`. In this case the for-block won't be executed:

```for i in range(-5):
print('Hello, world!')
```

Let's have more complex example and sum the integers from 1 to n inclusively.

```result = 0
n = 5
for i in range(1, n + 1):
result += i
# this ^^ is the shorthand for
# result = result + i
print(result)
```

Pay attention that maximum value in range() is `n + 1` to make `i` equal to n on the last step.

To iterate over a decreasing sequence, we can use an extended form of range() with three arguments - `range(start_value, end_value, step)`. When omitted, the step is implicitly equal to 1. However, can be any non-zero value. The loop always includes start_value and excludes end_value during iteration:

```for i in range(10, 0, -2):
print(i)
# 10
# 8
# 6
# 4
# 2
```

## 2. setting the function print()

By default, the function `print()` prints all its arguments separating them by a space and the puts a newline symbol after it. This behavior can be changed using keyword arguments `sep` (separator) and `end`.

```print(1, 2, 3)
print(4, 5, 6)
print(1, 2, 3, sep=', ', end='. ')
print(4, 5, 6, sep=', ', end='. ')
print()
print(1, 2, 3, sep='', end=' -- ')
print(4, 5, 6, sep=' * ', end='.')
```

### How do you print a number loop in Python?

First, initialize a variable “numbers” with the number of numbers you want to print. Then use for loop and print the values. With the input() function you can take e user input value. Do comment if you have any doubts or suggestions on this Python number topic.

### How do you print a value in a for loop in Python?

Python for loop.
Parameter: ... .
Syntax: for <variable> in range(<number>): ... .
Syntax: for "variable" in range("start_number", "end_number"): ... .
Example: >>> for a in range(2,19,5): print(a) 2 7 12 17 >>>.

### How do you print numbers from 1 to 10 in for loop?

C For Loop: Exercise-1 with Solution.
Pictorial Presentation:.
Sample Solution:.
C Code: #include <stdio.h> void main() { int i; printf("The first 10 natural numbers are:\n"); for (i=1;i<=10;i++) { printf("%d ",i); } printf("\n"); } ... .
Flowchart: ... .
C Programming Code Editor:.

Tải thêm tài liệu liên quan đến bài viết How to print numbers in python using for loop