You are on page 1of 9

PYTHON REVISION TOUR - 1

1. An ordered set of instructions to be executed by a computer to carry out a specific task is called a
program. A program written in high level language is called source code.
2. The language that is used to write a specific set of instructions to carry out a specific task is
called a programming language. Python was developed by Guido Von Rossum.
3. Python uses a compiler for blocks of codes and interpreter for execution line by line. Interpreter
stops working when an error is encountered or when the program ends. Compilers work on the
program as a whole and generates error after full execution if any.
4. Features of python –
(i) Free, open source, object oriented, high-level language.
(ii) Interpreted language
(iii) Easy to understand as syntaxes are well defined.
(iv) Nested and loops are in form of index syntax
(v) Case sensitive
(vi) Rich library with many predefined functions
(vii) Platform independent, portable
5. The python interpreter is called as python shell. We can work in two following modes in python

Script mode Interactive mode


- Execution of block - Execution of single statements
- Statements can be saved - >>> type a statement to work and press
- File extension is .py enter, interpreter starts working.
- By default files are stored in Python - Convenient for testing single line code
installation Folder - Statements cannot be saved
- Convenient for executing block of codes
6. Smallest unit or item in a python program is called a token or a lexical unit. Tokens are
keywords, literals, identifiers, punctuators, and operators.
7. Keywords in python –
(i) Reserved words and have special meaning to the interpreter.
(ii) Must be written in the same way as is meant to be written.
(iii) To get keywords in python, import keyword and type print (keyboard.kwlist).

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

8. Identifiers in python – names given to a variable


(i) Must not use keywords.
(ii) Can start with any alphabet or underscore followed by alphanumeric values or
underscore

Page 1 of 9
(iii) Can be of any length but is advised to keep it short and meaningful for clarity.
(iv) No special character should be used.
9. Variables in python – are those identified by specific name in python.
(i) Refers to a value stored in python memory.
(ii) If strings are assigned enclose in quotes
(iii) For numeric variable assignment, quotes are not needed.
(iv) Assigning a variable is implicit type
(v) When another value is assigned to a variable, old one is
replaced by new one.
(vi) It is best to assign a variable before using it.
10. Comments in python –
(i) Are statements that are used to enhance the understanding of
the program by a user
(ii) Can be single line or multi line
(iii) Starts with #
(iv) Not executed by interpreter
11. The function id() in interactive mode returns the ID of the variable.
12. The function type() gives the type of datatype:

>>> num1 = 10 >>> var1 = True


>>> type(num1) >>> type(var1)
<class ‘int’> <class ‘bool’>
13. Barebones of a python program
a) Expressions – An expression is a set of operators and operands that together produce a
unique value when interpreted.
b) Statements – Which are programming instructions or pieces of code that a Python
interpreter can carry out.
c) Comments – Python comments start with the hash symbol # and continue to the end of the
line. Comments can be single line comments and multi-line comments.
d) Functions – A function is a block of code that only executes when called. You can supply
parameters—data—to a function. As a result, a function may return data.
e) Block or suit – In Python, a set of discrete statements that together form a single code block
are referred to as suites. All statements inside a block or suite are indented at the same level.
14. Datatypes – identifies the type of data an object belongs to and defines the operations that can
be performed on them.

(i) Numbers – stores numerical types only. (bool – returns true or false)
(ii) Sequences –
ordered
collection of
items where
each item is
indexed by an
integer.
(iii) Sets –
unordered collection of items separated by comma enclosed in {} and can’t have duplicate
entries.

Page 2 of 9
(iv) None is a special data type with a single value used to signify the absence of value in a
situation. None supports no special operations, and it is neither same as False nor 0.
(v) Mapping – unordered datatype. Currently, only one is available – dictionary
15. Variables whose values can be changed after they are created and assigned are called mutable.
16. Variables whose values cannot be changed after they are created and assigned are called
immutable.
17. When an attempt is made to update the value of an immutable variable, the old variable is
destroyed and a new variable is created by the same name in memory.
18. Python compares strings lexicographically, using ASCII value of the characters. If the first
character of both the strings are same, the second character is compared, and so on.
19. Deciding usage of datatypes –
(i) Frequent modifications – list
(ii) Tuples – need not change
(iii) To avoid redundancy – sets
(iv) Modified often – dictionary
20. An operator is used to perform specific mathematical or logical operation on values. The values
that the operators work on are called operands.

