0% found this document useful (0 votes)
8 views

Solutions to exercises_Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Solutions to exercises_Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Solutions to exercises for sections 3.1 - 3.

5 :
Question 1

a)

I)

1) Invalid
2) Invalid
3) Invalid
4) Invalid
5) Valid
6) Invalid

II)

b) All should give errors! These are reserved words that should not be used as variable names!

c) The cause of error is the unequal amount of indentation in the second if statement.

1
Solution:

d)

Result:

e)

Result:

2
Question 2

a) Variables are reserved memory locations to store values of some sort. This means that when
you create a variable you reserve some space in memory for a value

b)

c)

d)

Result:

3
e) List and Explanation of different number types:
Variables of type int are restricted to numbers in the range ±231 while
variables of type long can store numbers of arbitrary size. Additionally,
Integers have 32 bits of precision while long have unlimited precision.
Precision is the number of digits in a number. For example, the number
123.45 has a precision of 5.
Float and decimal can be referred to as similar however the difference is
related to their precision. Float has 7 digits (32 bits) and decimal can have
28-29 significant digits (128 bits). In simple terms, integer is a whole
number while a float is a decimal number.
To find out the different number type in Python, use the Type function.
For instance, type into Python Interpreter and hit Enter to see what type
of number it is:
type (-1.5) or type (5)

f)

Result:

g)

4
Question 3

a) Four Basic operator used in python with example :

Arithmetic Operators e.g. +, -

Comparison (Relational) Operators e.g. ==, !=

Assignment Operators e.g =, +=

Logical Operators e.g. and, or

b)

c)

I) x==y

II) x!=y

d)

x+=y means x=x+y which is x = 5+10 = 15


x*=y means x=x*y which is x = 5*10 = 50
x%=y means x=x%y which is x=5%10 = 5

e) Explanation of three logical operators used in Python.


AND - If both operands are true, conditions become true
OR - If any of the two operands are non zero, then conditions becomes true
NOT - Used to reverse the logical state of its operand. If condition is true, then the NOT
operator makes it false.

5
Question 4

a)
Sometimes you will come across situations whereby you need to execute a block of code
several number of times. Generally, statements are executed sequentially: the first statement in
a function is executed first, followed by the second, and so on. A loop statement allows us to
execute a statement or group of statements multiple times.
While loop - repeats a statement or a group of statements while a given condition is true.
For loop - executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.

b)

Result:

c)

Result:

6
d)

Result:

e)

Result:

7
Question 5

a)
The if statement is used to check a condition and if the condition is true, we run a block of
statements (called the if-block), else we process another block of statements (called the else-
block).
An else statement contains the block of code that executes if the conditional expression in the
if statement resolves to 0 or a false value.
The elif statement allows you to check multiple expressions for truth value and execute a block
of code as soon as one of the conditions evaluates to true.

b)

Result:

c)

Result:

8
d)

Result:

9
Solutions to exercises for sections 3.6 - 3.11

Question 6

a)

3rd item - onion


3rd to 6th item - onion, ketchup, garlic, ginger
After replacing garlic with mustard, our new list will be - [bread, sausage, onion, ketchup,
mustard, ginger]
Concatenating MyShopping and newlist = [bread, sausage, onion, ketchup, mustard, ginger,
coke, Orange Juice]

b) Please refer to explanation given in the screenshot for (a) - similar explanation applies here
as well.

10
3rd item - 256

3rd to 6th item - 256, 8.5, salary, -9

Concatenating MyTuple and NewTuple - staff, id, 256, 8.5, salary, -9, studname, studid, 700499

Question 7

a)

mydict.keys() - studname, studid, major, term

mydict.values ()- Tracy, 700499, Geomatics, Summer Semester

Question 8

a)

A function is a block of organized, reusable code that is used to perform a single, related action.
Functions are needed to avoid having to write programs by ourselves and also it allows us to
share pieces of code with others.

b) simple rules to follow when defining a function in Python:


 Function blocks follow this format: def function name ( ).

 Any input parameters or arguments should be placed within these parentheses ().
 The first statement of a function can be an optional statement - the
documentation string of the function or docstring.

 The code block within every function starts with a colon (:) and is indented.

11
The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.

c)

print Hello ('Tracy') - Hello Tracy | print Hello (str(24)) - Hello 24

d)

e)

12

You might also like