print strings from list - python

I am intended to construct a list of strings from list of numbers. For example, the given list is
list=[1,2,5,25,6]
Desired output:
['Odd', 'Even', Odd, 'multiples of 5 and odd', 'multiples of 2 and even']
My work so far:
list=[]
for num in numbers:
if num % 2:
list.append('Odd')
if not num % 5:
list.append('multiples of 5 and odd')
else:
if not num % 5:
list.append('multiples of 2 and even')
else:
list.append('even')
print(list)
It printed the list but in a wrong way. I was wondering you if you could review my code. Thanks for your help!

l = []
for num in numbers:
if num % 2 == 0:
# Even numbers
# Every even number is a multiple of 2 except number 0
if num == 0:
l.append('Even')
else:
l.append('Multiples of 2 and even')
else:
# Odd numbers
if num % 5 == 0:
l.append('Multiples of 5 and odd')
else:
l.append('Odd')
print(l)

Related

The code is for returning the outlying even or odd number, everything works except for my numbers are returning with brackets, thanks

example of a problem would be [2,4,6,8,9]
the code should return 9, instead it returns [9]
def find_outlier(integers):
even, odd = 0, 0
outlier = []
for num in integers:
if num % 2 == 0:
even += 1
else:
odd +=1
if even > odd:
for num in integers:
if num % 2 != 0:
outlier.append(num)
return outlier
else:
if odd > even:
for num in integers:
if num % 2 == 0:
outlier.append(num)
return outlier
You are returning the list and printing the list. That's the reason
you can returning the num instead of the outlier list
def find_outlier(integers):
even, odd = 0, 0
outlier = []
for num in integers:
if num % 2 == 0:
even += 1
else:
odd +=1
if even > odd:
for num in integers:
if num % 2 != 0:
outlier.append(num)
return num
else:
if odd > even:
for num in integers:
if num % 2 == 0:
outlier.append(num)
return num
print(find_outlier([2,4,6,8,9]))
But still your code is fully mess. It's not gonna return more than one odd or even. I exactly don't know what you wanted to mean by this code. But this shortened code may help
def CheckEvenOrOdd(integers):
even, odd= 0,0
Evenlist=[]
OddList=[]
for num in integers:
if num % 2 == 0:
Evenlist.append(num)
even += 1
else:
OddList.append(num)
odd +=1
if even > odd:
return OddList
else:
return Evenlist
print(CheckEvenOrOdd([2,4,6,8,9,11]))
It's gonna return list so there is no problem with that

finding odds and evens of user idea

i want to make program that it get a list of numbers from user and then find even numbers and odd numbers and print it.here is my code.please help me.i want my output should be list.this code has error now!!!
def find_evens_and_odds()
numbers=list(input("give me your list:"))
evens=list()
odds=list()
for i in numbers:
if i % 2 ==0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens,odds)
import ast
def find_evens_and_odds():
number=input("give me your list ") # Enter like this [1,2,3]
numbers= ast.literal_eval(number)
evens=[]
odds=[]
for i in numbers:
if int(i) % 2 ==0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens,odds)
find_evens_and_odds()
def find_evens_and_odds(numbers):
evens = list()
odds = list()
for i in numbers:
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens, odds)
numbers = list(map(int,input().split()))
find_evens_and_odds(numbers)

"super" number in python

