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

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

What is a floating point number?

A

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

1.94

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

What are the two ways to write integers?

A

1111111
Or
1_111_111

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

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

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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?’

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

What do Boolean values represent?

A

Truthfulness

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

What numeric values do True and False represent?

A

True == 1
False == 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

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

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

A

None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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 are the operands below called? (Hint: it’s not “a and b”)

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 operand 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

What is 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: ( ), **

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

3: *, /, //, %

4: +, - (binary)

5: <, <=, >=, >

6: ==, !=

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

Called snake case

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

print( True is bool )

console:
True

67
Q

Shortcut operators

A

Shortens the assignment expression of a variable.

So
x = x + 1
Becomes
x += 1

The format is as follows

variable = variable operator expression
becomes
variable operator = expression

68
Q

What does “dynamically-typed” mean?

A

It means that you don’t have to declare a variable (and its data type, specifically) before you assign its value.

That is, the interpreter determines a variable’s “type” at runtime, according to the value it contains.

Get it, it’s dynamically TYPEed, as in, the variables TYPE is dynamic, not the actual typing involved with typing the code or something

69
Q

What is another name for compound assignment operators?

A

Shortcut operators

70
Q

What is the output of the following code snippet

var = 2
var = 3
print(var)

71
Q

which of the following variable names are illegal in Python?

my_var
m
101
averylongvariablename
m101
m 101
Del
del

A

101 - starts with a digit
m 101 - contains a space
del - is a keyword

72
Q

what is the output of the following snippet?

a = ‘1’
b = “1”
print(a + b)

73
Q

what is the output of the following code snippet?

a = 6
b = 3
a /= 2 * b

print(a)

74
Q

What is a comment?

A

It is a note that the computer ignored during runtime and is usually used to explain some code to a human (since the computer ignores a comment entirely)

75
Q

When to use comments

A

To explain some code
To mark out code that isn’t currently used

76
Q

How to make a comment span several lines?

A

Put a hash in front of each line

Put the comment between sets of three quotation marks:

“””
here
Is a
Comment
“””

77
Q

If a variable has a self explanatory name it may also be called

A

Self commenting

78
Q

Explain this code snippet and its results:

g = “PYTHON”

print( g[ : : -5 ] )

A

We are printing the PYTHON string starting at the end of the string and moving backwards every -5 indexes from there

The two blank colons in the beginning mean, essentially, use the entire string for this indexing job.

Then the -5 means to start at the end of the string and move backwards 5 index from there

So the printout will be
NP

79
Q

True or False?
Positive indexing is inclusive?

A

False.

g = “haters”

print( g[ : 3] )

The result will be hat because the 3rd index (e) is not included

80
Q

The print function _____ data to the console

81
Q

The input function _____ data from the console

82
Q

Describe string comparison in Python

A

Strings are compared lexicographically (dictionary order), meaning character by character based on their ASCII values.

For numeric strings this means it would compare the first number of each string and a higher number has a higher ASCII value so it would be considered larger

Ex:

print( “200” > “80” )

Would print False to the console because “2” has a lower ASCII value than “8”

For letter comparison, letters earlier in the alphabet are smaller than letters later in the alphabet.

So
print(“a” < “b”) == True

And capital letters actually have a smaller value than their lowercase counterparts.

So
print(“A” < “a”) == True

83
Q

What is the result of the code snippet below?

print( “80” > “8” )

84
Q

What does UTF stand for?

A

Unicode Transformation Format

85
Q

Are ASCII characters valid in UTF-8?

86
Q

Pass is a _____ operation

87
Q

What happens when the step value in a range() function call is 0 ?

A

A ValueError is raised. The step argument cannot be 0)

88
Q

Can range() accept 0 as a step argument?

89
Q

What is the result of this code?

for i in range( 0, 3, 0 ): print(I)

A

ValueError: range() arg 3 must not be 0

90
Q

What is the result of this code snippet?

a = True
b = 10
print( a + b )

A

11

True evaluates to 1 (False evaluates to 0)

91
Q

What will this code snippet return? Why?

alist = [3, 4, 5]
print( “this one”,alist[-2:8])

A

this one [4,5]

String slicing doesn’t care if the end index is out of range for the string. It will slice as much of the string as it can