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 in the print() function?

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
What is a “literal”?
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.
26
What is the syntax for scientific notation?
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
27
What is an Integer?
Non floating point numbers i.e, numbers devoid of the fractional part 1 17 234 3766
28
What is a floating point number?
Numbers that contain (or are able to contain) the fractional part 1.94
29
What are the two ways to write integers?
1111111 Or 1_111_111
30
Does Python accept commas in integer literals? Or float literals?
No.
31
Can you omit the zero when it is the only number in front of or after the decimal?
Yes So 0.4 could be written as .4 And 4.0 could be written as 4.
32
Please show the two different ways of writing a string within a string
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
What do Boolean values represent?
Truthfulness
34
What numeric values do True and False represent?
True == 1 False == 0
35
What is the binary system
A system of numbers that employs 2 as the base. Therefore a binary number is made up of 0s and 1s only.
36
What is the literal that represent the absence of a value?
None
37
Operator
A symbol of the programming language which is able to operate on the values
38
Expressions
Data + operators Ex: 1+2
39
Most common operators
+, -, *, /, //, %, **
40
What is this operator? **
An exponentiation operator
41
When both ** arguments are integers the result is an _____ When at least one ** argument is a float the result of a _____
Integer Float
42
What is this operator? /
A divisional operator
43
What is the result of a divisional operator?
A float
44
What are the operands below called? (Hint: it’s not “a and b”) a / b
a = dividend b = divisor
45
What is this operator? //
An integer divisional
46
What is important to understand about the integer divisional operator?
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
What is the name of this operator? %
Modulo
48
What is the result of the modulo operator?
The remainder after dividing one value after another
49
What is the one important rule to remember about integer division, division, or modulo division?
Do not try to divide by zero
50
What is the name of this operator? -
Subtraction
51
What is a unary operator?
- , + 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
What is the result of the following code: a = -1 print( -a )
1 assigning the unary negative operator to a negative value makes that value positive
53
What is the hierarchy of priorities
The phenomenon that causes some operators to act before others
54
What is operator binding
It determines the order of some operators with equal priority when they are placed side by side
55
What type of binding does the exponentiation binder have?
Right-sided binding
56
List of priorities for operators
1: ( ), ** 2: + , - (Note: unary operators that are next to the right of the exponentiation operator bind more strongly) 3: *, /, //, % 4: +, - (binary) 5: <, <=, >=, > 6: ==, !=
57
Subexpressions in parentheses are always calculated ____[
First
58
What is the output of the following code: print( (2 ** 4), (2 * 4.), (2 * 4) )
16 8.0 8
59
What happens when the left modulo operand is larger than the right?
The expression returns the left side operand
60
What are the rules regarding variable names?
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
What does the PEP 8 style guide recommend for Variable names?
Variable names should be lowercase Variable names should have words separated by underscores to improve readability Called snake case
62
How to create a variable?
Simply assign a value to it.
63
What is the assignment operator?
=
64
What is the equality operator?
==
65
Can a variable be assigned the value of an expression?
Yes var = 3 + 1 x 6 car == 9
66
What does the “is” operator do?
Checks whether two variables point to the same object in memory print( True is bool ) console: True
67
Shortcut operators
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
What does “dynamically-typed” mean?
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
What is another name for compound assignment operators?
Shortcut operators
70
What is the output of the following code snippet var = 2 var = 3 print(var)
3
71
which of the following variable names are illegal in Python? my_var m 101 averylongvariablename m101 m 101 Del del Yurt_Turt heart-2-heart
101 - starts with a digit m 101 - contains a space del - is a keyword heart-to-heart - contains hyphens and variables may only contain alpha numeric characters and underscores
72
what is the output of the following snippet? a = ‘1’ b = “1” print(a + b)
11
73
what is the output of the following code snippet? a = 6 b = 3 a /= 2 * b print(a)
1.0
74
What is a comment?
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
When to use comments
To explain some code To mark out code that isn’t currently used
76
How to make a comment span several lines?
Put a hash in front of each line Put the comment between sets of three quotation marks: “”” here Is a Comment “””
77
If a variable has a self explanatory name it may also be called
Self commenting
78
Explain this code snippet and its results: g = “PYTHON” print( g[ : : -5 ] )
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
True or False? Positive indexing is inclusive?
False. g = “haters” print( g[ : 3] ) The result will be hat because the 3rd index (e) is not included
80
The print function _____ data to the console
Sends
81
The input function _____ data from the console
Takes
82
Describe string comparison in Python
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
What is the result of the code snippet below? print( “80” > “8” )
True
84
What does UTF stand for?
Unicode Transformation Format
85
Are ASCII characters valid in UTF-8?
Yes
86
Pass is a _____ operation
Null
87
What happens when the step value in a range() function call is 0 ?
A ValueError is raised. The step argument cannot be 0)
88
Can range() accept 0 as a step argument?
No
89
What is the result of this code? for i in range( 0, 3, 0 ): print(I)
ValueError: range() arg 3 must not be 0
90
What is the result of this code snippet? a = True b = 10 print( a + b )
11 True evaluates to 1 (False evaluates to 0)
91
What will this code snippet return? Why? alist = [3, 4, 5] print( "this one",alist[-2:8])
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
92
What will happen after the following code is executed? counter = 1 while counter < 10: x = counter + 1 print(x)
It will print 2 forever. counter is never incremented so it will always be less than 10, and so it will print 2 until your computer melts.
93