Solutions to exercises_Python
Solutions to exercises_Python
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
b)
c)
I) x==y
II) x!=y
d)
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)
b) Please refer to explanation given in the screenshot for (a) - similar explanation applies here
as well.
10
3rd item - 256
Concatenating MyTuple and NewTuple - staff, id, 256, 8.5, salary, -9, studname, studid, 700499
Question 7
a)
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.
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)
d)
e)
12