How to find smallest value with range of floats (Python) [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a math problem where I substitute x with numbers from 1 to 25. I did this using a for loop with x in range(1,26). This prints out a list of floats, as it should. After that, I am supposed to print the smallest number found between this range. I have tried using "min()" but I get an error saying that float object is not iterable. Can someone help me figure out a way to print the smallest value?

Min() takes a list, not a float. You don't need a for loop.
myNums = [1.234, 2.345, 4.543]
print min(myNums)
Otherwise if for your math problem you have to use a loop:
myNums = [1.234, 2.345, 4.543]
min = myNums[0] #initial low
for num in myNums:
if num < min:
min = num
print min

Related

Python--- How to find second lOWEST no in list given -- list has Float Value [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 9 days ago.
Improve this question
How to find second lowest no in list given -- list has Float Value
FOR INT VALUES I HAVE DONE THIS BUT I AM STUCK IN FLOAT VALUE.
I TRIED FOR INT VALUE AND IT completed IN INT FIRST I FIND LOWEST NO REMOVE IT THEN FINDING LOWEST NO AND PRINTING IT succesfully COMPLETED IN INT.
STUCK IN FLOAT
Data_in_list = [1,2,3,4,5,6]
frist_min_num = min(Data_in_list)
print(f"This is your list Frist Min Number : {frist_min_num}")
second_min_num = min(
[item for item in Data_in_list if item != frist_min_num]
)
print(f"This is your list Second Min Number : {second_min_num}")
You can try nested lists:
min_float= min(your_list)
print(min([i for i in your_list if i != min_float]))

Sum of digit of a number using recursion in 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 6 months ago.
Improve this question
Here in this program, I tried to understand but couldn't get completely.
How is this recursive function doing the sum and returning total sum of this? Please explain me in detail?
# Recursive Python3 program to
# find sum of digits of a number
# Function to check sum of
# digit using recursion
def sum_of_digit( n ):
if n < 10:
return n
return (n % 10 + sum_of_digit(n // 10)) # how this is working ?
num = 12345
result = sum_of_digit(num)
print("Sum of digits in",num,"is", result)
The best way to understand a recursive function is to dry run it.
First you need to understand what n % 10 mean is. this means the remainder of a n when divided by 10.
In this case when we divide 12345 by 10 , we get 5 as remainder.
so n % 10 part of code becomes 5.
Now, the second part is n//10 which gives you 1234 that are remaining digits.
Applying the same function again will give you 4 + sum_of_digit(123) and so on.
Even if this do not clear your confusion try, running this code on paper with some small number.

In for loop in python, Why the start point doesn't print when the increment is negative? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
In python,
for x in range(1,3,1):
print(x)
and the answer is:
1
2.
But when we type,
for x in range(1,3,-1):
print(x)
and the answer is:
<Nothing>
why 1 isn't printed in the second code?
Although the increment is false. However the starting integer must be printed?? isn't it??
The starting point is printed only if it is in the range you defined with range. Here, range(1, 3, -1) corresponds to the empty set, since you cannot reach 3 from 1 by adding -1 each time.
You can read more about it on python.org, where r[0] corresponds to the starting point:
A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.

How do I generate the Fibonacci sequence without using a list? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have been asked the following:
Using a while-loop, write a program that generates a Fibonacci
sequence of integers. Your program should ask the user how many
Fibonacci sequence entries to generate and print this quantity of them
to the screen.
I don't know where to begin. Can someone point me in the right direction?
use a variable to hold last value and current value, print the current value, and then update the last value... don't want to write it for you :)
Let's think about this problem a little bit before just giving out the answer:
The fibonacci sequence is of the form
0 1 1 2 3 5 8 13 21 ...
So as you can see your next number is the sum of the previous two numbers so, based on its definition you can tell you need two variables to store the previous two numbers in and a variable to store the sum in. You also need to know when you need to terminate your loop (the number you get from the user).
Looks like someone has already posted it for you...never mind.
Every fibonacci number is generated as the sum of the previous two fibonacci numbers. The first two fibonacci numbers are 0 and 1.
Using the above as the definition, let's start designing your code:
function fibonnacci:
n := ask user how many numbers to output # hint: use raw_input() and int()
if n is 1:
output 0
else if n is 2:
output 0, 1
else:
output 0, 1
lastNumber := 1
twoNumbersAgo := 0
count up from 3 to n:
nextNumber := twoNumbersAgo + lastNumber
output nextNumber
twoNumbersAgo := lastNumber
lastNumber = nextNumber
end function

iterating with python while loops [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.
Is anybody able to guide me through this, without spoiling it for me?
There are two things you can do. The "fast" way (a la the story about the young Gauss) recognizes that
sum(1:N) = N * (N + 1) / 2
But I doubt that is what is asked.
You need to create a loop (look at the for command) over a range (look at the range command), and in each iteration add the current value of the loop variable to the sum (which you initialize to zero before the start of the loop).
There - you should now be OK.
EDIT with a while loop, and still leaving you to do a little bit of work:
mySum = 0
i = 1;
while( <<< put some condition here >>> ):
mySum = mySum + i
<<<<< do something clever with i >>>>>
print <<<<< what do you think you should print here? >>>>>
Note that indentation is important in Python, and the : at the end of the while statement matters

Categories