Python Assignment

Someone think out an assignment to improve my Python skill.

Question: List 10 ways to add from 1 to 10 in Python.

Answer:

# Method 1
num_list = range(10+1)
result = sum(num_list)

# Method 2
num_list = range(10+1)
result = 0
for n in num_list:
	result += n

# Method 3
result = 1 + 2 + 3 + 4 + 5 + 6 +7 + 8 + 9 + 10

# Method 4 (Recursive call)
def calc(num):
	if num == 0:
		return 0
	return num + calc(num-1)

result = calc(10)

# Still thinking for the rest .... will update in future.
# Still thinking for the rest .... will update in future.
# Still thinking for the rest .... will update in future.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s