I have a problem with the task. The task is:
We say that number 1 is a super number. If a number x is super, then
the numbers 2x and 3x are also super. For example, since the number 1
is super, then the numbers 2 and 3 are super. As 2 and 3 are super,
then the numbers 4, 6 and 9 are super, and so on. At the same time,
numbers 10 and 7 are not super. Write a program that asks the user to
enter a natural number n. The program prints whether the entered
number is super.
And this is what I have done so far
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 5 == 0 or num % 7 == 0 or num % 11 == 0 or num % 13 == 0:
print("Number is not super.")
elif num == 1 or num % 2 == 0 or num % 3 == 0 or num % 8 == 0 or num % 9 == 0:
print("Number is super")
else:
print("Number is not super.")
The problem is that for some numbers like 62 it says that it is a super number, but it ain't..
Just following the definition of super number:
def is_super(k):
if k == 1: return True
if k % 2 == 0:
return is_super(k / 2)
if k % 3 == 0:
return is_super(k / 3)
return False
Some testing
print(is_super(9))
True
print(is_super(14))
False
print(is_super(32))
True
print(is_super(62))
False
To me it looks like you jumped in without first figuring out how you'd work it out manually.
Personally, I think the easiest way to start this would be recursively, though it'll be very inefficient with large numbers, so it's up to you if you then want to go and optimise it after doing the first version. I got it working pretty easily, but since it's a problem you need to solve, I'm not going to just copy and paste the answer.
It works something like this (psuedocode):
def is_super(i):
if i is below 1: not a super
if i is 1: is a super
if i is above 1: check if i/2 or i/3 is a super
There are some nice recursive solutions here, I'll propose a non-recursive one. As some of the comments hint, super numbers have a tell-tale factorization: they are all of the form 2x * 3y for x, y >= 0, and every integer of that form is a super number. This should become clear after some study because the only way to get a super number is to multiply an existing one by 2 or 3, and we start with 1. That leads to the following code to test for superness:
def is_super(n: int) -> bool:
if n < 1:
return False
while n % 3 == 0:
n //= 3
return n & (n - 1) == 0; // Suggested by #KellyBundy
I tried to find shortcut way to test for superness but failed to find anything better than this.
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 3 == 0:
while num % 3 == 0:
num = num / 3
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
elif num % 2 == 0:
while num % 2 == 0:
num = num / 2
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
else:
print("Number is not super")
This is what I've done so far but I don't think it will work for large numbers ?

How to use continue in while loop?

I tried to use continue in a while loop to skip print number but it doesn't work.
num = int(input())
while int(num) > 0 :
num-=1
print(num)
if num == 6:
continue
elif num ==1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six
Try this
num = int(input())
while num > 0 :
num -= 1
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
print(num)
Your print line should be after the if-else block
num = int(input())
while int(num) > 0:
num -= 1
# print(num) => if you print here, it does not check the condition
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
# print number here
print(num)
First of all, how do you know it's been 8 numbers? The input can be any number. Secondly, if you want to print every number but 6 you need to move the num -= 1 too. Being there, it won't print the first number.
Try this if you don't insist on using continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num != 6:
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Or this, if you want to test continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num == 6:
num -= 1
continue
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Finally, If you're ok with the num -= 1 there:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
num -= 1
if num == 6:
continue
print(num)
printed_numbers += 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
NOTE: I'm using printed_numbers because if the input is less than 6 you will print all the numbers else you'll have one less. You can use this condition instead.
You are print the number before the condition. chnage the code as follows.
num = int(input())
while int(num) > 0 :
num-=1
if num != 6:
print(num)
elif num == 1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six

Python - listing prime numbers from 1 to 1000 including 1

This program is for listing all the prime numbers between 1 and 1000, but my teacher would like me to include 1 in the results.
I tried to change it to say if num >= 1: and for i in range(1,num), but then when I ran it, the only result was 1 is a prime number!. Thanks!
for num in range(1,1001):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num,"is a prime number!")
You should not write for i in range(1, num):, because (any number) % 1 == 0. if num >= 1: can also be removed, because it's always true.
Try the following code:
for num in range(1, 1001):
for i in range(2, num):
if num % i == 0:
break
else:
print num, 'is a prime number'
And remember, technically speaking, 1 isn't a prime number.
Leave your code as is and above the main for loop add:
print("1 is a prime number")
a = int(input("enter the start number"))
b = int(input("enter the end number"))
for i in range(a,b+1):
if i > 1:
for j in range(2,i):
if i % j == 0:
break
else:
print(i,"is a prime number")
import math
n1 = 1000
run_lim = math.ceil(math.sqrt(n1))
prm_num = [2]
for i in range (1,n1+1) :
if i == 1 :
continue
else :
count = 0
for j in range (len(prm_num)) :
if (prm_num[j] <= run_lim) and (i%prm_num[j]) == 0 :
count += 1
if count == 0 :
prm_num.append(i)
print("The Prime Numbers are :- \n")
print(*prm_num, sep=",")
import gmpy2
c = []
for i in range(1,1000):
if gmpy2.is_prime(i)==True:
c.append(i)
print(c)

Categories

Resources