CodeCap
TRENDING
  • Home
  • PHP
  • PYTHON
  • ANDROID
  • JAVA
  • WORDPRESS
  • ARTICLES
  • TUTORIAL
No Result
View All Result
  • Home
  • PHP
  • PYTHON
  • ANDROID
  • JAVA
  • WORDPRESSNew
  • ARTICLES
  • TUTORIAL
SUBSCRIBE
  • Home
  • PHP
  • PYTHON
  • ANDROID
  • JAVA
  • WORDPRESSNew
  • ARTICLES
  • TUTORIAL
No Result
View All Result
CodeCap
No Result
View All Result
Home PYTHON Programming PYTHON Tutorial

Python For Loop – For i in Range Example

Loops are one of the main control structures in any programming language, and Python is no different.

by asdevil666
November 4, 2021
in PYTHON Programming, PYTHON Tutorial
0
Python For Loop – For i in Range Example
29
SHARES
181
VIEWS
Share on FacebookShare on Twitter

In this article, we will look at a couple of examples using for loops with Python’s range() function.

For Loops in Python

for loops repeat a portion of code for a set of values.

As discussed in Python’s documentation, for loops work slightly differently than they do in languages such as JavaScript or C.

A for loop sets the iterator variable to each value in a provided list, array, or string and repeats the code in the body of the for loop for each value of the iterator variable.

In the example below, we use a for loop to print every number in our array.

# Example for loop
for i in [1, 2, 3, 4]:
    print(i, end=", ") # prints: 1, 2, 3, 4,

We can include more complex logic in the body of a for loop as well. In this example we print the result of a small computation based on the value of our iterator variable.

# More complex example
for i in [1, 3, 5, 7, 9]:
    x = i**2 - (i-1)*(i+1)
    print(x, end=", ") # prints 1, 1, 1, 1, 1, 

When the values in the array for our for loop are sequential, we can use Python’s range() function instead of writing out the contents of our array.

The Range function in Python

The range() function provides a sequence of integers based upon the function’s arguments. Additional information can be found in Python’s documentation for the range() function.

range(stop)
range(start, stop[, step])

The start argument is the first value in the range. If range() is called with only one argument, then Python assumes start = 0.

The stop argument is the upper bound of the range. It is important to realize that this upper value is not included in the range.

In the example below, we have a range starting at the default value of 0 and including integers less than 5.

# Example with one argument
for i in range(5):
    print(i, end=", ") # prints: 0, 1, 2, 3, 4, 

In our next example, we set start = -1 and again include integers less than 5.

# Example with two arguments
for i in range(-1, 5):
    print(i, end=", ") # prints: -1, 0, 1, 2, 3, 4, 

The optional step value controls the increment between the values in the range. By default, step = 1.

In our final example, we use the range of integers from -1 to 5 and set step = 2.

# Example with three arguments
for i in range(-1, 5, 2):
    print(i, end=", ") # prints: -1, 1, 3, 

Summary

In this article, we looked at for loops in Python and the range() function.

for loops repeat a block of code for all of the values in a list, array, string, or range().

We can use a range() to simplify writing a for loop. The stop value of the range() must be specified, but we can also modify the starting value and the step between integers in the range().

Post Views: 708
Share12Tweet7Share3

Related Posts

Python: Check if List is Empty
PYTHON Programming

Python : end parameter in print()

April 18, 2021
Python: Check if List is Empty
PYTHON Programming

TypeError: only integer scalar arrays can be converted to a scalar index

April 18, 2021
Python: Check if List is Empty
PYTHON Programming

Pytest Fixtures Example

April 17, 2021
Python: Check if List is Empty
PYTHON Programming

TypeError: list indices must be integers or slices, not str

April 17, 2021
Python: Check if List is Empty
PYTHON Programming

How to take Input() in Python?

April 17, 2021
Python: Check if List is Empty
PYTHON Programming

TypeError: can only concatenate str (not

April 17, 2021
Next Post
The top 10 programming languages tech employers are seeking right now – Technical.ly

The top 10 programming languages tech employers are seeking right now - Technical.ly

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I agree to the Terms & Conditions and Privacy Policy.

Search

No Result
View All Result

Categories

  • Android Programming
  • Android Tutorial
  • Articles
  • Business Traveller
  • Destinations
  • Extreme Work
  • General News
  • Insider Guides
  • JAVA Programming
  • JAVA Tutorial
  • PHP Error Resolve
  • PHP Programming
  • PYTHON Programming
  • PYTHON Tutorial
  • Tutorial
  • Wordpress

Tags

alert alertbox android android error application array auto autocomplete autocomplete off coding confirm box development error features form form alert function function call inspiration in_array java java error java pages javascript mobile app myql mysqli error news off php php error programming python python programming shortcode software standard
Facebook Twitter LinkedIn Youtube RSS

About Us

Codecap is the best programming for the new generation for learning and development

  • 121 King Street, Melbourne
    VIC 3000, Australia
  • codecap666@gmail.com
  • 888-123-4567

Categories

  • Android Programming
  • Android Tutorial
  • Articles
  • Business Traveller
  • Destinations
  • Extreme Work
  • General News
  • Insider Guides
  • JAVA Programming
  • JAVA Tutorial
  • PHP Error Resolve
  • PHP Programming
  • PYTHON Programming
  • PYTHON Tutorial
  • Tutorial
  • Wordpress
VIDEO: Is There Enough Programming Variability For Week 1 of the 2022 CrossFit Semifinals? – Morning Chalk Up
Tutorial

VIDEO: Is There Enough Programming Variability For Week 1 of the 2022 CrossFit Semifinals? – Morning Chalk Up

by asdevil666
May 21, 2022
0

The stage is set for week 1 of Semifinals now that we see the complete picture of programming and the...

Read more
Football: Ohio State announces times, programming updates for three games – OSU – The Lantern

Football: Ohio State announces times, programming updates for three games – OSU – The Lantern

May 21, 2022
Fox, Tubi Unveil Three New Animated Films, 2022-23 Programming Slate – Cartoon Brew

Fox, Tubi Unveil Three New Animated Films, 2022-23 Programming Slate – Cartoon Brew

May 21, 2022
TelevisaUnivision Reveals Upcoming Programming: Sofia Vergara Animated Series, Selena Quintanilla Tribute Concert, More — Upfronts – Deadline

TelevisaUnivision Reveals Upcoming Programming: Sofia Vergara Animated Series, Selena Quintanilla Tribute Concert, More — Upfronts – Deadline

May 21, 2022
Baltimore City Schools Hosts Event For Summer Programming At Maryland Science Center – CBS Baltimore

Baltimore City Schools Hosts Event For Summer Programming At Maryland Science Center – CBS Baltimore

May 21, 2022

Creation by : AMTICY

No Result
View All Result
  • Home
  • PHP
  • PYTHON
  • ANDROID
  • JAVA
  • WORDPRESS
  • ARTICLES
  • TUTORIAL

Creation by : AMTICY

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh