How do you print 1 to 10 in a list python?

Last update on August 19 2022 21:50:48 [UTC/GMT +8 hours]

Python Basic - 1: Exercise-115 with Solution

Write a Python program to generate and prints a list of numbers from 1 to 10.

Expected output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
['1', '2', '3', '4', '5', '6', '7', '8', '9']

Sample Solution:

Python Code:

nums = range[1,10]
print[list[nums]]
print[list[map[str, nums]]]

Sample Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
['1', '2', '3', '4', '5', '6', '7', '8', '9']

Flowchart:

Python Code Editor:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Write a Python program to print letters from the English alphabet from a-z and A-Z.
Next: Write a Python program to identify nonprime numbers between 1 to 100 [integers]. Print the nonprime numbers.

Python: Tips of the Day

Unknown Arguments Using *arguments:

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc[*arguments]:
 for a in arguments:
   print a
myfunc[a]
myfunc[a,b]
myfunc[a,b,c]

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

Problem Definition

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

Solution

In programming, Loops are used to repeat a block of code until a specific condition is met. The While loop loops through a block of code as long as a specified condition is true.

To Learn more about working of While Loops read:

  • How To Construct While Loops In Python
  • Creating A Guessing Game In Python

Program

i = 1
while[i

Chủ Đề