Python - Revision Tour
Python - Revision Tour
com
INTRODUCTION
TOKENS
BAREBONES OF PYTHON
VARIABLES AND ASSIGNMENT
INPUT & OUTPUT
DATA TYPES
MUTABLE AND IMMUTABLE TYPESS
EXPRESSION
FLOW OF CONTROL
JUMP STATEMENT
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Punctuators
Identifier
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Keywords are the reserved words and have special meaning for
python interpreter. Every keyword is assigned specific work and it
can be used only for that purpose.
A partial list of keywords in Python is
String Size
„\\‟ 1
„abc‟ 3
„\ab‟ 2
“Meera\‟s Toy” 11
“Vicky‟s” 7
• You can check these size using len() function of Python. For example
• >>>len(„abc‟) and press enter, it will show the size as 3
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
>>> isMarried=True
>>> type(isMarried)
<class 'bool'>
>>> salary=None
>>> type(salary)
<class 'NoneType'>
• Operators can be
Type of Operators Symbols
Arithmetic +, -, *, /, %, **, //
Relational >, <, >=, <=, ==, !=
Logical and, or
Identity is, is not
Assignment =
Membership in, not in
Arithmetic +=, -=, *=, /=, %=, **=, //=
Assignment
Bitwise &, ^, |, <<, >>
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
B=A+20 Expressions
Inline Comment
C=A+B
if(C>=100) #checking condition
print(“Value is equals or more than 100”)
else: Block
Indentation
Now you can see that In python, each time you assign new
value to variable it will not use the same memory address
and new memory will be assigned to variable. In python the
location they refer to changes every time their value
change.(This rule is not for all types of variables)
string:KV OEF
Output will be
10 40 11
y, y = 10, 20
Example 4
r = int(input("Enter Radius "))
print("Area of circle is ",3.14*r*r)
Example 5
print(„Amar‟,‟Akbar‟,‟Anthony‟)
Output will be : Amar Akbar Anthony
space will be automatically inserted between different
values as separator because the default value of ‘sep’
parameter is space
Example 6
Print(„Amar‟,‟Akbar‟,‟Anthony‟, sep=„**‟)
Output will be : Amar**Akbar**Anthony
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Note:
Be default each print statement appends a new line character
after the printed value. The default value of „end‟ parameter
is “\n”
Example
print(“Learning Python”)
print(“Developed by Guido Van Rossum”)
Output
Learning Python
Developed by Guido Van Rossum
Note:
We can change the value of end to any other value.
Example
print(“Learning Python ”,end=““)
print(“ Developed by Guido Van Rossum”)
Output
Learning Python Developed by Guido Van Rossum
• Data type in Python specifies the type of data we are going to store in any
variable, the amount of memory it will take and type of operation we can
perform on a variable. Data can be of many types e.g. character, integer,
real, string etc.
• Python supports following data types:
Numbers ( int, float, complex)
String
List
Tuple
Dictionary
• From the name it is very clear the Number data types are used to
store numeric values. Numbers in Python can be of following types:
(i) Integers
a) Integers(signed)
b) Booleans
(ii) Floating point numbers
(iii) Complex Numbers
• Floating point number are mainly used for storing values like
distance, area, temperature etc. which have a fractional part.
• Floating point numbers have two advantage over integers:
they can represent values between the integers
they can represent a much greater range of values
• But floating point numbers suffers from one disadvantage also:
Floating point operations are usually slower than integer
operations.
• >>> student={'Roll':1,'Name':"Jagga",'Per':91.5}
• >>>print(student)
• >>> print(student['Per'])
• 91.5
• >>> val={1:100,2:300,4:900} # Key name can be string / numeric
• >>> print(val[1])
• 100
• Dictionary is mutable. i.e. We can modify dictionary elements.
• >>>val[2]=1000
• >>>print(val) # {1: 100, 2: 1000, 4: 900}
Boolean
• Python data object can be broadly categorized into two types – mutable and
immutable types. In simple words changeable/modifiable and non-modifiable
types.
• 1. Immutable types: are those that can never change their value in place. In
python following types are immutable: integers, float, Boolean, strings, tuples
• Sample Code:
a = 10
b=a
c = 15 # will give output 10,10,30
From this code, you can say the value of integer a, b,c
a = 20 could be changed effortlessly, but this is not the case. Let
b = 40 us understand what was done behind the scene
c=b
value
10 15 20 21 40 55
address 250 272 280 284 290 312
>>> a=10
a = 10 >>> b=a
b=a >>> c=15
c = 15 a b c >>> print(id(a))
1757402304
>>> print(id(b))
Python provides id() function to get the 1757402304
memory address to which value /variable is >>> print(id(c))
1757402384
referring
>>> print(id(10))
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & 1757402304
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
a = 20
Now let us understand the changes done to variable a, b,c b = 40
c=b
value
10 15 20 21 40 55
address 250 272 280 284 290 312
>>> a=20
a >>> b=40
c b >>> c=b
>>> print(id(a))
1757402464
Python provides id() function to get the >>> print(id(b))
memory address to which value /variable is 1757402784
referring >>> print(id(c))
1757402784
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• From the previous code it is clear that variable names are stored references to a
value-object. Each time we change the value the variable‟s reference memory
address changes. So it will not store new value in same memory location that‟s
why Integer, float, Booleans, strings and tuples are immutable.
• Variables (of certain type) are NOT LIKE storage containers i.e. with fixed memory
address where value changes every time. Hence they are immutable
• Every python object has three key attributes associated with it:
1. type of object
2. value of an object
3. id of an object