1 Python Revision Tour 1 1
1 Python Revision Tour 1 1
Expressive language
Interpreted Language
Its completeness
Cross-platform Language
Free and Open source
Keywords
Identifiers (Names)
Literals
Operators and
Punctuators
1) The smallest individual unit in a program is known
as ______
1) A token is also called as _________
2) There are _____ types of tokens.
3) List the types of tokens.
Example:
if a > 10 :
KEYWORDS
s = ‘ ‘ ‘ Hello
Welcome to class XII ’ ’ ’
CHARACTERS
Carriage Return -- ( \ r )
Horizontal Tab – ( \ t )
Character with 16 bit Hex value ( \ uxxxx)
Character with 32 bit Hex value ( \Uxxxxxxxx)
ASCII Vertical Tab – (\v)
Character with Octal Value – (\ooo)
Character with Hex Value – (\xhh)
New line character – ( \n)
Numeric literal
Integer literals
Decimal form – will begin with digits 1-9
e.g. 1234, 4100
Octal form – an integer beginning with 0o(zero followed
by letter o) - e.g. 0o35, 0o77
Note: remember that for Octal, 8 and 9 are invalid digits
Hexadecimal form – an integer beginning with 0x(zero
followed by x) - e.g. 0x73, 0xAF
Note: remember that valid digits/letters for hexadecimal
numbers are 0-9 and A-F
Floating point literals or real literal floats – are written with
decimal point dividing the integer and fractional parts are
numbers having fractional parts. e.g. 0.17E5, 3.E2, .6E4
Complex number literals – are of the form a + bj where a and
b are floats and j(or J) represents −1 (which is an imaginary
number)
Boolean literal
True
False
Special literal
None
OPERATORS
Types of operators:
Arithmetic operators +, -, *, /, //, %, **
Relational operators >, <, >=, <=, = =, !=
Logical operators and, or, not
Assignment operator =
Bitwise operator &, ^, |
Shift operator <<, >>
Identity operators is, is not
Membership operators in, not in
Arithmetic assignment operator /=, +=, -=, *=, %=, **=,
//=
EXPRESSIONS
An expression in Python is any valid combination of
operators, literals and variables.
TYPES OF EXPRESSIONS:
Arithmetic expression
Relational expression
Logical expression
OPERATORS THEIR ASSOCIATIVITY & PRECEDENCE
Highest
OPERATOR DESCRIPTION ASSOCIATIVITY
2) 12*(3%4)//2+6 2) 12*(3%4)//2+6 = 24
3) 2 ** 3 ** 2 3) 2 ** 3 ** 2 = 512
4) 7 // 5 + 8 * 2 / 4 – 3 4) 7 // 5 + 8 * 2 / 4 – 3 = 2.0
5) 8 * 3 + 2**3 // 9 – 4 5) 8 * 3 + 2**3 // 9 – 4 = 20
LINK
PUNCTUATORS
‘ “ # \ ( ) [ ] { } @ , : . =
DYNAMIC TYPING VS STATIC TYPING
print(‘hello’,’welcome’,’to’,’Alpha’,sep=‘#’)
Will be displayed as: hello#welcome#to#Alpha
print(‘ Hello!)
print(“How are you?’)
Will be displayed as: Hello!
How are you?
print(‘hello’,’welcome’,’to’,’Alpha’,sep=‘#’)
Will be displayed as: hello#welcome#to#Alpha
Immutable types: are those that can never change their value in
place. In python following types are immutable: integers, float,
Boolean, strings, tuples.
Mutable types: are those that can change their value in place.
In python the following are mutable types: Lists, Dictionaries,
Sets.
VARIABLE INTERNALS
Python is an object oriented language.
So every thing in python is an object.
An object is any identifiable entity that have some
characteristics/properties and behavior.
Every python object has three key attributes associated with it:
1. type of object
2. value of an object
3. id of an object
Type of an object determines the operations that can be
performed on the object.
Example:
>>> a=100
>>> type(a) returns <class ‘int’>
>>> type(100) returns <class ‘int’>
>>> name="Jaques"
>>> type(name) returns <class ‘str’>
TYPE CASTING
The explicit type conversion is also known as Type Casting.
An explicit type conversion is user-defined conversion that
forces an expression to be of specific type.
SYNTAX :
data type_to_convert(expression)
For example:
str = “100” # String type
num = int(str) # will convert the string to integer type
MATH LIBRARY FUNCTIONS
import math # to include math library
Important functions:
ceil(), sqrt(), exp(),
fabs(), floor(), log(),
log10(), pow(), sin(),
cos(), tan()
TYPES OF STATEMENT IN PYTHON
Statements are the instructions given to computer to perform
any task.
Task may be simple calculation, checking the condition or
repeating action.
Python supports 3 types of statement:
1) Empty statement
2) Simple statement
3) Compound statement
EMPTY STATEMENT
EXAMPLE:
bonus = 0
sale = int(input("Enter Monthly Sales :"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " , bonus)
if with else:
if with else is used to test the condition. If the condition is
True it perform certain action and alternate course of action if
the condition is false.
Syntax:
if <condition>:
Statements
else:
Statements
EXAMPLE:
1) break
2) continue
1) break statement in python is used to terminate the containing loop for any
given condition.
2) Program resumes from the statement immediately after the loop. Continue
statement in python is used to skip the statements below continue
statement inside loop and forces the loop to continue with next value.
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)
The above code produces the following output
12345
Loop Over
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = ‘ ‘)
print(‘Loop Over’)
The above code produces output
1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19
Loop Over
when the value of i becomes divisible from 6, condition will becomes True and loop
will skip all the statement below continue and continues in loop with next value of
iteration.
else with while
Loop in python provides else clause with loop also which will execute when the loop
terminates normally i.e. when the test condition fails in while loop or when last value
is executed in for loop but not when break terminates the loop.
Example
i=1
while i<=10:
print(i)
i+=1
else:
print("Loop Over")
Output
1 2 3 4 5 6 7 8 9 10
Loop Over
else with for
Example
names=["allahabad","lucknow","varanasi","kanpur","agra","ghaziabad",
"mathura","meerut"]
city = input("Enter city to search: ") for c in
names:
if c == city:
print(“City Found") break
else:
print("Not found") Output:
Enter city to search : varanasi City
Found
Enter city to search : unnao Not
found