How to Print Numbers in a List That Are Less Than a Variable. Python

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 as list is also a python type
  • the second while loop causes the issue, as the length of the list is by default equal to aantal 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?
  1. 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.
  2. You were using the same variable names for different tasks such as w for denoting integer less than n and also range in for loop. This should be avoided too!
  3. Idk, why you were using the variable a but there's no need to initialize variables with 0 if you are using as range in for loop.
  4. You were running the for loop till 1 instead should run it 0.
  5. 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 computation
res: Contain the minimum
old_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



Leave a reply



Submit