Python Graphics Worksheet With Answers
Python Graphics Worksheet With Answers
Python Graphics Worksheet With Answers
CSE
sellDict={"seid":[10000,0.05,5000], "chala":[2000,0.05,5000],
"abenezer":[4000,0.05,5000],"abel":[20000,0.05,5000],
"kebede":[8000,0.05,5000],"mohammed":[5000,0.05,5000]}
list1=[]
for key in sellDict:
list1=sellDict[key] # assigns the value mapped to each key to list1
for i in range (len(list1)): # get the element of the list
salary=int(list1[2]+list1[0]*list1[1]) # calculate the salary
sellDict[key].append(salary) # append the salary to the end of the list which is the
#value mapped to the current key
print sellDict
Seid 5500
chala 5100
abenezer 5200
abel 6000
kebede 5400
mohammed 5250
sellDict={"seid":[10000,0.05,5000], "chala":[2000,0.05,5000],
"abenezer":[4000,0.05,5000],"abel":[20000,0.05,5000],
"kebede":[8000,0.05,5000],"mohammed":[5000,0.05,5000]}
list1=[]
for key in sellDict:
list1=sellDict[key]
for i in range (len(list1)):
salary=int(list1[2]+list1[0]*list1[1])
sellDict[key].append(salary)
"kebede":[8000,0.05,5000],"mohammed":[5000,0.05,5000]}
List2=[]
list3=[]
key=None
list1=[]
for key in sellDict:
list1=sellDict[key]
for i in range (len(list1)):
salary=int(list1[2]+list1[0]*list1[1])
sellDict[key].append(salary)
List2=[]
list3=[]
key=None
list1=[]
for key in sellDict:
list1=sellDict[key]
for i in range (len(list1)):
salary=int(list1[2]+list1[0]*list1[1])
sellDict[key].append(salary)
3. Write a python program that read the following word in reverse order
Name=”I love python” after reverse name=”nohtyp evol I”
from __future__ import print_function
String= input("Input a word to reverse: ")
for char in range(len(String) - 1, -1, -1):
print(String[char], end="") print("\n")
In the function definition, parameters b, c are assigned a default value, b is assigned 2 and c
is assigned 4. So, when the function square_root() is called with just one argument, that
argument will be assigned to parameter a.
Square_root(2) # since the square_root() function is called with one argument, parameter
a is mapped to 2, parameter b and are mapped to the default values(b=2, c=4).
So the answer is 4.0
square_root (2,8) # since the square_root() function is called with two arguments,
parameter a is mapped to 2, parameter b is mapped to 8 and c is mapped to the default
values( c=4).
So the answer is 8.0
square_root (1,2,2) ) # since the square_root() function is called with three arguments,
each parameter , i.e., is mapped to respective argument, parameter a is mapped to 1,
parameter b is mapped to 2 and c is mapped to 2.
So the answer is 2.0
calc() # The output will be: 1*2, 5*2, 9*2, 13*2, 17*2 which is
2
10
18
26
34
7. Write a Python program to get a string made of the first 2 and the last 2 chars from a given
a string. If the string length is less than 2, return instead of the empty string.
Sample String : 'university'
Expected Result : 'unty'
Sample String : 'un'
Expected Result : 'unun'
def print_first2Last2(String):
if len(String)<2:
return
else:
str1=String[0:2]
str2=String[len(String)-2:len(String)]
str3=str1+str2
return str3
print print_first2Last2("university")# prints unty
print print_first2Last2("un")# prints unun
print print_first2Last2("u") # prints None
8. Write a Python program to get a string from a given string where all occurrences of its first
char have been changed to '$', except the first char itself.
def change_char(str1):
char = str1[0] # gets the first character of a given string.
str1 = str1.replace(char, '$') # replace first char if appeared
again in the given string. For example,
“communicationcommunication”, char 1 is ‘c’, all c’s except
first will be replaced with ‘$’ like
“communi$ation$ommuni$ation”
str1 = char + str1[1:]
return str1
print(change_char(' communicationcommunication '))
# output is “communi$ation$ommuni$ation”
9. Write a Python function that takes a list of words and returns the length of the longest one.
mylist=[“hello”,”world”,”Introduction”,”computing”]
longest=len(mylist[0]) # initialize longest as the first element of the list. In-built function
len() is used to find a length of the string. Compare it with the remaining
element in the list.
for i in mylist:
if len(i)>longest:
longest=i
print longest
10. Write a Python program to get the largest number from a list.
mylist=[10,20,30,15,45,25,13,100,150,5]
largest=mylist[0] # initialize the first list element as the largest element and go through
the list to find the largest element through comparing
it with the remaining elements in the list.
for i in mylist:
if i>largest:
largest=i
print largest
11. Write a Python program to get a list, sorted in increasing order by the last element in each
tuple from a given list of non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
mylist=[(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)] # sample list
def last_element(mylist):
return mylist[-1] # to get the last value of each
#tuple in the list as element.
def sortList_bylast(tuples):
return sorted(tuples, key=last_element)
print(sortList_bylast(mylist))
for x in mylist:
if x not in dup_items:
uniq_items.append(x) # add x to uniq_items[] list
dup_items.append(x) # add x to dup_items[] list
print(dup_items)
def bar():
global c
c += 1
print c
14. Draw the output of the following python program on the canvas.
from cs1graphics import *
paper=Canvas(300,350)
defdraw_face():
c=Circle(40,Point(170,170))
b=Path(Point(170,210),Point(170,280))
paper.add(c)
paper.add(b)
defdraw_leg():
leg=Path(Point(170,280),Point(170,320))
return leg
theleg=draw_leg()
leftleg=theleg.clone()
rightleg=theleg.clone()
leftleg.rotate(90)
rightleg.rotate(-90)
paper.add(theleg)
paper.add(leftleg)
paper.add(rightleg)
draw_face()
You need to trace as in the figure above. I correct the code for you. Can=Canvas(300,350)
changed to paper=Canvas(300,350)
15. Write a Python script to print a dictionary where the keys are numbers between 1 and 15
(both included) and the values are square of keys.
Answer: since the questions requires to write a program to print the dictionary, we need
to write a program to create a diction first and then use print function to print the
dictionary.
Sample Dictionary
{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}
OR
dict1={}
for i in range(1, 16):
dict1[i]=i**2
print dict1
reflection(statue)
Run it and check and learn it. It is like seeing yourself in the mirror.