Arithmetic operators
Operato Function Examples
r
+ Adds two operators and is also used to concatenate >>> num1 = 5
strings >>> num2 = 6
>>> num1 + num2
11
>>> str1 = "Hello"
>>> str2 = "India"
>>> str1 + str2
'HelloIndia'
- Subtracts two operators >>> num1 = 5
>>> num2 = 6
>>> num1 - num2
-1

* Multiplies two values or repeats a string accordingly >>> num1 = 5


>>> num2 = 6
>>> num1 * num2
30
>>> str1 = 'India'
>>> str1 * 2
'IndiaIndia'
/ Divides two operands and gives quotient >>> num1 = 8
>>> num2 = 4
>>> num2 / num1
0.5
% Divides two operands and gives remainder >>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3
// Divides two operands and gives the whole number part >>> num1 = 13

Page 3 of 9
alone. (integer division) >>> num2 = 4
>>> num1 // num2
3
>>> num2 // num1
0
** Exponents >>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81
Relational operators
== If values of two operands are equal, then true >>> num1 == num2
False
>> str1 == str2
False
!= If two operands are not equal then true >>> num1 != num2
True
>>> str1 != str2
True
>>> num1 != num3
False
> Greater than >>> num1 > num2
True
>>> str1 > str2
True
< Less than >>> num1 < num3
False
>>> str2 < str1
True
<= Less than or equal to >>> num1 < num3
False
>>> str2 < str1
True
>= Greater than or equal to >>> num1 >= num2
True
>>> num2 >= num3
False
>>> str1 >= str2
True
Assignment operator
= Assign a value >>> num1 = 2
>>> num2 = num1
>>> num2
2
>>> country = 'India'
>>> country
‘India’
+= Adds both operands and result is stored in L variable >>> num1 = 10
>>> num2 = 2
>>> num1 += num2
>>> num1
12

Page 4 of 9
-= Subtracts two operands and stores value in the L variable >>> num1 = 10
>>> num2 = 2
>>> num1 -= num2
>>> num1
8
*= Multiplies two operands and stores value in the L >>> num1 = 2
variable >>> num2 = 3
>>> num1 *= 3
/= Divides and assigns quotient to left >>> num1 = 6
>>> num2 = 3
>>> num1 /= num2
>>> num1
2.0
%= Modulus operation and stores in L variable >>> num1 = 7
>>> num2 = 3
>>> num1 %= num2
>>> num1
1
//= Performs floor division and stores in L variable >>> num1 = 7
>>> num2 = 3
>>> num1 //= num2
>>> num1
2
**= Exponentiation and stores it in L variable >>> num1 = 2
>>> num2 = 3
>>> num1 **= num2
>>> num1
8
Logical operators
and If both are true, result is true >>> True and True
True
>>> num1 = 10
>>> num2 = -20
>>> bool(num1 and num2) True
or If any 1 is true, then result is true >>> True or True
True
>>> True or False
True
>>> bool(num1 or num3)
True
>>> False or False
False
not Used to reverse logical state >>> num1 = 10
>>> bool(num1)
True
>>> not num1
>>> bool(num1)
False

Identity operators
is Evaluates True if the variables on either side of the >>> num1 = 5

Page 5 of 9
operator point towards the same memory location and >>> type(num1) is int
False otherwise True
>>> num2 = num1
>>> id(num1)
1433920576
>>> id(num2)
1433920576
>>> num1 is num2
True
is not Evaluates to False if the variables on either side of the >>> num1 is not num2
operator point to the same memory location and True False
otherwise
Membership operators
in Returns True if the variable/value is found in the >>> a = [1,2,3]
specified sequence and False otherwise >>> 2 in a
True
>>> '1' in a
False
not in Returns True if the variable/value is not found in the >>> a = [1,2,3]
specified sequence and False otherwise >>> 10 not in a
True
>>> 1 not in a
False

21. Expressions –
(i) An expression is defined as a combination of constants, variables, and operators.
(ii) An expression always evaluates to a value.
(iii) A value or a standalone variable is also considered as an expression but a standalone
operator is not an expression.
22. Precedence of operators –

23. A statement is a unit of code that the Python interpreter can execute.

