RESOURCES

COMP150: Practice Exercises

COMP150: Practice Exercises

Progression 1: Practice exercises

Make a decent attempt at each question before clicking a "Show solution" button. If you do click the button, we recommend typing out the answer for yourself (as opposed to copying and pasting).

Question 1

Given the following:

   
s1 = "friends "
s2 = "for "
s3 = "ever "

write an expression using only these variables and suitable operators that will evaluate to

                "for ever friends friends for ever “

s2 + s3 + s1 + s1 + s2 + s3

Question 2

Enter the following code into a Python script:

  
x = 3
y = 'Bang!'
            

Add to the script so that y is printed x times on one line with no spaces.

print y * x

Question 3

Enter the following code into a script:

  
x = 3
s = 'Hey'
            

Add code to print the result of x * s and the type of data resulting from this expression.

print x * s
print type(x*s)

Question 4

Enter the following code into a script:

   
x = 4
s = '73'
          

Add code to print the result of x * s and the type of data resulting from this expression.

print x * s
print type(x * s)

Question 5

Enter the following code into a script:

   
x = 47
y = 10
            

Add code to print the remainder of x divided by y

print x % y

Question 6

Enter the following into a script:

  
x = 3
y = 5
z = 9
           

Add code to print the result of x raised to the power of y, then divided by z

print x ** y / z

Question 7

Enter the following into a script:

   
x = 3
y = 5
z = 9
           

Add code to print x raised to the power of the result of y divided by z

print x ** (y / z)

Question 8

Enter the following code into a Python script:

  
word_1 = "Good "
word_2 = "dog "
            

Add to the script so that it prints:

"Good dog Good dog Good dog "
           

How many different ways can you achieve the same result?

print (word_1 + word_2) * 3
# OR
print word_1 + word_2 + (word_1 + word_2) * 2
# OR
print word_1 + word_2 + word_1 + word_2 + word_1 + word_2
# ETC