You are on page 1of 7

L2c: f-string, index, slice, conditionals

Data types: Defines operators functions/methods. What we can do with data type.

We can change the data type.

str “Hello” characters ‘a’ + ‘b’ str()


concatenation
.lower()
.upper()
int 123 integer BODMAS int()
times, divide,
add, substract
float 3.132 real numbers float()
bool True True false if statements bool()

Bool of anything is true. Because it is not an empty string.

Name str
customer ID int (if there is characters, we
can use str)
Postcode int
Do you own or rent string/bool (encode, own =
true, rent = false)
length of bench top float
width of bench top float
are you interested in further bool
offers

“7cakes”

357.23

True

Empty string has no space in it. “”

bool(“”) = False

bool(“ “) = True

Operators

+,-, *, /, //, %, **

Operators are any symbols calculating operands.

1+2

1 and 2 are operands

+ is an operator.

‘1’ + ‘1’  ‘11’

1+12

Concatenation is different from addition.

Var = 2 is arbitrary name to store value.

3a a/a 1.0
3b b+b 4
3c b+c 4.0
3d a/b 0.5
3e a //b 0
3f a%b 1
3g a+b/c 2.0
3h 1.5

% ask for the remainder after a divided by d

1%2=1

2%2=0

3%2=1

1%3=1

2%3=2

13 % 4 = 1

4a. 246

4b. 123123

4c. Error

4d. 12

4e. ‘3333’

4f. Error

Floating point error

Tutorial sheet 4
Discussion

1. Boolean is a true or false condition. It stores ‘true’ or ‘false’. Other types can be
converted to it. Empty strings “” is false; zero is also false.
2. (a) Boolean value; (b) a relational operator; (c) a logical operator

== comparison > comparison False bool


relational relational
!= comparison and logic <= comparison
relational relational
or logic >= comparison not logic
relational
True bool < comparison in comparison
relational relational

3. How to use ‘if’ statement:

4. Sequence?
5. Index: variable_name[num]
6. Slice: variable_name[start:end:direction,step]
7. Function:

What is a function?

Function takes a set of input values, perform some calculation based on them, and return a
value.

Input parameters are optional

reutrn is optional

Exercise

1. Evaluate truth expressions: not > and > or

a True or False True


b True and False False
c False and not False or True True
d False and (not False or True) False

(c) False and [not False] or True

>> [False and True] or True

(d) False and (not False or True)

>> False and (True or True)

>> False and True

>> False

2. Give an example of a value for ‘var’ which trigger it and one which not

Trigger Not trigger


if 10 > var >= 5: 5,6,7,8,9 1,2,3,4,11
a
b if var[0] == “A” and var[-1] Aanythinginbetweene anywhere
== “e”:
Anywhere
c if var in [“VIC”, “NSW”, VIC vic
“ACT”]:
NSW nsw
ACT act
d if var: anything nothing

3. Mistake

Lower and uppercase might not work.

4. Mistake

Wrong symbol. eggs == 3 should be eggs = 3

assignment is =

eggs = 5 should be eggs == 5

5. Evaluate: s = “python”

p y t h o n
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1

a s[1] y

b s[-1] n

c s[1:3] + s[3:5] yth + hon => ython

d s[10] Error

e s[10:] nothing, empty string “”

f s[-4:-2] th

g s[:-4] py

h s[::2] pto (2 step, s[0], s[2], s[4], s[6] not here)

i s[::-1] nohtyp

The slicing stops before the end


6. Change the print(answer) in the def into return answer

Otherwise, it will return None as output

In order to use the output of a function (to assign it to a variable) we need to return a value.

Return is also a way of terminating a function

IDE:

Sublime

Grok

PyCharm

Python IDE

Jupyter notebook

You might also like