DV Module 2 Functions & Operators Flashcards
What print is an example of a
Function name
Can you make your own functions in Python
Yes you can and the name should be significant to its use.
Anything thing between quotes will be taken literally, not as code but as_____.
Data which is string
A functions_name and a argument(s) forms a
function invocation
Steps the code takes to use a Function(argument)
- checks of the name is LEGAL]
- allows you to invoke; make sure there is the correct amount of functions.
- Leaves your code for a moment; jumps into the function you want to invoke.
- Executes its code; causes the desired effect
- returns to your code
Two ways you can create a newline or and emptyline
empty print() \n
Why would one use , in an argument?
To put more then one argument in the string
what is the keyword ‘end=’ used for
Code:
print(“My name is “, end=””)
print(“Monty Python.”)
to tell the code to keep the next argument on the same line of code.
Output:
My name is Monty Python.
what is keyword ‘sep=’ used for
Code:
print(“My”, “name”, “is”, “Monty”, “Python.”, sep=”-“)
sep = separator. it is used to choose what separate’s the argument’s
Output:
print(“My”, “name”, “is”, “Monty”, “Python.”, sep=”-“)
Can you mix more then one Keyword argument
yes you can mix keyword in one invocation
Explane a Literal
Data whose values are determined by the literal itself
Difference between an
Integers & Floats
Integers: devoid of fractional parts
Floats: Numbers that contain fractional parts
11111111 how is this different in python to 11_111_111
They are the same and underscores are the only way that Python allows you to make integers easier to read.
What symbols are used to represent an octal&hexadeciamal number
‘0o’ is used for octaldecimal numbers
‘0x’ is used for hexadecimal numbers
what abbreviation would one use to shorten a long number such as 2_0000_0000?
2 * 10e8 or 2 * 10E8
Exponent: value after E must be an integer
Base: before E may or may not be an integer
What are Boolean values used for?
they’re used to represent a very abstract value - truthfulness.
What types of literals are the following four examples?
- “1.5”
- 2.0
- 528
- False
- String
- Float
- Integer
- Boolean
What is unique about the Arithmetic Operation: division?
who would we negate this unique effect
code:
print(6 / 3)
output?
The result is always a float
to allow division to be integer we must use “//”
print(6//3)
What does using the ‘%’ operator do
Code:
print(14 % 4)
Output:?
displace the remainder of the integers being divided
14 // 4 gives 3 → this is the integer quotient;
3 * 4 gives 12 → as a result of quotient and divisor multiplication;
14 - 12 gives 2 → this is the remainder.
Output = 2
in some other codes this is called the Modulo
Operater Bindings
Ex 1:
Code:
print(9 % 6 % 2)
Output ?
Ex 2:
Code
print(2 ** 2 ** 3)
Output?
Ex 1
Division is Left sided binding going from left to right
Output: from left to right: first (9 % 6) gives 3, and then (3 % 2) gives 1;
Ex 2
the exponentiation operator uses right-sided binding.
Output: (2 ** 3) → 8; (2 ** 8) → 256
List priorities of Operators
- +, - unary A unary operator is an operator with only one operand, e.g., -1, or +3.
- **
- *, / , // , %
- +, - binary A binary operator is an operator with two operands, e.g., 4 + 5, or 12 % 5.
Exercise 1
What is the output of the following snippet?
print((2 ** 4), (2 * 4.), (2 * 4))
16 8.0 8
Exercise 2
What is the output of the following snippet?
print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))
-0.5 0.5 0 -1
Exercise 3
What is the output of the following snippet?
print((2 % -4), (2 % 4), (2 ** 3 ** 2))
-2 2 512