python basics Flashcards
An algorithm and what is representaiton
and what are the steps of algorithm
that solves the problem by use of the
representation. A sequence of unambiguous
instructions for solving a problem, for obtaining a
required output for any legitimate input in a finite
amount of time.
that captures all the relevant
aspects of the problem.
design, analysis, planning, implementation,
what is a complier and intrepreter
Compiler: Converts entire high-level language programs to machine language at once, takes less time
An error in code makes the whole compliation stop
Interpreter: Converts high-level language to machine language line by line and
execute instructions in place of CPU, if there is an error the code stops executing, takes more time to execute, interpreter gives a more detailed error message, and more accurate debugging
binary system digits and decimal systems and hexadecimal, and coversion
0,1
0,1,2,3,4,5,6,7
0-9 (num is terms of binary)
10-15 in term of (A,C,D,E,F)=> binary of 10-15
binary => hexa
0-10 numbers,
binary to decimal (8421 method) hexa is base of 16
hexadecimal to decimal
Main memory and secondary memoey
low level, middle level, high level
A Memory where currently executing
programs reside
It is volatile, the contents are lost
when the power is turned off.
Provides long-term storage of programs and data. Non-volatile, the contents are retained when power is turned off.
low level, middle, high level
directly understood by the machine. Much harder to code with, but has the fastest performance
Machine language
Middle level
provides some basic data structures and definitions while still
maintaining direct interaction with the machine. Still somewhat hard to code
with, but has fast performance. C, C++
High level
Focuses on ease of use and provides many programming structures
by default. Worse performance but very easy to code and develop with. Python, JavaScript
What is docstring?
special type of comment
(“”” or ‘’’)
what is a syntax and what is a semantics error and which will show error in complier
a set of characters and the acceptable sequences
(arrangements) of those characters.
a type of logic error in computer programming that occurs when a program’s meaning is incorrect:
But program runs successfully
only syntax errors will displayed on compliers
Characteristics of print function
Can display/print anything in the world.
* Can take any type of arguments.
Values could be Numbers, Boolean, strings, collections, expressions, functions,
etc.
* Can take any number of arguments.
* Has the capability to evaluate the expression.
* Formatting is possible to some extent using the keyword separators – sep & end
what is the code to change the separation from (space) to :
And what is sep
And what is end function
and what removes next like using end
print(10,12,14, sep = “:”)
10:12:14
string inserted between values, default a space.
end function
string appended after the last value, default a newline or in this case all numbers are in the same line
print() can be used here
print(10,12,14, end = “ ”)
print(16,18)
10 12 14
16 18
variable rules
- can begin with a-z or A-Z. not with numbers
- 0-9 or a special character and Quotes are not allowed.
- Spaces are not allowed as part of an identifier
- underscores are allowed but but we do not prefer to use _ as the first character as it it has a special
meaning.
no special characters allowed - can only contain alpha numeric characters and underscores
what does an id() do
what is the function ‘‘is’’
returns the identity of an
object: like a serial number
Commonly used to check if two variables or objects are similar
function is
The is keyword is used to test whether two
variables belong to the same object.
The test will return True if the two objects are the same else it
will return False
what is a datatype and what is the significance and what is function that returns the type of the object
what are relational operators and example
Memory associated with it
Operations that can be performed on it.
Range of values allowed in it
type(object)
relational operators
Relational expressions are a type of Boolean expression, since they evaluate
to a Boolean result
3 > 2 > 1
(3>2) and (2>1)
what is Truncation Division and symbol left and right division
boolean logical operators: false and true
Membership operator (in and not in)
// (coverts decimal point into whole number)
operands can be both integer and floating points
ex: 5//2 = 2
5//2.0=2.0
left right division will return the answer in float or one decimal place
boolean false and true
False Values: 0 , ‘’ (Empty String), [] , {} , ()
True Values: Values: non – Zero numbers numbers , Non Empty String
Membership operator
membership operators can also be used to check if a given string occurs
within another string
> ‘E’ in ‘PES’ => true
‘F’ not in ‘PES => false
Bitwise operations &, |, ^, «,», ~ ,%
and meaning
characters that perform actions on data at the bit level, or binary level
AND, OR| (if val is 0 other value is considered true),
^ Exclusive OR: result is 1 if and only if one of the bits is 1
«_space;
LEFT SHIFT, multiply by 2^n for each left shift
» (ans=number of 1s on the binary)
RIGHT SHIFT divide by 2 for each right shift
~
ONE’S COMPLIMENT change 0 to 1 and 1 to 0
-(n +
% Check if the least significant bit is 1 (odd number)
print(var)
what is addition, subtraction, multiplication, division in bitwise function, division, modulus, Exponentiation in bitwise function
a += b
a = a + b
a -= b
a = a - b
a *= b
a = a * b
a /= b => floating point
a = a / b ex 4.0
a //= b
a = a //b
a %= b (division, modulus, Exponentiation in bitwise function)
a = a % b
a **= b
a = a ** b
what is the order precedence
what is operator associativity and the exception
Boolean logical operators (Short Circuit Evaluation)
it follows PEMDAS, for ex multiplication has more preference than addition
Operator associativity
Operators in the same box group left to right
Operators in the same box group left to right (except for exponentiation, which groups
from right to left).
Ex: 2 ** (3 ** 2) # 2 ** 9 = 512
Solve ans 3
result = 2 + 3 * 4 ** 2 / 8 - 5
print(result)
Boolean short circuit
logical and: if the first operand evaluates to false, then regardless of the value of the
second operand, operand, the expression expression is false
logical or: if the first operand evaluates to true, regardless of the value of the second operand, the expression is true.