Class XII - L1 - Python Revision Tour 1
Class XII - L1 - Python Revision Tour 1
1
1.1 INTRODUCTION
• Python programming language was developed by Guido Van Rossum in early
1990s, has become very popular among beginners and as well as the developers.
• A python program may contain various components like expressions, statements,
comments, functions, blocks and indentation.
2
c. Literals / values:
• Literals are data items that have a fixed/ constant values.
• Python allows several kinds of literals, which are:
1. String literals:
• string literal is a sequence of characters surrounded by
quotes. String literals can be either be single line string or
multi-line string.
• Single line string must terminate in one line. The closing
quotes should be on the same line as that of the opening
quotes.
• Multi-line string are string spread across multiple lines.
2. Numeric literals:
• Numeric literals are numeric values and these can be one :
1. Int (signed integer)
2. Floating point literals.
3. Boolean literals
4. Complex number literals
3. Boolean literals:
• A boolean literal in python is used to represent one of the
boolean values i.e., true or false
• A boolean literal can either have value as true or false.
3
d. Operators:
• Operators are tokens that trigger some computation/ action when
applied to variables and other objects in an expression.
• The operators can be:
1. Arithmetic operators (+, -, *, /, %, **, //)
2. Bitwise operators (&, ^, |)
3. Shift operators (<<, >>)
4. Identity operators ( is, is not)
5. Relational operators(<, >, <=, >=, ==, !=)
6. Logical operators (and, or)
7. Assignment operators( =)
8. Membership operators( in, not in)
9. Arithmetic-assignment operators (/=, +=, -=, */, %=, **=, //=)
e. Punctuators:
• Punctuators are symbols that are used in programming languages to organize
sentences, structures and indicate the rhythm and emphasis of expressions,
statements and program structure.
• Most common punctuators of python programming are
‘, “, #, \, ( ), {}, [], @, :, ., `, =.
4
1.4 VARIABLES AND ASSIGNMENTS:
• Variables represents labelled storage locations, whose values can be manipulated
during program run.
• In python, to create a variable, just by assigning to its name the value of appropriate
type.
• Example: student = ‘jacob’, age = 16.
Dynamic typing:
• A variable pointing to a value of a certain type, can be made to point to a
value/object of different type, this is called dynamic typing.
• Dynamic typing means a variable can hold values of different types at different
times.
• Example:
x=10
print (x)
10
x= “hello world”
print (x)
hello world
Multiple assignment:
• Assigning same value to multiple variables: we can assign same value to multiple
variables in a single statement, e,g.,
A= b= c =10
It will assign value 10 to variables a, b, c.
• Assigning multiple values to multiple variables: we can assign multiple values to
multiple variables in single statement, e,g.,
x, y, z = 10, 20, 30
It will assign the values in order wise, i,e., first variable first value, second variable
second value and third variable third value. x=10, y=20, z=30.
5
• Example: name = input (‘ what is your name ?’)
• The input() function always returns a value of string type. Python offers two
function int () and float().
• While inputting integer values using int() with input(), we should make sure that
the values being entered must be int type compatible. Same with the float() with
input().
6
1. Data types for number:
a. Python offers data types to store and process different types of numeric data
b. Integers: there are two types of integers in python. Signed integer and
booleans.
c. Floating point numbers: this type represent machine-level double precision
floating point numbers.
d. Complex numbers: python represents complex numbers in the form A+Bj.
Complex numbers are a composite quantity made of two parts: the real and
imaginary part, both of which are represented as float values.
4. Tuples:
a. Tuples are represented as group of comma-seperated values of any data type
within the parentheses.
b. Example: p = (1, 2, 3, 4), r = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
5. Dictionaries:
a. The dictionary is an unordered set of comma-seperated key : value pairs,
within { }, with the requirement that within a dictionary, no two keys can be the same.
b. Example: {‘a’:1, ‘e’:2, ‘i’:3, ‘o’:4, ‘u’:5}.
7
1.7 MUTABLE AND IMMUTABLE TYPES:
Immutable types:
• The immutable types are those that can never change their values in place.
• Integers, floating point numbers, booleans, string, tuples are immutable types in
python.
• In immutable types, the variable names are stored as references to a value
object Each time change of the value address of memory also changes.
Mutable types:
• Mutability means that in the same memory address, new value can be stored as
and when user wants.
• The types that do not support this property are immutable types.
• The mutable types are those values can be changed in place.
• Lists, dictionaries and sets are the three mutable types in python.
1.8 EXPRESSIONS:
• An expression in python is any valid combination of operators, literals and
variables.
• The expressions in python can be of any type: arithmetic expressions, string
expressions, relational expressions, logical expressions, etc.
• Arithmetic expressions involves numbers and arithmetic operators.Example:
2+5**3, -8*6/5, etc
• An expression having literals and/or variables of any valid type and relational
operators is a relational expression.
Example: x > y, y <= z, z != q, etc
• An expression having literals and/or variables of any valid type and logical
operators is a logical expression.
Example: a or b, a and not b.
• Python provides two string operators + and *, when combined with string
operands and integers, from string expression.
Example: “and” + “then”, “and” * 2
8
Evaluating Arithmetic expression:
Rules for evaluating arithmetic expressions:
• Determines the order of evaluation in an expression considering the operator
precedence.
• As per the evaluation order, for each of the sub-expression
i. Evaluate each of its operands or arguments.
ii. Performs any implicit conversions.
iii. Compute its result based on the operator.
iv. Replace the sub-expression with the computed result and carry on the
expression evaluation.
v. Repeat till the final result is obtained.
In a mixed arithmetic expression, python converts all operands up to the type
of the largest operand. If both arguments are standard numeric types, the
following are applied:
• If either argument is a complex number, the other is converted to complex.
• Otherwise, if either argument is a floating point number, the other is converted
to floating point.
• No conversion if both operands are integer.
9
• While evaluating, python minimizes internal work by following the rules:
1. In or evaluating, python only evaluates the second argument if the first one is
falsetval.
2. In and evaluation, python only evaluates the second argument if the
first one is truetval.
Type casting:
• An explicit type conversion is user-defined conversion that forces an
expression to be of specific type. The explicit type conversion is also known as
type casting.
• Type casting in python is performed by <type>( ) function of appropriate
data type. Syntax: < datatype > (expression)
• Where <datatype> is the data type to which user want to type cast the
expression.
Example: if we have ( a = 3 and b = 5.0) then
int( b) will cast the data-type of the expression as int.
10
Some of the math library functions:
Prototype
S No. Function Description Example
(General Form)
The ceil () function
math ceil (1.03) gives 2.0
1. ceil Math cell (num) returns the smallest
math ceil (-103) gives – 10.
integer root less than
The sqrt ( ) function
returns the square root
2. sqrt Math. Sqrt (num) math sqrt (81.0) gives 9.0
of num. if num < 0,
domain error occurs.
The exp ( ) function re-
turns the natural loga- math. Exp (2.0) gives the
3. exp Math exp (arg)
rithm e raised to the org value of e2
power.
math. Fabs (1.0) gives 1.0
The fabs ( ) function re-
4. fabs Math. Fabs (num) turns the absolute value Math. Floor (-1.03)
of num
gives – 2.0
The floor ( ) function
returns the natural oga-
math. Floor (1.3) gives 1.0
rithm for num. A domain
5. floor Math. Floor (num) error occurs if num is Math. Floor (-1.03)
negative and a range
gives – 2.0
error occurs if the argu-
ment num is zero.
The log () function re-
turns the natural loga- math. Log (1.0) gives the nat-
11
The log 10 ( ) function
returns the base 10
logarithm for num. a
Math. Log 10 math. Log 10 (1.0) gives base
7. log 10 domain error occurs if
(num) 10 logarithm for 1.0
num is negative and a
range error occurs if
the argument is zero
The pow ( ) function
returns base raised to
exp power i. e., base math. Pow (3.0, 0) gives value
12
1.9 STATEMENT FLOW CONTROL:
• Statements can be executed sequentially, selectively or iteratively. Every
programming language provides constructs to support sequence, selection or
iteration.
• A conditional is a statement set which is executed, on the basis of result of a
condition.
• A loop is a statement set which is executed repeatedly, until the end condition is
satisfied.
Compound statement:
• A compound statement represents a group of statements executed as a unit.
• The compound statements of python are written in a specific pattern:
< compound statement header > :
< indented body containing multiple simple and/or compound statements >
• The conditionals and the loops are compound statements, they contain other
statements.
• For all the compound statements:
a. The contained statements are not written in the same column as the control
statements rather they are indented to the right and together they are
called a block.
b. The first line of compound statement, i.e., its header contains a colon ( : )
at the end of it.
• Example:
num1 = int(input(“enter the number 1”))
num2 = int(input(“enter the number 2”))
if num2 < num1:
t = num2 * num1
t = t +10
print(num2, num1, t)
Simple statements:
• Compound statements are made of simple statements. Any single executable
statement is a simple statement in python.
13
Empty statement:
• The simplest statements is the empty statements i.e., a statement which does
nothing. In python and empty statement is the pass statement.
• Whenever python encounters pass statement, python does nothing and moves to
next statement in the flow control.
Test
Expression
?
body of if
• Example:
if grade == ‘A’ :
print(“ congratulations ! you did well”)
14
• Flow chart:
Test
Expression False
?
True
body of if
Example:
if a > = 0:
print( a, “is zero or a positive number”)
else:
Print(a, “ is a negative number”)
The if- elif conditional statement:
• If the user wants to check a condition after or when control reaches else
i.e., condition test in the form of else if.
• To serve such conditions, python provides if-elif and if-elif-else statements.
• Syntax:
if < conditional expression > :
Statement
[statements]
elif <conditional expression >:
Statement
[statements]
Nested if statements:
• Python also supports nested-if form of it. A nested if is an if that has another
if in its if’s body or in else body or in elif body.
• Example:
if x < y and x < z:
if y < z:
min, mid, max = x, y, z
else:
min, mid, max = x, z, y
elif y < x and y < z :
if x < z:
min, mid, max = y, x, z
else:
min, mis, max = y, z, x
15
Storing conditions:
• The conditions being used in code are complex and repetitive. In such cases
to make programmore readable, named conditions can be used, we can store
conditions in a name and thenuse that name conditional in the if statement.
• Example: consider
b, c = 2, 3
#Store condition in a name called all.
all = a == 1 and b == 2 and c == 3
#Test variable.
if all:
Print(“condition fulfilled”)
#Use it again.
if all:
Print(“condition fulfilled again”)
16
1.12 JUMP STATEMENT:
Break statement:
• A break statement terminates the very loop it lies within.
• A break statement skips the rest of the loop and jumps over to the statement following
the loop.
• Example:
if b == 0 :
Print(“ division by zero error ! Aborting !”)
Break
else :
c = a/b
print(“quotient = “, c)
Continue statement:
• The continue statement forces the next iteration of the loop to take place unlike
thebreak statement, skipping any code in between.
• The continue statement skips the rest of the loop statements and cause the next
iteration of the loop to take place.
17
Nested loops:
• A loop may contain another loop in its body. This form of a loop is called
nested loop.
• In a nested loop the inner loop must terminate before the outer loop.
• Example:
for i in range (1, 6) :
for j in range (1, i) :
print(“*”, end = ‘ ‘)
print( )
• In nested loops, a break statement will terminate the very loop it appears in. If
break statement is inside the inner loop then only the inner loop will terminate and
outer loop will continue.
18