Theory Python Questions
Theory Python Questions
1. Syntax errors
2. Run-time errors – error does not occur until program starts running. Error occurs when we
code an exceptional case or illegal statement. (10/0)
3. Semantic error –
Program will run but desired output will not be generated
A variable is a name given to a memory allocation. It is the basic unit of storage in a program.
1. Variable name must start with a letter or underscore, it cannot start with a number
2. A variable name can contain alpha-numeric characters and underscore. No special
characters are allowed.
3. Variable name are case-sensitive
4. Reserved keyword (built-in in python) cannot be used as variables
When more than one operator appears in an expression we use the rule of precedence which is
given by the acronym PEMDAS
1. Parenthesis
2. Exponential
3. Multiplication and Division
4. Addition
5. Subtraction
5. What are the different types of operators in python?
1. Arithmetic operators – used with numerical values to perform common mathematical operations
5. Identity operators - Identity operators are used to compare the objects, not if they are equal, but
if they are actually the same object, with the same memory location
Eg – is, is not
6. Membership operators – Used to check if a sequence belongs to a certain object or not (in and not
in)
Comments are the description written in the source code contained in some special characters
so that they get ignored by the compiler while running the code.
Usage –
In Python, break and continue statements can alter the flow of a normal loop. Loops iterate over
a block of code until test expression is false, but sometimes we wish to terminate the current
iteration or even the whole loop without checking test expression. The break and continue
statements are used in these cases
1. Break statement
The break statement terminates the loop containing it. Control of the program flows to
the statement immediately after the body of the loop. If it is inside a nested loop (loop
inside another loop), break will terminate the innermost loop.
2. Continue statement
The continue statement is used to skip the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next iteration.
8. How many numerical types are available in python? Explain them.
•int (signed integers): They are often called just integers or ints. They are positive or negative
whole numbers with no decimal point. Integers in Python 3 are of unlimited size. Python 2 has
two integer types - int and long. There is no 'long integer' in Python 3 anymore.
•float (floating point real values) : Also called floats, they represent real numbers and are
written with a decimal point dividing the integer and fractional parts
•complex (complex numbers) : are of the form a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an imaginary number). The real part of the number is
a, and the imaginary part is b. Complex numbers are not used much in Python programming
A list is an ordered set of values, where each value is identified by an index. The values that
make up a list are called its elements. Lists are similar to strings, which are ordered sets of
characters, except that the elements of a list can have any type. Lists and strings and other
things that behave like ordered sets are called sequences. Unlike strings, lists are mutable, which
means we can change their elements.
They will not be same for all strings. This is because the list will be joined from from where it was
split i.e the space. Therefore each character will come twice. However in a string without any
spaces it would give same output.
https://www.geeksforgeeks.org/differences-and-applications-of-list-tuple-set-and-dictionary-in-
python/
The pickle module implements binary protocols for serializing and de-serializing a
Python object structure. “Pickling” is the process whereby a Python object hierarchy is
converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte
stream (from a binary file or bytes-like object) is converted back into an object hierarchy.
15. Scope of variable
16. Difference between print and return
17. Tell the different modes of opening a file in python
Read and Write (‘r+’) : Open the file for reading and writing.