Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic function python
#1
Hi everyone I'm new to this world and I would appreciate any advice or answer would be better for me if answer would be given in order of numbers something like that (1 2 3 4) Thanks.

def sum_of_numbers(n):
    total = 0
    for i in range(n+1):
        total+=i
    print(total)
sum_of_numbers(10)


def find_even_numbers(n):
      evens = []
      for i in range(n+1):
          if i % 2 == 0:
              evens.append(i)
      return evens
print(find_even_numbers(10))
Reply
#2
(Jan-12-2020, 07:38 PM)sernikov Wrote: I would appreciate any advice or answer would be better for me if answer would be given in order of numbers something like that (1 2 3 4)
And the questions are....?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
For example like here below I think it can be understandable

variable = 'hello people' 1
print(variable) 2
Reply
#4
Please, 1. Use BBcode as advised and 2. Ask clear questions. We won't guess what your questions are.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
is he asking for a basic sorting algorithm?
print(sorted([3,2,4,1]))
Reply
#6
I just wondering what processes is performing in function range(n+1)
and whenever I've calling a function sum_of_numbers(10) output is 55
Reply
#7
I have trouble understanding your questions.

What does range(n+1) do? You can print it out:

>>> n = 10                                                           
>>> list(range(n+1))                                                 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
As function sum_of_numbers sums all integers in range this is equal to:

>>> sum(list(range(n+1)))                                            
55
However, no need to convert into list, you can feed range directly into sum:

>>> sum(range(n+1))                                                  
55
If the objective is to find sum of integers in the range then instead of brute-force approach triangular number could be used:

>>> n * (n+1) // 2         # // returns integer, / returns float                                           
55
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
range() function docs
sum_of_numbers(10) prints (as the name suggests) the sum of all numbers between 0 and 10 (incl)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Thanks perfringo for help now i can continue my journey with python
Reply
#10
(Jan-14-2020, 08:29 AM)sernikov Wrote: Thanks perfringo for help now i can continue my journey with python

You are welcome.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  basic random number generator in replace function krug123 2 2,007 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help in writing a basic function meru120 1 2,170 Apr-27-2018, 09:37 AM
Last Post: Gribouillis
  Basic input() function question rebubula76 2 3,195 Nov-27-2017, 08:12 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020