How to print numbers in a list that are less than a variable. Python
random_num=random.randint(0,10)
list = []
list.extend(range(random_num))
This should do the trick.
range(random_num)
is an iterator of all numbers smaller than random_num.
list(range(random_num))
is a list of all numbers smaller than random_num.
You can use the extend method to impart 'list' with the elements of the iterator.
Trying to show all numbers lower than the average in another list
2 things:
- don't name a variable
list
aslist
is also a python type - the second
while
loop causes the issue, as the length of the list is by default equal toaantal
as the list has already been created. So whatever is in the while loop is never executed. Rather you could just iterate over the list in a simple for loop
That makes:
from random import randint
kleiner = []
list_ = []
teller = 0
aantal = int(input("Hoeveel random getallen wilt u? "))
veelvoud = int(input("Welke veelvoud? "))
while len(list_) < aantal:
getal = randint(1, 100)
if getal % veelvoud == 0:
list_.append(getal)
gemiddelde = sum(list_) / len(list_)
for i in list_:
if i < gemiddelde:
kleiner.append(i)
print(list_)
print(list_[::-1])
print(kleiner)
Printing final list in variable
I guess this is what you are trying to achieve
def properArray(pivot_index, A):
pivot = A[pivot_index]
smaller = []
for i in range(len(A)):
if A[i] < pivot:
smaller.append(A[i])
print (smaller)
resized_array =properArray
resized_array(3, [1,5,6,9,3,4,6])
Instead of printing smaller
array on every iteration, you need to print it once, after the for loop is complete
Print all perfect numbers less than N
Here's how you can do it:
n = input('Enter the integer:')
w = int(n) - 1
l = [] # creating an empty list 'l'
for i in range(w, 0, -1): # Runs the loop for all integers below n and greater than 0.
s = 0 # Set the sum to 0.
for j in range(i,0, -1): # Runs the loop till 0 to check for all divisors
if i % j == 0 and j!= i: # If the number is a divisor of 'i' and not equal to the 'i' itself.
s = s + j # Then add the divisor to the sum
if s == i: # If sum is equal to the number 'i'
l.append(i) # Then add the number 'i' to the list 'l'
print(l)
Output:
Enter the integer:10000
[8128, 496, 28, 6]
What you were doing wrong?- Naming a list by a Python keyword is a bad practice and should be avoided! You named the list by the name
list
which is a keyword in Python. - You were using the same variable names for different tasks such as
w
for denoting integer less thann
and also range in for loop. This should be avoided too! - Idk, why you were using the variable
a
but there's no need to initialize variables with0
if you are using asrange in for loop
. - You were running the for loop till
1
instead should run it0
. - You were not setting the sum to zero at every integer.
How to print out a number if it is less than the previous number in a loop?
Below the code that you want.
I clean the code that you gave and did some modification.
The main problem of your infinite loop was your condition time > 0
(it was always true).
import random
from math import sqrt
# We choose arbitrarily value bigger than 7
res = 10
old_res = 11
cur_res = 10
accuracy = 0.01
while old_res-res > accuracy:
f1 = random.random()
f2 = random.random()
f3 = random.random()
x = 15*(f1/(f1+f2+f3))
y = 15*(f2/(f1+f2+f3))
z = 15*(f3/(f1+f2+f3))
cur_res = ((sqrt(8**2 + (x**2)))/3) + (y/5) + ((sqrt(6**2 + (z**2)))/4)
if cur_res < res:
old_res = res
res = cur_res
print (res)
I've renamed time by res which is less confused, and I managed three variable for res.cur_res
: Contain the current of the computationres
: Contain the minimumold_res
: Contain the last best result after res
.
If you want to find the best result close to 6, you can modify the accuracy
. (it's just old_res - res
).
Increase the accuracy to have the better result.
For each numbers list or (tuple) from Lists or (Tuples) get a difference from this main one list and return or store all those Lists to variable
Here is one way:
main_set = set(main_list)
res = [main_set.symmetric_difference(i) for i in all_list_of_numbers]
Output:
[{6, 7, 8, 9, 10},
{4, 6, 7, 8, 10},
{1, 3, 5, 6, 9},
{1, 2, 4, 7, 9},
{1, 2, 3, 7, 10},
{2, 4, 5, 8, 9},
{1, 4, 5, 7, 9},
{1, 2, 3, 4, 6},
{3, 5, 7, 8, 10},
{4, 5, 6, 7, 10},
{2, 3, 4, 7, 10},
{1, 6, 8, 9, 10},
{2, 4, 6, 8, 9},
{1, 3, 7, 9, 10},
{1, 2, 4, 6, 7},
{1, 5, 6, 8, 10},
{1, 2, 5, 6, 8},
{1, 2, 5, 9, 10},
{1, 2, 3, 4, 9},
{2, 3, 5, 8, 10}]
Related Topics
How to Smooth a Curve in the Right Way
How to Extract Data from Dictionary in the List
How to Use and Print the Pandas Dataframe Name
How to Extract All Upper from a String - Python
How to Hide Tkinter Python Gui
Stripping Non Printable Characters from a String in Python
How to Extract Address from Raw Text Using Nltk in Python
Getting the Bounding Box of the Recognized Words Using Python-Tesseract
Unable Log in to the Django Admin Page With a Valid Username and Password
Python Replace Elements in Array At Certain Range
Wait Until a Certain Process (Knowing the "Pid") End
Typeerror: Unsupported Operand Type(S) for ** or Pow(): 'List' and 'Int'