Assig 2 Soln
Assig 2 Soln
1. Write a python function that computes the square of a number. The function should return the
output as a tuple in the following form: (a, a^2 ) where a is the input value to the function.
In [1]:
def square(x):
return (x,x**2)
In [2]:
Out[2]:
(4, 16)
2. Write a function that can accept two strings as input and concatenate them and then print it as an
output.
In [3]:
In [4]:
Welcome Home!
3. Write a function that can accept an integer number as an input and print "Even number" if the
number is even, otherwise print "Odd number".
In [5]:
def even_odd(x):
if x % 2 == 0:
print("Even number")
else:
print("Odd number")
In [6]:
even_odd(3)
Odd number
In [7]:
even_odd(10)
Even number
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 1/7
12/9/2019 Assignment_II_solution
4. Write a function that can print a dictionary where the keys are numbers between 1 and 20 (both
included) and the values are square of keys.
In [8]:
def print_dict():
d = {}
for i in range(21):
d[i] = i**2
return(d)
In [9]:
print_dict()
Out[9]:
{0: 0,
1: 1,
2: 4,
3: 9,
4: 16,
5: 25,
6: 36,
7: 49,
8: 64,
9: 81,
10: 100,
11: 121,
12: 144,
13: 169,
14: 196,
15: 225,
16: 256,
17: 289,
18: 324,
19: 361,
20: 400}
5. Write a function to compute the square-root of a given number. If the given number is negative,
your function should give an error message.
In [10]:
def square_root(x):
if x >= 0:
return(x**(1/2))
return ("You entered an invalid value!")
In [11]:
square_root(25)
Out[11]:
5.0
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 2/7
12/9/2019 Assignment_II_solution
In [12]:
square_root(-3)
Out[12]:
6. Write a function to input two separate lists, one with names (name_list) and another with numbers
(number_list). Your function should then output another list (output_list) which contains tuples of
names and number.
In [13]:
In [14]:
In [15]:
print(output_list)
7. With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and
the last half values in another line.
In [16]:
mytuple = (1,2,3,4,5,6,7,8,9,10)
midpoint = int(len(mytuple)/2)
print("{}\n{}".format(mytuple[:midpoint],mytuple[midpoint:]))
(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)
8. Write a function that takes in a list of usernames of students. The function should then output
another list which returns the email addresses by adding @thebritishcollege.edu.np” to the
usernames in the list. The output of your function should be a list.
In [17]:
def out_email(usernames):
out_list = []
for n in usernames:
out_list.append(str(n)+"@thebritishcollege.edu.np")
return out_list
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 3/7
12/9/2019 Assignment_II_solution
In [18]:
Out[18]:
['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]']
9. In the list given below, use a while loop to print the output. Your loop should run until all the items
in the list have been printed.
In [19]:
In [20]:
i = 0
while i < len(name_list):
print("Name: ", name_list[i][0])
print("Contact no: ", name_list[i][1])
i = i + 1
10. Write a function that accepts a list of tuples as an input, and outputs the values as a dictionary
shown below:
In [21]:
def tuple_to_dict(inList):
out_d = {}
for i,j in inList:
out_d[i] = j
return out_d
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 4/7
12/9/2019 Assignment_II_solution
In [22]:
tuple_to_dict(input_list)
Out[22]:
11. Write a function to go through a string and count the number of words in the given string.
In [23]:
def count_words(s):
word_list = s.split(" ")
return("number of words: {}".format(len(word_list)))
In [24]:
input_str = "If you count the words in this sentence you will get twelve"
count_words(input_str)
Out[24]:
'number of words: 12'
12. Write a function to print Fibonacci sequence up-to the given term.
In [25]:
def fibonacci(N):
x = 1
y = 1
print (1)
for i in range(N-1):
x,y = y,x+y
print (x)
# test
fibonacci(7)
1
1
2
3
5
8
13
13. Write a program to play a game of rock, paper and scissor between two players. Take two user
inputs from the keyboard and according to user's choice decide who wins.
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 5/7
12/9/2019 Assignment_II_solution
In [27]:
if userA_choice.lower() == userB_choice.lower():
print("It is a tie! Please play again!")
1
22
333
4444
55555
666666
7777777
88888888
999999999
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 6/7
12/9/2019 Assignment_II_solution
In [28]:
num = 1
for i in range(1,10):
for j in range(1,i+1):
print(num, end = " ")
print("\n")
num = num + 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
15. Write a Python function that accepts a hyphen-separated sequence of words as input and prints
the words in a hyphen-separated sequence after sorting them alphabetically.
For example:
In [29]:
def sort_words(word):
wordlist = word.split("-")
return "-".join(sorted(wordlist))
In [30]:
sort_words("s-i-t-c-o-m")
Out[30]:
'c-i-m-o-s-t'
http://localhost:8888/nbconvert/html/Assignment_II_solution.ipynb?download=false 7/7