Python - Lecture 2

36
Python Lecture 2 - Ravi Kiran Khareedi

description

Python tutorial

Transcript of Python - Lecture 2

Page 1: Python - Lecture 2

Python

Lecture – 2 Lecture – 2

- Ravi Kiran Khareedi

Page 2: Python - Lecture 2

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

Page 3: Python - Lecture 2

Operators

• + -> Add

• - -> Subtract

• * -> Multiply

• / -> Divide

• ** -> Power of• ** -> Power of

• = -> Assignment

• % -> Modulus

• << -> Left Shift

• >> -> Right Shift

Page 4: Python - Lecture 2

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

Page 5: Python - Lecture 2

Operators (Cont)

Bitwise Operators

• & - Bitwise And

• | - Bitwise Or

• ^ - Bitwise Xor• ^ - Bitwise Xor

• ~ - 1’s complement

Page 6: Python - Lecture 2

Operators(Cont)

Logical Operators

• And

• Or

• Not• Not

Page 7: Python - Lecture 2

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.

Page 8: Python - Lecture 2

Precedence

Note : We haven’t seen few operators. For now it can ignored.

Page 9: Python - Lecture 2

Chapter : CONTROL FLOW

Page 10: Python - Lecture 2

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

Page 11: Python - Lecture 2

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

Page 12: Python - Lecture 2

Example

a = 5;

if (a == 5):

print "ok !"

else:

print "Not ok";

Print “End”;

TASK :

1. Try with out else

2. Try with Elif

Page 13: Python - Lecture 2

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)

Page 14: Python - Lecture 2

While statement

while expression:

statement(s)

• Ex:

i = 1i = 1

while (i < 10):

print i;

i = i + 1;

print “Done”;

Page 15: Python - Lecture 2

For statement

for iterating_var in sequence:

statements(s)

Ex:Ex:

for i in range(1, 5):

print i

Page 16: Python - Lecture 2

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.

Page 17: Python - Lecture 2

Using break

for i in range(1 , 20):

if(i == 10):

break;

print i;

Page 18: Python - Lecture 2

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.

Page 19: Python - Lecture 2

Continue Usage

for i in range(1 , 20):

if(i == 10):

continue;

print i;print i;

Page 20: Python - Lecture 2

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.

Page 21: Python - Lecture 2

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 )

Page 22: Python - Lecture 2

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

Page 23: Python - Lecture 2

Chapter: Functions

Page 24: Python - Lecture 2

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”.

Page 25: Python - Lecture 2

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

Page 26: Python - Lecture 2

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

Page 27: Python - Lecture 2

TASK

• Guess the output:a = 10;

def fun():

a = 5;

print a , "- inside function";print a , "- inside function";

fun();

print a, " - Outside function";

Page 28: Python - Lecture 2

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.

Page 29: Python - Lecture 2

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.

Page 30: Python - Lecture 2

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);

Page 31: Python - Lecture 2

TASK

Valid or Invalid Statements ?

• def fun(a , b =5)

• def fun (a=5, b)

• def fun(a=5, b =3)

Page 32: Python - Lecture 2

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

Page 33: Python - Lecture 2

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

Page 34: Python - Lecture 2

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.

Page 35: Python - Lecture 2

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)

Page 36: Python - Lecture 2

References

• Python Tutorial by Tutorialspoint.com

• A Byte of Python by Swaroop C H