+ 1

Variable Scope

Im trying to add up all the digits in a number. Im not so sure if my for loop is the most efficient way of doing this. Im creating a new list of the numbers that i converted to an int in another variable and printing out the sum of that list because i cant return the sum of the variable because its only inside the for loop. Is there a better way of doing this? https://code.sololearn.com/cgXECPuAt6S2/?ref=app

21st Jun 2023, 8:56 PM
Junior
Junior - avatar
13 Respostas
+ 3
You could also use the map() function to make the string digits integers: print(sum(map(int, list("123"))))
21st Jun 2023, 9:53 PM
Lisa
Lisa - avatar
+ 3
G B Okay so map requires a function and is applied to each element and then map is returning an iterable and sum is going through that iterable.
22nd Jun 2023, 12:07 PM
Junior
Junior - avatar
+ 2
map() is basically: "Apply this function to each element of my iterable", here int() is applied to each character of the string.
22nd Jun 2023, 1:55 AM
Lisa
Lisa - avatar
+ 2
2 Things to refine. 1. The map function applies the given function to every Element in the given iterable. All the results are then packend into an iterable again, which will be returned. So map itself evaluates to an iterable. 2. In python there actually is no such concept as type casting. Python is not strictly typed, so a variable Can change its type during runtime. What the int function does is, it Creates a new int object and Returns it. So Yes, int() is a function (method) - more specifically the it is the "constructor" method of class int.
22nd Jun 2023, 7:34 AM
G B
G B - avatar
+ 2
G B Thanks
22nd Jun 2023, 5:04 PM
Junior
Junior - avatar
+ 1
Junior exactly.
22nd Jun 2023, 1:56 PM
G B
G B - avatar
0
you do not have to create a new distinct list object, But you could use a list comprehension as in add_digits2: from time import perf_counter import numpy as np def add_digits(digits): store_numbers = list(digits) new_lst = [] for item in store_numbers: conv_number = int(item) new_lst.append(conv_number) return sum(new_lst) def add_digits2(digits): store_numbers = list(digits) return sum([int(num) for num in store_numbers] ) runtime = [] for _ in range(1000): t0 = perf_counter() result = add_digits("123") runtime.append(perf_counter() - t0) print(f" {result}, {np.mean(runtime)}") runtime = [] for _ in range(1000): t0 = perf_counter() result = add_digits2("123") runtime.append(perf_counter() - t0) print(f" {result}, {np.mean(runtime)}") This may only be a tiny bit more efficient though...
21st Jun 2023, 9:54 PM
G B
G B - avatar
0
Lisa : true, this makes it much more effective 👍
21st Jun 2023, 10:03 PM
G B
G B - avatar
0
Lisa Thanks, i will definently see more about map
21st Jun 2023, 11:55 PM
Junior
Junior - avatar
0
Lisa I do have one question. okay so maps require a function right, so correct me if im wrong but int isnt a function. Is it converting the number to an ingeter like type casting?
22nd Jun 2023, 12:00 AM
Junior
Junior - avatar
0
Oh wait im getting ahead of myself, okay so I got rid of list because map take iterables and strings are iterables but then im casting it as an int and ints are not iterables? So what is exactly happeninng here?
22nd Jun 2023, 12:42 AM
Junior
Junior - avatar
0
Lisa So how is my sum working then because its an int, sun only takes iterables and integers are iterablw unless you put it in like a list or something
22nd Jun 2023, 2:03 AM
Junior
Junior - avatar
0
Lisa So is map like how list work? it stores that number in it? and go goes through it amd finds the sum?
22nd Jun 2023, 3:52 AM
Junior
Junior - avatar