For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (2024)

# Table of Contents

  1. Using a For loop to print the numbers from 1 to 10
  2. Using a While loop to print the numbers from 1 to 10
  3. Using a For loop to print the numbers from 10 to 1
  4. Using a While loop to print the numbers from 10 to 1

# Using a For loop to print the numbers from 1 to 10

Use the range() class to loop from 1 to 10 in a for loop, e.g.for num in range(1, 11):.

The range class takes start (inclusive) and stop (exclusive) argumentsand enables us to loop a specific number of times in for loops.

main.py

Copied!

# ✅ `for` loop 1 to 10 (including 10)for num in range(1, 11): print(num)# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(list(range(1, 11)))

For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (1)

The code for this article is available on GitHub

If you need to exclude 10 from the range, use the following code sample instead.

main.py

Copied!

# ✅ `for` loop 1 to 10 (excluding 10)for num in range(1, 10): print(num)# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9]print(list(range(1, 10)))

We used the range() class to loop from 1 to 10 in afor loop.

Note that the first parameter of the range() class (the start value) is inclusive, whereas the stop value is exclusive.

The range() class iscommonly used for looping a specific number of times in for loops and takesthe following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it isconsidered to be the value for the stop parameter.

main.py

Copied!

for num in range(10): print(num)# 👇️ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]print(list(range(10)))

Note that the range() class returns a range object, not a list.

main.py

Copied!

# 👇️ range(0, 10)print(range(10))# 👇️ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]print(list(range(10)))

If you need to convert the range object to a list, pass it to thelist() class.

# The stop value is exclusive (up to, but not including)

If values for the start and stop parameters are provided, the start valueis inclusive, whereas the stop value is exclusive.

main.py

Copied!

# 👇️ for loop 1 to 10 (including 10)for num in range(1, 11): print(num)# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(list(range(1, 11)))

The code for this article is available on GitHub

If you need to exclude 10 from the range that is being iterated, use a stopvalue of 10.

main.py

Copied!

for num in range(1, 10): print(num)# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9]print(list(range(1, 10)))

# Using a While loop to print the numbers from 1 to 10

To print the numbers from 1 to 10 in a while loop:

  1. Declare a new variable and initialize it to 1.
  2. Use a while loop to iterate for as long as the variable is less than or equalto 10.
  3. Increment the variable by 1 on each iteration.

main.py

Copied!

number = 1while number <= 10: print(number) number += 1

For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (2)

The code for this article is available on GitHub

We declared a new variable and initialized it to 1.

The while loop iterates for as long as the number variable is less than orequal to 10.

On each iteration, we print the current value and increment the variable by 1.

Once the number variable is equal to 11, the condition is no longer met andwe exit the while loop.

# Using a while True loop to print the numbers from 1 to 10

You can also use a while True loop to print the numbers from 1 to 10 inPython.

main.py

Copied!

number = 1while True: if number > 10: break print(number) number += 1

For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (3)

The code for this article is available on GitHub

The while True loop iterates until the break statement is used.

We initialized the number variable to 1 just like we did in the previousexample.

On each iteration of the while loop, we check if the number variable isgreater than 10.

If the condition is met, we use the break statement to exit the while loop.

The break statement breaks out of theinnermost enclosing for or while loop.

If the condition isn't met, the number variable is in the specified range (1to 10), so we print its value and increment it by 1.

# Using a For loop to print the numbers from 10 to 1

To print the numbers from 10 to 1 using a for loop:

  1. Use the range class to get a range of the numbers from 1 to 10.
  2. Use the reversed() function to reverse the range.
  3. Use a for loop to iterate over the range from 10 to 1.

main.py

Copied!

for num in reversed(range(1, 11)): print(num)# 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]print(list(reversed(range(1, 11))))

For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (4)

The code for this article is available on GitHub

We used the range class to get a range object containing the numbers from1 to 10.

main.py

Copied!

# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(list(range(1, 11)))

The next step is to use the reversed() function to reverse the range.

main.py

