How to exclude a number from a range in python? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new to coding, and I wrote a program using a for loop and a range to check if a number from that range is odd or even. However I want to exclude the number 0
for num in range (0, 11):
if num %2 == 0:
print (num,' is an even number!')
else:
print (num,' is an odd number!')
I expected the output of all the numbers but not the 0.
could someone help me?

As answers have already mentioned, range(1, 11) will produce 1 2 3 4 5 6 7 8 9 10 as requested. However you can exclude specific numbers by doing:
all_numbers = range(0, 11)
exclude_set = {0} # a set containing the value 0
numbers = (num for num in all_numbers if num not in exclude_set)
This could be useful if you have a set of numbers that you know are wrong, but otherwise have a contiguous range.
"""Represent retail stores in a chain"""
store_numbers = range(1, 51)
# stores 14, 17, and 32 have been closed, so
closed_stores = {14, 17, 32}
valid_stores = (store for store in store_numbers if store not in closed_stores)

If you want to exclude 0 then change your range to (1,11). The way range works is the lower limit is inclusive where as upper limit is exclusive.
Docs to read more about range: https://docs.python.org/3/library/functions.html#func-range
On an unrelated note, if your lower limit is 0, you need not include the lower limit and write it as range(11) which is same as range(0,11).

Range is inclusive of the first parameter, but does not include the second parameter. The first number in range should be the number you want to start with.
Change your code to
for num in range(1, 11):
if num % 2 == 0:
print(num, ' is an even number!')
else:
print(num, ' is an odd number!')

Related

Indexerror:::: list index out of range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi this is my code and I don't know why I received this type of error
x = int(input())
n = [int(i) for i in input().split()]
middle = n[int((x - 1) / 2)
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
It produces an error as such IndexError: list index out of range because n has the minimum number of list values entered by the user.
Since your intention was for n to have more values than the minimum number of digits entered by the user, I added .range() to the list iteration.
Here is the modified code:
x = int(input("Put in a number: "))
n = [int(i) for i in range(int(input("Put in another number: ")))]
middle = n[int((x - 1) / 2)]
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
If you have any questions or need clarification, please do not hesitate to ask.

Why we use count variable & and how its work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
#program of prime number from 2 to 100
for i in range (2,100):
count = 0
for j in range(1, i+1):
if i % j = 0:
count = count + 1
if count == 2:
print(i)
First of all, your code seems to not work due to a syntax error(might be a typo).
if conditions in python, requires double equals (==).
if i % j == 0:
In the question you posted, the count variable acts as a flag or counter variable.
A prime number is a number which can only be perfectly divided, by 1 or the number itself. (eg: 11 can be divided by 1 and 11 only, therefore a prime number)
So the counter variable i.e. count holds the number of times that a number(2-200) gets perfectly divided(i.e. (%) modulo operation results in value 0). If the value of count is 2 then we can confirm that it is a prime number since a prime number can only be divided with 1 and the number itself (hence count=2). And if the count == 2 after all the possible divisions happening in the j loop (2nd for loop), we can conclude that it is a prime number.
for i in range (2,200):
count = 0
for j in range(1, i+1):
if i % j == 0:
count += 1
if count == 2:
print(i)
count variable is being used to ensure if the particular number i is being divisible by any other number other than 1. Because if it does, then it is not a prime number. The moment it becomes 2, it means that number is divisible by 1 and some other number less than i proving it be a non-prime number.
Run this to get primes from 100, you can change the 100 to whatever you want
for num in range(100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")

Count simple numbers in diapasone python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)

I do not understand foor loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Why inside for loop is written upper + 1 and not just upper. If I change upper+1 with upper and run the code, I will have the same result!
for num in range(lower,**upper + 1)**
# Python program to display all the prime numbers within an interval
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
It's because range(x,y) stops at y-1. For example:
for i in range(2,5):
print(i)
will give you:
2
3
4
So if you want to check upper as well, you should add 1, so upper+1. Your code returns the same results since upper is not prime, so even if upper is checked (with upper+1 in range), upper will not be printed.

Replacing numbers ending in 3 and 7 in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Write a program that generates and prints a list of n elements (n informed by the user) containing the natural numbers (starting with 1) and replacing multiples of 3 by the word 'ping', multiples of 7 by the word 'pong', and multiples of 3 and 7 by the word 'ping-pong'
Here is the code for that
result = []
number = eval(input("Enter a whole number: "))
for index in range(number):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0:
result.append("ping")
elif index % 7 == 0:
result.append("pong")
else:
result.append(index)
print(result) == 0
Now also replaces numbers ending in 3 by the word ‘PING’ and numbers ending in 7 by the word ‘PONG’ this I am not sure how to go about doing.
I tried to make your code do what you want while doing as few modifications to it as possible.
Do NOT use eval. Ever. Bad, bad, bad eval. To cast an string to an int, use int().
Your code was starting at 0 when it was asked that it started at 1, I
changed the range.
To know the last digit, I calculated the number modulo 10, based on the clever comment by #Renuka Deshmukh. Other less clever solutions could have been to check the end of the number casted as a string, with str(index).endswith("7") or str(index)[-1] == "7", for example.
What was your print(result) == 0 trying to do? I removed the ==0.
Here is the resulting code:
result = []
number = int(input("Enter a whole number: "))
for index in range(1,number+1):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0 or index % 10 == 3:
result.append("ping")
elif index % 7 == 0 or index % 10 == 7:
result.append("pong")
else:
result.append(index)
print(result)

Categories