1. Generating Numbers

Create a function generateNumbers(num) that takes in a positive number as argument and returns a list of number from 0 to that number inclusive. Note: The function range(5) will return a list of number [0, 1, 2, 3, 4].

def generateNumber(num): new=[] num1=num+1 for i in range(num1): new.append(i) return new

2. Generating Numbers II

Create a function generateNumbers(start, end) that takes in two numbers as arguments and returns a list of numbers starting from start to the end number (inclusive) specified in the arguments. Note: The function range(x, y) can takes in 2 arguments. For example, range(1, 5) will return a list of numbers [1,2,3,4].

def generateNumber(start, end): new=[] end1=end+1 for i in range(start,end1): new.append(i) return new

3.Generating Numbers III

Create a function generateNumbers(start, end, step) that takes in three numbers as arguments and returns a list of numbers ranging from start to the end number (inclusive)and skipping numbers based on the step specified in the arguments. Note: The function range(x, y, z) can takes in 3 arguments. For example, range(1, 11, 2) will return a list of numbers [1,3,5,7,9].

def generateNumber(num1,num2,num3): if num3<0: num4=num2-1 else: num4=num2+1 new=[] for i in range(num1,num4,num3): new.append(i) return new

4. Adding A Range Of Numbers

Create a function addNumbers(num) that takes in a positive number as argument and returns the sum of all the number between 0 and that number (inclusive).

def addNumbers(num): num1=num+1 add=0 for i in range(num1): add+=i return add

5. Adding A Range Of Numbers II

Create a function addNumbers(start, end) that takes in two positive numbers as arguments and returns the sum of all the number between the start and end number (inclusive).

def addNumbers(num1,num2): num3=num2+1 add=0 for i in range(num1,num3): add+=i return add

6. Adding Even Numbers

Create a function addEvenNumbers(start, end) that takes in two positive numbers as arguments and returns the sum of all the even numbers between the start and end number (inclusive). Note: x % 2 returns 0 if x is an even number.

def addEvenNumbers(num1,num2): if num1%2==0: num11=num1 else: num11=num1+1 add=0 for i in range(num11,num2+1,2): add+=i return add

7. Using ‘Continue’ To Skip Code Within A Loop

Making use of the ‘continue’ statement to skip the rest of execution within a loop block for current iteration, and move to the next iteration.

# Insert 'continue' in following code so that it returns a # string with no vowels. def skipVowels(word): novowels = '' for ch in word: if ch.lower() in 'aeiou':continue novowels+=ch return novowels

8. Using ‘Break’ To Abort A For/While Loop

Making use of the ‘break’ statement to abort a for/while loop, and move to the code after the loop block .

# insert 'break' in following code so that the comment # after '#' (including '#') are stripped. def stripComment(sentence): codeonly = "" for ch in sentence: if ch == '#':break codeonly+=ch return codeonly

9. Create Set From A List

Create a function genSet() that takes in a list of numbers and returns a sorted set.

def genSet(nums): numbers=set(nums) return sorted(numbers)

10. Using ‘For’ To Traverse String.[MCQ]

Different ways of traversing strings using ‘for’ loop

Question:

[MCQ] Which of the following ‘for’ loops does not give the same output?

** greetings = ‘Hello World’

for x in greetings:

print x,

** greetings = ‘Hello World’

for x in range(len(greetings)):

print greetings[x],

** **greetings = ‘Hello World’
for x in greetings:
print greetings[x], **

** greetings = ‘Hello World’

for x in range(len(greetings), 0, -1):

print greetings[-x],

** None of the above.

11. Sum Of Digits

Write a function sumOfDigit(number) that takes in a number as argument and returns the sum of the individual digit in the number.

def sumOfDigit(num): num1=str(num) a=len(num1) i=0 add=0 for i in range(a): add+=int(num1[i]) return add

#Thank you very much. Another option of code

def generateNumber(num):

L=[ ]

for i in range(num+1):

L.append(i)

i += 1

return L