Copied!

# 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]print(list(reversed(range(1, 11))))

The reversed function takes aniterator, reverses it and returns the result.

Once the range is reversed, we can use a for loop to iterate and print thenumbers from 10 to 1.

# Using a While loop to print the numbers from 10 to 1

To print the numbers from 10 to 1 using a while loop:

  1. Declare a new variable and initialize it to 10.
  2. Use a while loop to iterate for as long as the variable's value is greaterthan or equal to 1.
  3. Print the value of the variable and decrement it by 1.

main.py

Copied!

number = 10while number >= 1: print(number) number -= 1

For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (5)

The code for this article is available on GitHub

The number variable is initially set to 10.

On each iteration of the while loop, we print the current value of thevariable and decrement it by 1.

Once the number variable is set to 0, the condition is no longer met and weexit the while loop.

# Using a While True loop to print the numbers from 10 to 1

You can also use a while True loop to print the numbers from 10 to 1.

main.py

Copied!

number = 10while True: if number < 1: break print(number) number -= 1

The code for this article is available on GitHub

The while True loop iterates until the break statement is used.

On each iteration, we check if the number variable is less than 1.

If the condition is met, we use the break statement to exit the loop.

Otherwise, we print the variable and decrement its value by 1.

Once the number variable is set to 0, the condition is no longer met and weexit the while loop.

# Additional Resources

You can learn more about the related topics by checking out the followingtutorials:

  • Using multiple variables in a For loop in Python
  • Using a For or While Loop to take user input in Python
  • Detect the Last item in a List using a for loop in Python
  • How to restart a Loop in Python
  • Python: How to calculate the MD5 Hash of a File
  • python.exe: can't find __main__ module in Path
  • How to exit an if statement in Python [5 Ways]
  • -215:Assertion failed !_src.empty() in function 'cvtColor'
For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz (2024)
Top Articles
Jeffrey Dahmer And Other Serial Killers Have These 5 Traits In Common
30 Bone-Chilling Facts About Jeffrey Dahmer - The Fact Site
SZA: Weinen und töten und alles dazwischen
Boomerang Media Group: Quality Media Solutions
Kansas Craigslist Free Stuff
Is Csl Plasma Open On 4Th Of July
Notary Ups Hours
Ribbit Woodbine
Hello Alice Business Credit Card Limit Hard Pull
Large storage units
World Cup Soccer Wiki
Zendaya Boob Job
Hillside Funeral Home Washington Nc Obituaries
Buying risk?
Becu Turbotax Discount Code
Epro Warrant Search
Navy Female Prt Standards 30 34
Military life insurance and survivor benefits | USAGov
Horn Rank
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Chicago Based Pizza Chain Familiarly
Dr. Nicole Arcy Dvm Married To Husband
O'reilly's In Monroe Georgia
Craigslist Auburn Al
*!Good Night (2024) 𝙵ull𝙼ovie Downl𝚘ad Fr𝚎e 1080𝚙, 720𝚙, 480𝚙 H𝙳 HI𝙽DI Dub𝚋ed Fil𝙼yz𝚒lla Isaidub
Greater Orangeburg
Walter King Tut Johnson Sentenced
Adecco Check Stubs
Sitting Human Silhouette Demonologist
Kips Sunshine Kwik Lube
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Metro By T Mobile Sign In
Game8 Silver Wolf
Alpha Asher Chapter 130
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Karen Wilson Facebook
LoL Lore: Die Story von Caitlyn, dem Sheriff von Piltover
Charli D'amelio Bj
Best Conjuration Spell In Skyrim
Bmp 202 Blue Round Pill
Hampton In And Suites Near Me
Waco.craigslist
10 Best Tips To Implement Successful App Store Optimization in 2024
Is TinyZone TV Safe?
Wwba Baseball
Arnold Swansinger Family
Chitterlings (Chitlins)
Craigslist.raleigh
Acellus Grading Scale
Www.card-Data.com/Comerica Prepaid Balance
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5961

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.