AL ICT - programming
language – Python -Assignment 1
11) What do the follow Python statements return? If an error
results, just write "error". (assume that you be the
interpreter)
What You Type
|
What Python Returns
|
||
>>> 1 + 2 * 3
|
|||
>>>
4 ** (5 - 2 - 1)
|
|||
>>>
len('cat') + 2 * len('mouse')
|
|||
>>>
len(str(2 * 3 * 4))
|
|||
>>>
int(3 * str(2))
|
|||
>>>
4 * "10"
|
|||
>>>
"4" * "10"
|
2 ) You must use
exact Python syntax in the following questions!!
Question Answer
Write a fragment of Python code that works
as follows. Ask the user to enter their favourite drink, and if they say
"water", print "good!". Otherwise, print "what a
poor idea".
|
||
Write
a for-loop that prints the numbers
1000 down to 1 (inclusive), one number per line, i.e.:
1000
999
998
.
.
.
2
1
|
||
Re-do the previous question, but this time
use a while-loop.
|
33)How old is Python? What languages does it relate
to? Give a very brief summary about python language.
4 4) Consider
the following Python fragment:
n =
3
m =
5
n =
n + m
m =
n – m
n =
n – m
What values do we have in the two variables at the end? Can
you summarize the meaning of the last three lines, in general?
5. Consider
the following code:
if x > 3:
if x <= 5:
y = 1
elif x != 6:
y = 2
else:
y = 3
else:
y = 4
If
y
has the value 2
after executing the above
program fragment, then what do you know about the initial value of x
?
6) Consider the following two fragments:
if x == 5:
x = x + 1
else:
x = 8
|
if x == 5:
x = x + 1
if x != 5:
x = 8
|
Are the two fragments logically equivalent? Why or why not?
7) Simplify the following expressions:
assume that a=5
·
a and
not a …………………………………
·
a or not
a …………………………………
·
x >=
6 and x < 7 …………………………………
·
a ==
False …………………………………
·
a ==
True …………………………………
·
a or
True …………………………………
·
a and
True …………………………………
·
a and
False …………………………………
·
a or
False …………………………………
·
False
and False or True …………………………………
·
False
and (False or True) …………………………………
8) What
is the output of the following code
fragment:
n = 10
m
= n
n = 12
print m
Does
the change in n
affect m
at all? Why (or why not)?
9) Briefly
explain and evaluate (assume i holds an integer value):
i =
i + 1
i
== i + 1
i
== i
i =
i
10.
Consider the following Python code:
i =
10
while
i < 10:
i = i - 1
print
i
What
does it print (or amount to)? Do you see any potential problem with it
11.
s = 0
i = 10
while i > 0:
s =
s + i
i =
i - 1
print s
What does it print (if anything)?
12.
Your
school wants you
to write a program for them that will calculate the average number of wins for
their chess team
over the past five years. The user of
the program should be able to enter the number of wins each year. The program will calculate the average number
of wins during that five year period and display that information to the
screen. Draw a flowchart and write
a python program to accomplish the above task.
13.
A dietician wants you to write a program that will calculate
the number of calories a person can lose by walking at a slow pace for a mile;
however, the user will have only the distance given by a pedometer, which is
measured in steps and not miles. Assume
each mile a person walks is equivalent to 2000 steps, and that for every mile
walked, a person loses 65 calories.
Allow the user of the program to enter the number of steps taken
throughout the day. The program will
calculate the distance in miles and the number of calories lost. The user of the program should also be able
to enter the day of the week the data is being calculated for. The day of the week, the distance in miles,
and the calories lost should then be displayed to the screen. The
algorithm is given for you
a.
Get the day of the week.
b.
Get the number of steps reported on the pedometer.
c.
Divide the number of steps taken by 2000, which is the number
of steps per mile.
d.
Multiply the result of Step 3 times 65, which is the number of
calories burned per mile.
e.
Display the input information in Step 1.
f.
Display the calculated information in Step 4 and 5.
Draw a flow chart and write a python program to accomplish
the task/
14.
Is the loop in the code below infinite? How do you know (for
sure) before you run it?
m = 3
n = 5
while n <
10:
m = n - 1
n = 2 * n - m
print n, m
15.
Test the given 2 examples to practice for loop and write
down the output of each.
for letter in
'Python': # First Example
print 'Current Letter :', letter
fruits =
['banana', 'apple', 'mango']
for fruit in
fruits: # Second Example
print 'Current fruit :', fruit
print
"Good bye!"
16.
Test the given 2 examples to practice while loop and
write down the output of each.
count = 0
while (count
< 9):
print 'The count is:', count
count = count + 1
print
"Good bye!"
17.
Write the output of the
following program before you run it. Then check your answer with running the
program.
for letter in
'Python':
if letter == 'h':
break
print 'Current Letter :', letter
18.
Write the output of the
following program before you run it. Then check your answer with running the
program.
var = 10
while var
> 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break
19.
Write the output of the
following program before you run it. Then check your answer with running the
program.
for letter in
'Python':
if letter == 'h':
continue
print 'Current Letter :', letter
20.
Write the output of the
following program before you run it. Then check your answer with running the
program.
var = 10
while var
> 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print
"Good bye!"
No comments:
Post a Comment