Module 2 Flashcards

1
Q

A function in a programming language:

A

Causes some effect:
–> like printing to the terminal ( print() )
–> finding the length of an object ( len() )

Evaluates a value
–> abs() = returns the absolute value of a number

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

function argument(s)

A

Arguments are the actual values that get passed on to the function THROUGH its parameters

ex: the print() function can take multiple arguments

print( “this”, end=” ,”)

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

function parameter

A

the variable listed inside the parentheses of a function definition

i.e.:
def myFunc(a: str, b: str) –> str:
return a + b

The function above takes two string arguments (values) and concats them to return a concatinated string value

the variables “a” and “b” in the definition above are the function’s parameters

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

What happens during a function invocation?

A

1 - Python checks if the name is legal (and isn’t already assigned to an existing function)

2 - checks if the number of arguments provided match the number required

3 - it passes your provided arguments into the function

4 - it executes the code, causes the desired effect and finishes its task

5 - it returns to your code (to the place just after function invocation) and resumes its execution

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

What kind of error does this tiny script throw?

print(Dave)

A

NameError - because the object “Dave” isn’t defined anywhere in the code

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

What kind of error does this throw?:

print “Dave”

A

SyntaxError
– > since the print() function isn’t built correctly it throws this error

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

For Python, how many instructions can one line contain?

A

One

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

The print() function begins its output from a _______ line each time

A

new

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

What does a print() function without arguments create?

A

A newline

“\n”

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

what is the backslash called when used inside strings?

A

the escape character

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

what does “\n” mean?

A

newline character which urges the console to start a new output line

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

The print() function puts a ______ between the outputted arguments as default separator

A

space

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

What is “the positional way” of passing arguments into a function

A

The position of each argument corresponds to each parameter it is passed on to.

For example:

def fuBar( a: str, b: int) -> str:
return a * b

and calling the function:

print(fuBar( 2 , “goose”))

Would result in an error because passing the number 2 in the first position when we should have passed the str, “goose”

The argument in position one only accepts a str not an int.

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

keyword argument

A

an argument that can be assigned a value and are identifiable within that function by the specific names they have

they have three elements
a keyword
an equal sign (an assignment operator)
and a value assigned to the argument

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

keyword arguments have to be put after the ______ positional argument

A

last

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

What does the “sep” keyword argument do?

A

It separates the internal string by the value it is assigned.

print(“this”, “is”, “a”, “string”, sep=”_”)

becomes:
this_is_a_string

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

Built in functions are

A

always available and don’t have to be imported

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

Python 3.8 comes with _____ built-in functions

A

69

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

How to invoke a function (call a function)

A

use the function name, followed by parentheses

pass arguments to the function by placing them inside the parentheses

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

Python strings are delimited with _____

A

quotes or apostrophes (must be the same on either side of the str)

So “string” or ‘string’ not “string’

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

computer programs are collections of _________

A

instructions

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

If a number is preceded by a 0o or 0O what type of value is it?

A

Octal

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

If a number is preceded by a 0x or 0X what type of value is it?

A

Hexadecimal

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

What will the following code print?
And why?

print(“hello “ ‘world ‘ “it “ ‘is ‘ “I”)

A

hello world it is I

  • Python will automatically concat strings that are next to each other
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is a “literal”?

A

A literal is the data whose values are determined by the literal itself

In other words, a literal is literally the data itself

Another way to think about it—in the code below c is a variable that is assigned a literal value of “this string”

c = “this string”

So the literal above is a “string literal” with the value of “this string”

c on the other hand has no innate meaning or value except for what we assign it. And the value it is assigned is a literal of some type.

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

What is the syntax for scientific notation?

A

Ne^M or Ne^-M

Where N is any number greater than 0 and less than 10 and M is any integer

1.23e3 = 1230.0

4.78e-4 = 0.000478

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

What is an Integer?

A

Non floating point numbers

I.e, numbers devoid of the fractional part

1
17
234
3766

28
Q

What is a floating point number?

A

Numbers that contain (or are able to contain) the fractional part

