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 is the name of the operands below?
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 operator 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
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: exponentiation
2: + , -
(Note: unary operators that are next to the right of the exponentiation operator bind more strongly)
3: *, /, //, %
4: +, - (binary)
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
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