# Question about the 'for' statement...

Page 1 of 1

## 4 Replies - 830 Views - Last Post: 15 November 2012 - 09:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300199&amp;s=6894bb2d7a705b709a8907d09840f689&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ricardo49

Reputation: 0
• Posts: 12
• Joined: 02-November 12

# Question about the 'for' statement...

Posted 14 November 2012 - 11:27 AM

This piece of code will print the numbers 7 through 98:

```for i in range(7,99):
print(i)
```

Suppose I just want it to print every other number (that is, just the odd numbers) starting with 7. How would the code be changed?
Is This A Good Question/Topic? 0

## Replies To: Question about the 'for' statement...

### #2 jon.kiparsky

• Beginner

Reputation: 11071
• Posts: 18,909
• Joined: 19-March 11

## Re: Question about the 'for' statement...

Posted 14 November 2012 - 11:30 AM

Sniff, sniff... is that homework I smell?

Try the documentation

Quote:

Quote

range(stop)
range(start, stop[, step])
This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised).

### #3 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: Question about the 'for' statement...

Posted 14 November 2012 - 12:23 PM

You could read up the documentation or just do it yourself with an if-statement:

```for number in iterable:
if number is not something:
print that number

```

### #4 ricardo49

Reputation: 0
• Posts: 12
• Joined: 02-November 12

## Re: Question about the 'for' statement...

Posted 15 November 2012 - 09:28 AM

jon.kiparsky, on 14 November 2012 - 11:30 AM, said:

Sniff, sniff... is that homework I smell? />

Try the documentation

Quote:

Quote

range(stop)
range(start, stop[, step])
This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised).

Thanks for directing me to the documentation. With that info, I was able to figure out how to answer my question.

Although new to Python, I am not currently enrolled in any sort of class (with homework). I am doing a really nice tutorial at cscircles.cemc.uwaterloo.ca where I’m on Chapter 11 of 18 chapters. After completing the tutorial, I’m thinking of taking “Introduction to Computer Science (CS101) Building a Search Engine” at Udacity the next time it’s offered. It’s been reported that more than 200,000 students have enrolled in that course, so I imagine it will be offered again. I seem to do better in language courses if I’ve been exposed to the language before I begin the course, even if the course is said to be for “beginners.” (This holds true for human languages like Russian as well as computer languages like Python.)

I need to learn how to read documentation. It’s written in code, too…like range(start, stop [,step]). You have to know not to use the [] in your code with range, even though you do use them in code when dealing with substrings.

Again, I appreciate the help.

By the way, my question about range had to do with a program I want to write to generate a list of prime numbers, where you divide a given number by every odd number up to a certain limit.

ricardo49

### #5 jon.kiparsky

• Beginner

Reputation: 11071
• Posts: 18,909
• Joined: 19-March 11

## Re: Question about the 'for' statement...

Posted 15 November 2012 - 09:34 AM

The MIT Open courseware material from Eric Guttag is quite good as well.

for prime generation, a list of odd numbers is good, but why not use a list of primes?

That is, start with an empty list and see if anything on it divides two evenly. It doesn't, so add two to the list. Then see if anything on the list divides 3 evenly. It doesn't, so add it to the list. Then check four - well, two divides four, so don't add it. And so forth.
No need to check to see if 9 divides 17, when you've already found out that 3 doesn't.