Python - Lecture 2
-
Upload
ravi-kiran-khareedi -
Category
Technology
-
view
100 -
download
2
description
Transcript of Python - Lecture 2
Python
Lecture – 2 Lecture – 2
- Ravi Kiran Khareedi
Operators
• Operators are the functionality to perform
some work.
• They need some thing to work on – They are
called Operands.called Operands.
• Ex: sum = a + b;
• + , = ->Operators
• Sum, a,b -> Operands
Operators
• + -> Add
• - -> Subtract
• * -> Multiply
• / -> Divide
• ** -> Power of• ** -> Power of
• = -> Assignment
• % -> Modulus
• << -> Left Shift
• >> -> Right Shift
Operators (Cont)
Comparison Operators:
> Greater than
< Less than
== Equal to
<= Less Than or equal to<= Less Than or equal to
>= Greater than or equal to
!= Not equal to
Operators (Cont)
Bitwise Operators
• & - Bitwise And
• | - Bitwise Or
• ^ - Bitwise Xor• ^ - Bitwise Xor
• ~ - 1’s complement
Operators(Cont)
Logical Operators
• And
• Or
• Not• Not
Precedence
• Consider 2+3*4
• Guess the answer ?
• Are we sure which operation to perform first • Are we sure which operation to perform first
in an expression?
• To do this we need to know which operator
has higher priority than the other. This is
nothing but PRECEDENCE.
Precedence
Note : We haven’t seen few operators. For now it can ignored.
Chapter : CONTROL FLOW
Control Flow
• Use to alter the flow of execution
• Without Control Statements, the flow of
execution is always sequential.
• 3 Control Statements: • 3 Control Statements:
– if
– for
– while
if statement
• ‘if’ statement is used to check the condition. (Execute certain statements based on certain conditions)
• If (condition is true)
{ # Some statements here}{ # Some statements here}
Else
{ # Some statements here}
NOTE: Code grouping in Python is done based on indentation. {} are not allowed
Example
a = 5;
if (a == 5):
print "ok !"
else:
print "Not ok";
Print “End”;
TASK :
1. Try with out else
2. Try with Elif
Nested If’s
if expression1:
statement(s)
If expression2:
statement(s)
elif expression3:
statement(s) statement(s)
else
statement(s)
elif expression4:
statement(s)
else:
statement(s)
While statement
while expression:
statement(s)
• Ex:
i = 1i = 1
while (i < 10):
print i;
i = i + 1;
print “Done”;
For statement
for iterating_var in sequence:
statements(s)
Ex:Ex:
for i in range(1, 5):
print i
Break statement
• Used to stop the execution of a looping
statement,
– even if the loop condition hasn’t become false.
– Or the sequence of items has been completely – Or the sequence of items has been completely
iterated over.
Using break
for i in range(1 , 20):
if(i == 10):
break;
print i;
Continue statement
• The continue statement is used to tell Python
to skip the rest of the statements in the
current loop block and to continue to the next
iteration of the loop.iteration of the loop.
Continue Usage
for i in range(1 , 20):
if(i == 10):
continue;
print i;print i;
Else block’s in looping Statements
• Python supports to have an else statement
associated with a loop statements.
• If the else statement is used with a for loop, the else
statement is executed when the loop has exhausted statement is executed when the loop has exhausted
iterating the list.
• If the else statement is used with a while loop, the
else statement is executed when the condition
becomes false.
TASK
• Print the even numbers from 1 to 20 using for
loop and while loop.
• Print the numbers from 1 to 20 skipping the
multiples of 5. (Don’t print 5, 10, 15 )multiples of 5. (Don’t print 5, 10, 15 )
For statement, bit more
for i in range(1 , 20, 2):
print i;
3rd argument specifies the step size for 3rd argument specifies the step size for
incrementing i
Chapter: Functions
Functions
• Functions are a reusable piece of program.
• In simple words, it’s a block of statements
which has a name.
• We can run a function from anywhere in the • We can run a function from anywhere in the
program, any number of times using its name,
this is called “Function Calling”.
Functions: Usage
• Functions are defined using def
• Syntax:
def fnName():
Statement(s)Statement(s)
EX:
def sayHello():
print 'Hello World!' # block belonging to the function
# End of function
sayHello() # call the function
Function Parameters
• A function can take parameters which are just values you supply to the function so that the function can do something utilising those values
• Ex:def printMax(a, b): # These are Function Parametersdef printMax(a, b): # These are Function Parameters
if a > b:
print a, 'is maximum'
Else:
print b, 'is maximum'
printMax(3, 4) # directly give literal values
X = 5
y = 7
printMax(x, y) # give variables as arguments
TASK
• Guess the output:a = 10;
def fun():
a = 5;
print a , "- inside function";print a , "- inside function";
fun();
print a, " - Outside function";
Local Variables
• Variable defined inside a function is limited to
the scope of the function.
• They are different from the variables of same
name outside the function. name outside the function.
Global Variables
def func():
global x
print 'x is', x
x = 2
print 'Changed global x to', xprint 'Changed global x to', x
x = 50
func()
print 'Value of x is', x
- We should try to avoid using global.
Default Argument Values
• For some functions, you may want to make
some of its parameters as optional and use
default values if the user does not want to
provide values for such parameters – This is provide values for such parameters – This is
Default ArgumentsEx:
def say(message, times = 1):
print message, times
say ('Hello');
say ('World', 5);
TASK
Valid or Invalid Statements ?
• def fun(a , b =5)
• def fun (a=5, b)
• def fun(a=5, b =3)
Note: Default Arguments
• Only those parameters which are at the end of the parameter
list can be given default argument values i.e. you cannot have
a parameter with a default argument value before a
parameter without a default argument value in the order of
parameters declared in the function parameter list.parameters declared in the function parameter list.
• This is because the values are assigned to the parameters by
position
Keyword Arguments
• In case of large number of parameters, Default
arguments may be difficult to use.
• So Python Supports Keyword arguments.
• It provides more flexibility• It provides more flexibility
• Note: You cannot have a parameter with a default argument
value before a parameter without a default argument value
Advantages of Keyword Arguments
There are two advantages –
• Using the function is easier since we do not
need to worry about the order of the
arguments. arguments.
• We can give values to only those parameters
which we want, provided that the other
parameters have default argument values.
return statement
• The return statement is used to return from a function i.e. break out of the function.
• We can optionally return a value from the function as well.
• Ex:• Ex:def maximum(x, y):
if x > y:
return x
else:
return y
print maximum(2, 3)
References
• Python Tutorial by Tutorialspoint.com
• A Byte of Python by Swaroop C H