Chapters 2-4: Functions and Conditionals Flashcards

1
Q

Boolean Expression

A

Expression either true or false.

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

=

A

Assignment operator

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

==

A

Comparison operator - equals

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

greater than, =/greater than, is/is not operators

A

> , >=, is, is not

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

elif

A

Used after the if, short for else if. can be as many as you want in a block, or none. Used same as if, ie if …..:

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

else

A

used at and of the block (if needed) - sets what happens if no conditions (if and elif) are met). used by itself, ie else:

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

nested conditional

A

when a conditional eg ‘else:’ has 2 sub braches, eg an if and an else

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

try/except

A

try: some code. if there is an error (eg cant float an input because it’s words), move on to the except:. No exception? skip the except:.

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

Guardian pattern

A

A logical expression guarding against a runtime error, using the fact logical expressions are evaluated left to right. eg [something] and x!=0 and (3/x)>2. x!=0 guards against runtime error caused if x=0, as 3 cant be divided by it.

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

what error message tells you about error locaction

A

tells you where python detected the error, not necessarily where it is. true error may be earlier in code.

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

Function

A

named sequence of statements that performs a computation, eg type()

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

Argument

A

the expression in the brackets of a function, ie the the expression the function “takes” before it “return”s a “value”

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

max()/min()

A

Functions which return the largest/smallest character/number in a string/comma’d list

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

len()

A

Function which returns the number of characters in a string

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

import math

A

Imports the “math” module. Can then be used, eg assign radians=0.7, height=math.sin(radians) (prefix functions with math.)

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

import random

A

Imports the “random” module, allows the generation of pseudorandom (deterministic but you cant really tell) with various random.functions

17
Q

x=random.random()

A

Assigns x as random number between 0.0 and 1.0 (inclusive of 0.0 but not 1.0)

18
Q

random.randint(1, 10)

A

Returns a pseudorandom number between 1 and 10 inclusive.

19
Q

x=[1, 3, 5, 7]

A

Assigns x as a sequence, from which individual numbers can be chosen.

20
Q

random.choice(x)

A

Returns a pseudorandom choice from a pre-assigned sequence, x.

21
Q

def

A

defines a function, eg def print_lyrics(): // [tab]print(‘xxxxx’). 1st line is head, ends with “:”. Rest is the body, indented.

22
Q

def x(y)
z(y)
z(y)

A

defines a function with (a) parameter(s). Python learns to do the same regardless of parameter if the y is the same in x(y) and z(y)

23
Q

fruitful vs void function

A

function which returns a ‘worked-out’ result eg input() or math.sqrt(), vs a function which performs a task eg print()

24
Q

return

A

used in the body of a function definition, to return the result of a fruitful function, eg def addtwo(a, b)//[tab]added=a+b//[tab]return added. Without final line, python adds the parameters but the result isnt reported, so it’s pointless.

25
Q

None

A

A type of value, just like str or int, which means it is the result of a void function, or a fruitful function where the output has not been returned.

26
Q

round(x, n)

A

Function, rouund a number, x, to n decimal places.

27
Q

quit()

A

quit python/python environment

28
Q

user_input=input(‘Type something!\n’)

A

assigns user_input variable with the vavlue user enters

29
Q

Semantic error

A

The situation in which a program executes but does not produce the results that were intended. No error message, just wrong result.

30
Q

Syntactic error

A

Error on syntax, ie grammar, of the command which means the interpreter cannot intrpret it. Leads to error message.

31
Q

Logic error

A

An error in logic, often due to order of statements being wrong, or how the relate to one another.

32
Q

Variable

A

A name you can assign a value, e.g. a=45, message=’hey man’. ‘x’ not needed for variables, but is for value. Python interprets these IDENTICALLY to their value unless changing the value with variable=something new. Case sensitive!

33
Q

int()/float()/str()

A

returns argument as given type. convert with x=int(‘x’) etc

34
Q

**

A

exponentiation, eg 5**2 = 5^2 = 25. alongside

35
Q

//

A

/ but truncates result to an interger, i.e. rounds down to nearest interger. 5.9//2=2.0

36
Q

%

A

modulus, gives the remainder not the quotient. 7//3=2, 7%3=1. v. useful, used in clever ways.