1.94

29
Q

What are the two ways to write integers?

A

1111111
Or
1_111_111

30
Q

Does Python accept commas in integer literals? Or float literals?

31
Q

Can you omit the zero when it is the only number in front of or after the decimal?

A

Yes

So 0.4 could be written as .4
And 4.0 could be written as 4.

32
Q

Please show the two different ways of writing a string within a string

A

aStr = ‘he said, “this is a string” ‘
Or
aStr = “he said, \”this is a string\””

Another example is contractions:

aStr = “I mean, ain’t life grand?”
Or
aStr = ‘I mean, ain\’t life grand?’

33
Q

What do Boolean values represent?

A

Truthfulness

34
Q

What numeric values do True and False represent?

A

True == 1
False == 0

35
Q

What is the binary system

A

A system of numbers that employs 2 as the base. Therefore a binary number is made up of 0s and 1s only.

36
Q

What is the literal that represent the absence of a value?

37
Q

Operator

A

A symbol of the programming language which is able to operate on the values

38
Q

Expressions

A

Data + operators

Ex:

1+2

39
Q

Most common operators

A

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

40
Q

What is this operator?

**

A

An exponentiation operator

41
Q

When both ** arguments are integers the result is an _____

When at least one ** argument is a float the result of a _____

A

Integer

Float

42
Q

What is this operator?
/

A

A divisional operator

43
Q

What is the result of a divisional operator?

44
Q

What is the name of the operands below?

a / b

A

a = dividend
b = divisor

45
Q

What is this operator?

//

A

An integer divisional

46
Q

What is important to understand about the integer divisional operator?

A

The results lack the fractional part, in other words the results are always rounded so the results are either an integer or a float that is always equal to zero

The result of integer division always rounded to the nearest integer value that is less than the real result

In other word the rounding always goes to the lesser integer — the rounding is always down

So if the result would be 1.5 for instance, it will be 1 after integer division

If the result would be -1.5 the result will be -2 (because -2 is less than -1.5)

47
Q

What is the name of this operator?

%

48
Q

What is the result of the modulo operator?

A

The remainder after dividing one value after another

49
Q

What is the one important rule to remember about integer division, division, or modulo division?

A

Do not try to divide by zero

50
Q

What is the name of this operator?

-

A

Subtraction

51
Q

What is a unary operator?

A
  • , +

The unary operators take only one argument:

-1

and they change the value of the operator they work on

So -1 is a negative number and +1 is a positive number

52
Q

What is the result of the following code:

a = -1
print( -a )

A

1

assigning the unary negative operator to a negative value makes that value positive

53
Q

The hierarchy of priorities

A

The phenomenon that causes some operators to act before others

54
Q

What is operator binding

A

It determines the order of some operators with equal priority when they are placed side by side

55
Q

What type of binding does the exponentiation binder have?

A

Right-sided binding

56
Q

List of priorities for operators

A

1: exponentiation

2: + , -
(Note: unary operators that are next to the right of the exponentiation operator bind more strongly)

3: *, /, //, %

4: +, - (binary)

57
Q

Subexpressions in parentheses are always calculated ____[

58
Q

What is the output of the following code:

print( (2 ** 4), (2 * 4.), (2 * 4) )

59
Q

What happens when the left modulo operand is larger than the right?

A

The expression returns the left side operand

60
Q

What are the rules regarding variable names?

A

It must be composed of upper or lowercase letters, digits, or the underscore

The name must begin with a letter (the underscore is considered a letter)

Names are case sensitive

Named must not be any of Python’s reserved words

61
Q

What does the PEP 8 style guide recommend for Variable names?

A

Variable names should be lowercase

Variable names should have words separated by underscores to improve readability

62
Q

How to create a variable?

A

Simply assign a value to it.

63
Q

What is the assignment operator?

64
Q

What is the equality operator?

65
Q

Can a variable be assigned the value of an expression?

A

Yes

var = 3 + 1 x 6

car == 9

66
Q

What does the “is” operator do?

A

Checks whether two variables point to the same object in memory