473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

syntax question

Expand|Select|Wrap|Line Numbers
  1. def sumTo(n):
  2.     sum = 0
  3.      for i in range(1, n+1):
  4.        sum = sum + i
  5. return sum
  6.  
hi with the code above can someone please walk me through what is actually happening with n is equal to say 10 and the output is 55?

many thanks!
Apr 7 '13 #1
1 1199
bvdet
2,851 Expert Mod 2GB
To begin, you need to fix your indentation:
Expand|Select|Wrap|Line Numbers
  1. def sumTo(n):
  2.     sum = 0
  3.     for i in range(1, n+1):
  4.         sum = sum + i
  5.     return sum
The code iterates over the list created by calling range() (1, 2,....,10) and adds all the numbers to the initial value of "sum" which is 0. BTW, you should not use the name "sum" as an identifier, because the built-in function sum() will be masked. Another way to express your function would be:
Expand|Select|Wrap|Line Numbers
  1. reduce(lambda x, y: x+y, range(1, n+1), 0)
or:
Expand|Select|Wrap|Line Numbers
  1. >>> import operator
  2. >>> reduce(operator.add, range(1, n+1))
  3. 55
  4. >>> 
Apr 7 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
3
by: Mike | last post by:
I have done this before in VB but I am realitively new to C#. I have a datagrid where the first bound column represents the identity column from the table. I have designated that first bound column...
2
by: Colin McGuire | last post by:
Sorry to ask a simple question on syntax but the line inside the subroutine "assignBitmapArray" won't compile giving me an "Expression Expected" error. What is the correct syntax to initialize an...
5
by: pwsmeister | last post by:
I'm fairly new to javascript, my take on the below code is that it is creating a array called event and populating it with 3 functions( add, remove and DOMit). Is my take on this code correct? ...
11
by: deppy_3 | last post by:
Hi! The syntax of fputs() is similar with the syntax of fgets(); For example if we have:fgets(str,maxlen,stdin) which is the syntax of the fputs();
12
by: Yusuf | last post by:
I'm sorry for the second post in as many hours, and both about structs. The code below compiles. The variables test1 and test2 are structures of datatype teststruct1 and teststruct2. The size of...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
11
by: Frankie | last post by:
I have been learning about the AsyncOperation class and came across this code (sorry I got it a few days ago and don't remember the specific link). Anyway, I am wondering what the tilde (~) on...
2
by: movieking81 | last post by:
Hello All, I have a simply syntax question. I'm new to PHP, but I have worked with ASP for years. Can you reference a database field on an html page with PHP the same way you can with ASP. For...
6
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.