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.
what is selection control and iterative control
sequential control
also known as Decision control statements or branching
statements.
The selection statement allows a program to test several conditions and execute
The set of statements following a header in Python is called a suite
The statements of a given suite must all be indented the same amount
some examples: If, if-else,if-elif-else
iterative control
- A control statement providing the repeated execution of a set of instructions
aka loops
Sequential control
Sequential statements are a set of statements whose execution process happens
in a sequence
while loop
valid indentation
- Repeatedly executes a set of statements based on the provided expression (condition).
As long as the condition of a while statement is true, the statements within the loop are (re)executed (re)executed. - Once the condition becomes false, the iteration terminates and control continues withthe first statement after the while loop
idealy 4 spaces should be present but both if and else statements spacing must match
and spacing must match adajcent statements
what is ord and char
how to print random numbers
range(4,0) correct
what are some basic errors they will ask you about
is there any limit on size of int
SyntaxError
ord gives ASCII value and char converts ASCII to char
import random
random.randrange()
only has ascending, not descending if range(4,0), it must have range(4,0,-1)
decrements also
ERRORS
print(x)
NameError => name ‘x’ is not defined
#IndentationError
print(x+y)
#TypeError
data types
#ValueError
limit on size of int
There is no practical limit to the size of an integer in Python.
pyramid structure code
for i in range(4,0,-1): # Loop starts from 4, goes down to 1
for j in range(i, 5): # Inner loop starts at ‘i’ and ends at 4
print(j, end=” “) # Print each number in the row
print() # Move to the next line after each row
how to reverse list
type casting
Which of the following operators has the lowest precedence?
not
%
and
**
[:-1]
x=2
The expression float(x) + 4.5 is an example of type casting (also known as type conversion), specifically explicit type casting.
following operators has the lowest precedence
and has the lowest precedence.
What are the advantages and limitations in python?
brute force approach and
what is limits of computational problem solving
Ease of Learning and Use, Python is used in a wide range of fields,
DIS:
Slower Execution Speed,Python is known to use more memory
BRUTE FORCE
A brute force approach is a straightforward problem-solving technique where all possible solutions or combinations are tried until the correct one is found.
Time and space required grow with input size. Some problems (e.g., NP-hard) are infeasible to solve for large inputs.
what is literal?
A literal in programming is a fixed value that is directly written into the code, representing a constant value of a specific type. Literals are used to assign values to variables or as direct values in expressions.
and types of literals
Integer literal
floating-point literal
boolean literal
string literal
none literal
complex literal