Assignment 1 Flashcards

week 1/2/3 excluding lecture 1.1

1
Q

Arithmetic Operations on Python

A

1) addition (+), subtraction (-), multiplication (*), division (/)
2) exponent (**)
3) integer division (//)
4) remainder (%)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Operation on strings

A

+ or *

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Cannot add different variable types

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variable types

A

String
Integer
Float
Boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

int(value)
string(value)
float(value)

A

changes value to int/string/float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Python is case sensitive and avoid using keywords as variable names

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Relational operators?

A

<, >, <=, >=, ==, !=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Relational operators
in, not in

A

string operators
eg: ‘app’ in ‘apples’ returns TRUE
eg: ‘bbc’ in ‘apples’ returns FALSE
eg: ‘abp’ in ‘apples’ returns FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Boolean operators

A

or
and
not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Boolean operators precedence

A

not -> and -> or

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Precedence of ALL operators

A

()
**
+x/-x (positive/negative)
*, /, //, %
+, -
relational operators
not -> and -> or

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

print() and input()

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

\n

A

new line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

\t

A

tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

\

A

inserts backslash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

", '

A

inserts ‘ or “

17
Q

\

A

used to split a long string on multiple lines

18
Q

s.upper()
s.lower()

A

returns string s in all caps or lowercase

19
Q

s.strip()

A

removes leading and trailing space

20
Q

s.count(substring)

A

returns the number of time substring occurs in s

21
Q

s.replace(old, new)

A
22
Q

len(s)

A

nb of characters

23
Q

s.split(‘character’, max number of items in list)

A
24
Q

3 Types of Error

A

Syntax, Runtime, Semantic

25
Q

Syntax Error

A

Cased by writing code that is not the correct structure or format

26
Q

Runtime Error

A

Caused by doing something that’s not allowed (e.g. subtracting strings, using not assigned variables)

27
Q

Semantic Error

A

Caused when your program does something, just not the right thing

28
Q

Error Messages

A

Parse, Type, Name, Value

29
Q

Parse Error

A

Caused by incorrect syntax

30
Q

Type Error

A

Combining value types that are not compatible
eg: string + int

31
Q

Name Error

A

typos or using an unassigned variable

32
Q

Value Error

A

using a variable that has a correct type but a problematic value

33
Q

s[first: last (excluded) : occurrence]

A

if occurrence is -x, then prints reversed

34
Q

str.format()

A

eg:
‘{} {} and {} {}’.format(‘3’, ‘oranges’, ‘5’, ‘apples’)
‘{0} {1} and {2} {3}’.format(‘3’, ‘oranges’, ‘5’, ‘apples’)
»> ‘3 oranges and 5 apples’

‘{2} {1} and {0} {3}’.format(‘3’, ‘oranges’, ‘5’, ‘apples’)
»> ‘5 oranges and 3 apples’

35
Q

str.format() -> a variable can appear multiple times

A

‘{0} {1} and {0} {1} and {2} {3}’.format(‘3’, ‘oranges’, ‘5’, ‘apples’)
»>’3 oranges and 3 oranges and 5 apples’

36
Q

str.format() -> keyword arguments

A

‘{num1} {fruit1} and {num2} {fruit2}’.format(
num2 = 5,
fruit1 = ‘apples’,
fruit2 = ‘oranges’ ,
num1 = 3)
»> ‘3 apples and 5 oranges’

37
Q

str.format() -> rounding

A

num1 = 4.674545
num2 = 6.342421
‘{num1:.1f} kg of {fruit1} and {num2:.1f} kg of {fruit2}’.format(
… num1 = num1,
… num2 = num2,
… fruit1 = ‘apples’,
… fruit2 = ‘oranges’)

> > > ‘4.8 kg of apples and 6.3 kg of oranges’

pi = 3.14159265354
f’{pi : .2f}’
»> 3.14