Python PCAP
What is the inequality operator?
!=
How do you print strings on multiple lines?
"""
When do you use Concatenation?
"+ (plus) sign, when applied to two strings, becomes a concatenation operator Glues two strings together (both arguments must be strings). print(""\nYour name is "" + fnam + "" "" + lnam + ""."")"
What does key word sep="-" do in a print statement?
"It prints each element with - as a separator. print(""My"", ""name"", ""is"", ""Monty"", ""Python."", sep=""-"")"
Can you have a variable named the same as a function's parameter?
"Yes, It's legal, and possible. The parameter named number is a completely different entity from the variable named number def message(number): number = 1234" print(""Enter a number:"", number)
How many arguments can range() take?
"range() function may also accept three arguments (third argument is an increment - default = 1)for i in range(2, 8, 3): - will start with 2 and then 5. "
When are the 2 times None can be used?
"when you assign it to a variable (or return it as a function's result) when you compare it with a variable to diagnose its internal state."
Which has priority ** or *?
'** the exponentiation operator does
Who invented Python?
Guido van Rossum
How do you swap over 2 variables without losing them?
variable1, variable2 = variable2, variable1
how can you loop over iterables?
While or For
How do you get a value from a dictionary?
you have to deliver a valid key value: print(phoneNumbers['Suzy'])
What is the one unbreakable rule of positional and keyword arguments?
you have to put positional arguments before keyword arguments.
What is the outcome of (xor) calculation x ^ 1 = ?
~x - negative 'x' - the xor operator always reverses the bit
What is the difference between division / and //?
"The result produced by the division operator is always a float If you need a integer then you need to use the double division // which for integer by integer division gives an integer result."
Can a variable outside a function be use in a function?
"Yes, A variable existing outside a function has a scope inside the functions' bodies. excluding those of them which define a variable of the same name, but the scope of a variable existing outside a function is supported only when getting its value (reading)"
What is replication?
* (asterisk) sign, when applied to a string and number, becomes a replication operator: 5 * "2" (or "2" * 5) gives "22222"
Which has priority + or /?
/ division does - + and - are the lowest priority
How do you signify binary numbers?
0b or 0B for binary bin(195935983), '0b1011101011011011111011101111'
How do you signify hexidecimal numbers?
0x or 0X for hex
What will does 11_111_111 represent in Python 3.6 onwards?
1111111 - it is just a way of breaking up the number.
What is the output from print(14 % 4)?
2 - The remainder (modulo) % gives the remainder left after the integer division
What is the output from print(3**2)?
9 - (double asterisk) sign is an exponentiation (power) operator. Its left argument is the base, its right, the exponent. If one argument is a float, output will be a float.
What does a function return?
: if a function doesn't return a certain value using a return expression clause, it is assumed that it implicitly returns None.
How do you compare 2 variables?
==
Which has priority >= or ==?
>=
What is an ordered, indexed, mutable collection of objects?
A list
What is a literal?
A literal is data whose values are determined by the literal itself
What is a function?
Action which causes some effect or evaluate a value or some values.
What does 1e-22 represent?
Base 1 and exponent -22, so 0.0000000000000000000001
What does 3e8 represent?
Base = 3 and exponent =8 - so 300000000
How do you create a variable?
By assigning a value to it - variables are just names and values.
What does # signify?
Comments - for marking up code, or taking out bits of code no longer required.
What is the process called where you break code down into chunks?
Decomposition
What is a mutable, unordered collection of pairs?
Dictionary
What is the output from print(True < False)?
FALSE
how do you mark a factorial and what is it?
Factoral is marked with an exclamation mark, and is equal to the product of all natural numbers from one up to its argument. 4! = 1 * 2 * 3 * 4
What are the two ways of printing with quotes:
I like "Monty Python" ? use the escape character print("I like \"Monty Python\"") or to use an apostrophe instead of a quote. print('I like "Monty Python"').
What can you use for conditional?
If, else, elif
What does the global keyword do within a function?
It extends a variable's scope in a way which includes the functions' bodies
What is the ~ (tilde) bitwise operator ?
It is bitwise negation = if A = 0 then ~A = 0
What is the "|" (bar) bitwise operator ?
It is the disjunction - if either A or B = 1, then result =1
What do 0x123 represent?
It represents the Hexidecimal number of 123 (or 291)
What do 0o123 represent?
It represents the Octal number of 123 (or 83)
What does the shift operator (two digraphs) << or >> do?
It shifts value one bit to the left thus corresponds to multiplying it by two or one to the right (ie divid by 2.
What is a parameter?
It's a variable inside a function, arguments exist outside functions,
What is the ^ (caret) bitwise operator ?
It's bitwise exclusive or (xor). Need to be 1 and 0 to be 1, otherwise = 0
How do you remove a dictionary value?
Just delete the key, del dict['dog'] or use popitem() method to remove last item
How do you add or change a value?
Just specify the key and the new value eg: dict['cat'] = 'minou'
Can you have a function without ()?
No
Is there a limit to the number of parameters into a function?
No
If you change a parameter's value in a function does it affect outside the function?
No, A variable that exists inside a function has a scope inside the function body (not outside), You can use the global keyword followed by a variable name to make the variable's scope global.
Is the return keyword obligatory at the end of a function?
No, it will be executed implicitly at the end of the function.
Is None a value?
No, not a value at all, so mustn't take part in any expressions. None is a keyword.
When you import a module, do the names enter your codes namespace?
No, they don't enter your code's namespace. So could have an entity called "pi" and import math, but won't replace pi.
Can you change a Tuple?
No, you Can't change but you can delete tuple as a whole: del myTuple
What is the mnemonic for remembering which mathatic operation takes preference?
PEMDAS: Parentheses, exponent **, Multiplication, Division: Addition, Subtraction, Left to right:
What goes in the () of a function?
Parameters or arguments
What is recursion?
Recursion -a technique where a function invokes itself. Don't forget to consider the conditions which can stop the chain of recursive invocations,
In integer division //, how does it chose which integer if isn't a round number?
Rounding always goes to the lesser integer. Eg print(6//4) = 1. Integer division // can also be called floor division.
What is a mutable, unordered collection?
Set
Name one way to take items out of a list and make a new list?
Slices - eg myLst = lst[1:-1] chops of the first and last element in lst, If you want to copy the list then need to do slicing
What are the two types of literals?
String and Integer
If you "print(2 ** 2 ** 3)", which side does it start? Left (2 ** 2) or Right (2 ** 3)?
The exponentiation operator uses right-sided binding (print(2 ** 2 ** 3) - 2 ** 3 → 8; 2 ** 8 → 256)
What does the len() function give you?
The length of a list
What is an immutable, ordered collection?
Tuple
How can you use a shortened form of the module name?
Use aliases - import module as alias or import math as m Can also import a element with an alias: from module import name as alias
How can you protect against arguments not being given?
Use predefined values, eg lastName="Smith"
How would you check if an element is in a last?
Use the IN or NOT IN operators
What are the rules for naming variables?
Variables have upper and lower case letters (case-sensitive), digits and underscore_ It must begin with a letter, and can't be any of Python's reserved words
Is it possible to have an empty tuple or a one element tuple?
Yes, emptyTuple = (), oneElementTuple2 = 1.,
Is this a valid tuple?
Yes, it is possible to create a tuple just from a set of values separated by commas, but better to use parenthesis tuple1 = (1, 2, 4, 8)
You can't amend to a tuple, but can us use len(), concatenate, multiply or IN /NOT IN?
Yes, len() function will work. You can concatenate with +. Or multiply with *. And 'in' and 'not in'
If you have 2 lists created using the = operator, are they the same list?
Yes, they will both point to one and the same list in memory
If you just do a import (eg import math), what do you have to do when calling a function?
You must prefice with the modules name eg math.sin(math.pi/2)) - Alternatively, you can import the entity: from math import pi
What does the escape text "\\" in a string give you?
\
What symbolises a new line?
\n
What methods can you use to use a for loop with a dictionary?
a dictionary is not a sequence type, so can't use for loop, but you can loop over dict.keys() or dict.values(): or dict.items() - which reutrns a tuple of key, value pairs
How do you stop a while loop?
break
Math () Module - how do you find the the ceiling of x (the smallest integer greater than or equal to x)?
ceil(x)
How do you move a while loop onto next variable?
continue
How do you copy a dictionary?
copy() method - polEngDict.copy()
How do you define a function?
def functionName():
How can you delete from a list?
del lst[2:4], lst.remove(4), lst.pop(5)
How do you get a full list of entitiies in a module?
dir() Function, returns an alphabetically sorted list containing all entities' names (if module imported whole) available. for name in dir(math): print(name, end="\t")
How do you get a factoral of x?
factorial(x) → returns x! (x has to be an integral and not a negative)
How can you import all entities from a module as names?
from module import *
What does the return keyword do?
gets functions to return a value, and causes the immediate termination of the function's execution, and an instant return (hence the name) to the point of invocation
If you "print(9 % 6 % 2)", which side does it start? Left (9 % 6) or Right (6 % 2)?
left to right: first 9 % 6 gives 3, and then 3 % 2 gives 1;
How do you add to a list?
list.append(value) or list.insert(location, value) - second you can chose where in the list it is added.
How do you remove all a dictionarys items?
polEngDict.clear()
"What does the positional statement end="""" at the end of print do?
print(""My name is"", ""Python."", end="" "") " If end="" then no new lines are sent to output and all output is on one line.
What is the output of the input() function?
result of the input() function is a string (you mustn't use it as an argument of any arithmetic operation - as not an integer)
What function can you use to reverse a list?
reverse()
How can you round 'pi' up to 4 decimal places?
round((math.pi), 4) - or 3.1416
What function can you use to sort a list?
sort()
How to you convert a number, 505 to a string?
str() or str(505)
What does floor(x) give?
the floor of x (the largest integer less than or equal to x)
How can you truncate an number to a single integer?
trunc(x)
How can we shorten x = x * 2?
x *= 2 - Any two argument operator can be treated in this way +, /, %, -, **