001. PYTHON _ REVISION TOUR
001. PYTHON _ REVISION TOUR
TOKENS
BAREBONES OF PYTHON
VARIABLES AND ASSIGNMENT
INPUT & OUTPUT
DATA TYPES
MUTABLE AND IMMUTABLE TYPESS
EXPRESSION
FLOW OF CONTROL
JUMP STATEMENT
Python was created by Guido Van Rossum
The language was released in February I991
Python got its name from a BBC comedy series from seventies-
“Monty Python‟s Flying Circus”
Python can be used to follow both Procedural approach and
Object Oriented approach of programming
It is free to use
Python is based on or influenced with two programming
languages:
ABC language [replacement of BASIC]
Modula-3
:
language
Punctuators
Identifier
:
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
:
GradePay
_ismarried
:
Literal
:
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
:
>>> isMarried=True
>>> type(isMarried)
<class 'bool'>
:
>>> salary=None
>>> type(salary)
:
• Operators can be
Type of Operators Symbols
Arithmetic +, -, *, /, %, **, //
Relational >, <, >=, <=, ==, !=
Logical and, or
Identity is, is not
Assignment =
Membership in, not in
Arithmetic +=, -=, *=, /=, %=, **=, //=
Assignment
Bitwise &, ^, |, <<, >>
:
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)
:
x=100 x 100
x=“KV OEF” x
:
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
:
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.
Backward indexing
:
• >>> 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
a = 20
b = 40
c=b
:
value 21
10 15 20 40 55
address 250 272 280 284 290 312
>>> a=10
a = 10 >>> b=a
b=a >>> c=15
c = 15 b c >>> print(id(a))
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))
1757402304
:
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
:
• 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
: