Module 2 Flashcards
A function in a programming language:
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
function argument(s)
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=” ,”)
function parameter
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
What happens during a function invocation?
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
What kind of error does this tiny script throw?
print(Dave)
NameError - because the object “Dave” isn’t defined anywhere in the code
What kind of error does this throw?:
print “Dave”
SyntaxError
– > since the print() function isn’t built correctly it throws this error
For Python, how many instructions can one line contain?
One
The print() function begins its output from a _______ line each time
new
What does a print() function without arguments create?
A newline
“\n”
what is the backslash called when used inside strings?
the escape character
what does “\n” mean?
newline character which urges the console to start a new output line
The print() function puts a ______ between the outputted arguments as default separator
space
What is “the positional way” of passing arguments into a function
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.
keyword argument
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
keyword arguments have to be put after the ______ positional argument
last
What does the “sep” keyword argument do?
It separates the internal string by the value it is assigned.
print(“this”, “is”, “a”, “string”, sep=”_”)
becomes:
this_is_a_string
Built in functions are
always available and don’t have to be imported
Python 3.8 comes with _____ built-in functions
69
How to invoke a function (call a function)
use the function name, followed by parentheses
pass arguments to the function by placing them inside the parentheses
Python strings are delimited with _____
quotes or apostrophes (must be the same on either side of the str)
So “string” or ‘string’ not “string’
computer programs are collections of _________
instructions
If a number is preceded by a 0o or 0O what type of value is it?
Octal
If a number is preceded by a 0x or 0X what type of value is it?
Hexadecimal
What will the following code print?
And why?
print(“hello “ ‘world ‘ “it “ ‘is ‘ “I”)
hello world it is I
- Python will automatically concat strings that are next to each other
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.
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
What is an Integer?
Non floating point numbers
i.e, numbers devoid of the fractional part
1
17
234
3766
What is a floating point number?
Numbers that contain (or are able to contain) the fractional part
1.94
What are the two ways to write integers?
1111111
Or
1_111_111
Does Python accept commas in integer literals? Or float literals?
No.
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.
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?’
What do Boolean values represent?
Truthfulness
What numeric values do True and False represent?
True == 1
False == 0
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.
What is the literal that represent the absence of a value?
None
Operator
A symbol of the programming language which is able to operate on the values
Expressions
Data + operators
Ex:
1+2
Most common operators
+, -, *, /, //, %, **
What is this operator?
**
An exponentiation operator
When both ** arguments are integers the result is an _____
When at least one ** argument is a float the result of a _____
Integer
Float
What is this operator?
/
A divisional operator
What is the result of a divisional operator?
A float
What are the operands below called? (Hint: it’s not “a and b”)
a / b
a = dividend
b = divisor
What is this operator?
//
An integer divisional
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)
What is the name of this operator?
%
Modulo
What is the result of the modulo operator?
The remainder after dividing one value after another
What is the one important rule to remember about integer division, division, or modulo division?
Do not try to divide by zero
What is the name of this operator?
-
Subtraction
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
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
What is the hierarchy of priorities
The phenomenon that causes some operators to act before others
What is operator binding
It determines the order of some operators with equal priority when they are placed side by side
What type of binding does the exponentiation binder have?
Right-sided binding
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: ==, !=
Subexpressions in parentheses are always calculated ____[
First
What is the output of the following code:
print( (2 ** 4), (2 * 4.), (2 * 4) )
16 8.0 8
What happens when the left modulo operand is larger than the right?
The expression returns the left side operand
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
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
How to create a variable?
Simply assign a value to it.
What is the assignment operator?
=
What is the equality operator?
==
Can a variable be assigned the value of an expression?
Yes
var = 3 + 1 x 6
car == 9
What does the “is” operator do?
Checks whether two variables point to the same object in memory
print( True is bool )
console:
True
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
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
What is another name for compound assignment operators?
Shortcut operators
What is the output of the following code snippet
var = 2
var = 3
print(var)
3
which of the following variable names are illegal in Python?
my_var
m
101
averylongvariablename
m101
m 101
Del
del
101 - starts with a digit
m 101 - contains a space
del - is a keyword
what is the output of the following snippet?
a = ‘1’
b = “1”
print(a + b)
11
what is the output of the following code snippet?
a = 6
b = 3
a /= 2 * b
print(a)
1.0
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)
When to use comments
To explain some code
To mark out code that isn’t currently used
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
“””
If a variable has a self explanatory name it may also be called
Self commenting
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
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
The print function _____ data to the console
Sends
The input function _____ data from the console
Takes
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
What is the result of the code snippet below?
print( “80” > “8” )
True
What does UTF stand for?
Unicode Transformation Format
Are ASCII characters valid in UTF-8?
Yes
Pass is a _____ operation
Null
What happens when the step value in a range() function call is 0 ?
A ValueError is raised. The step argument cannot be 0)
Can range() accept 0 as a step argument?
No
What is the result of this code?
for i in range( 0, 3, 0 ): print(I)
ValueError: range() arg 3 must not be 0
What is the result of this code snippet?
a = True
b = 10
print( a + b )
11
True evaluates to 1 (False evaluates to 0)
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