Page 6 of 9
24. The input() function prompts the user to enter data. It accepts all user input as string. The user
may enter a number or a string but the input() function treats them as strings only. The syntax
for input() is: input ([Prompt])
25. The input() takes exactly what is typed from the keyboard, converts it into a string and assigns it
to the variable on left-hand side of the assignment operator (=).
26. Python uses the print() function to output data to standard output device — the screen. The
syntax for print() is: print(value [, ..., sep = ' ', end = '\n

27. Explicit conversion, also called type casting happens when data type conversion takes place
because the programmer forced it in the program.
28. The general form of an explicit data type conversion is: (new_data_type) (expression). With
explicit type conversion, there is a risk of loss of information.
29. Example - chr(x): Converts ASCII value of x to character, ord(x): returns the character associated
with the ASCII code x.
30. Implicit conversion, also known as coercion, happens when data type conversion is done
automatically by Python and is not instructed by the programmer.
31. The process of identifying and removing mistakes, also known as bugs or errors, from a program
is called debugging. Errors occurring in programs can be categorised as:
i) Syntax errors
ii) Logical errors
iii) Runtime errors

Syntax errors Logical errors (semantic error) Runtime errors


- If any syntax error is - A logical error is a bug in - A runtime error causes
present, the interpreter the program that causes it abnormal termination of
shows error message(s) to behave incorrectly. program while it is
and stops the execution - produces an undesired executing.
there. output but without abrupt - Runtime error is when the
- For example, parentheses termination of the statement is correct
must be in pairs, so the execution of the program. syntactically, but the
expression (10 + 12) is - it is sometimes difficult to interpreter cannot execute
syntactically correct, identify these errors. it.
whereas (7 + 11 is not due - While working backwards - Runtime errors do not appear
to absence of right from the output of the until after the program
parenthesis. program, one can identify starts running or executing.
what went wrong

Page 7 of 9
32. For imaginary numbers, z.real and z.imag returns real part and imaginary part of a + bj.
33. eval() – string is taken as number, returns numeric result. If argument is not a string or if it
cannot be evaluated as number, it gives error.
34. The order of execution of the statements in a program is known as flow of control. The flow of
control can be implemented using control structures. Python supports two types of control
structures—selection and repetition.

35. The syntax of if statement is: 37. The syntax of if..elif (elif means else..if)
if condition:
if condition:
statement(s)
statement(s)
elif condition:
36. The syntax of if else is:
statement(s)
if condition:
elif condition:
statement(s)
statement(s)
else:
else:
statement(s)
statement(s)
38. Leading whitespace (spaces and tabs) at the beginning of a statement is called indentation. In
Python, the same level of indentation associates statements into a single block of code.
39. The interpreter checks indentation levels very strictly and throws up syntax errors if indentation
is not correct. It is a common practice to use a single tab for each level of indentation.
40. Repetition of a set of statements in a program is made possible using looping constructs.
41. For loop –
(i) Iterate over a range of values or a sequence.
(ii) Executed for each of the items in the range. These values can be either numeric or can
be elements of a data type like a string, list, or tuple.
(iii) With every iteration of the loop, the
control variable checks whether each of
the values in the range have been traversed or not.
(iv) When all the items in the range are exhausted, the statements within loop are not
executed; the control is then transferred to the statement immediately following the for
loop.
(v) While using for loop, it is known in advance the number of times the loop will execute.
42. The range() is a built-in function in Python. Syntax of range() function is: range([start], stop[,
step])
43. It is used to create a list containing a sequence of integers from the given start value upto stop
value (excluding stop value), with a difference of the given step value.
44. While loop –
(i) The while statement executes a block of code repeatedly as long as the control condition
of the loop is true.
(ii) The control condition of the while loop is executed before any statement inside the loop
is executed.
(iii) After each iteration, the control condition is tested again and the loop continues as long
as the condition remains true.

Page 8 of 9
(iv) When this condition becomes false, the statements in the body of loop are not executed
and the control is transferred to the statement immediately following the body of while
loop.
(v) If the condition of the
while loop is initially
false, the body is not
executed even once.
45. The break statement
alters the normal flow of
execution as it
terminates the
current loop and resumes
execution of the
statement following
that loop.
46. When a continue statement is
encountered, the

control skips the execution of remaining statements inside the body of the loop for the current
iteration and jumps to the beginning of the loop for the next iteration.
47. A loop may contain another loop inside it. A loop inside anoth er loop is called a nested loop

Page 9 of 9

You might also like