Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python FiuncA

Uploaded by

nitinolxuser
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python FiuncA

Uploaded by

nitinolxuser
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

The ...

is the secondary prompt, which the Python interpreter uses to denote that it is expecting
some more

input.

The functions can be used in any expressions.

>>> square(2) + square(3)

13

>>> square(square(3))

81

Existing functions can be used in creating new functions.

>>> def sum_of_squares(x, y):

... return square(x) + square(y)

...

>>> sum_of_squares(2, 3)

13

Functions are just like other values, they can assigned, passed as arguments to other functions etc.

>>> f = square

>>> f(4)

16

>>> def fxy(f, x, y):

... return f(x) + f(y)

...

>>> fxy(square, 2, 3)

13

It is important to understand, the scope of the variables used in functions.

Lets look at an example.

x=0

y=0

def incr(x):

y=x+1

return y

incr(5)
print x, y

Variables assigned in a function, including the arguments are called the local variables to the
function. The

variables defined in the top-level are called global variables.

Changing the values of x and y inside the function incr won’t effect the values of global x and y.

But, we can use the values of the global variables.

pi = 3.14

def area(r):

return pi * r * r

2.1. Getting Started 9

Python Practice Book, Release 2014-08-10

When Python sees use of a variable not defined locally, it tries to find a global variable with that
name.

However, you have to explicitly declare a variable as global to modify it.

numcalls = 0

def square(x):

global numcalls

numcalls = numcalls + 1

return x * x

Problem 7: How many multiplications are performed when each of the following lines of code is
executed?

print square(5)

print square(2*5)

Problem 8: What will be the output of the following program?

x=1

def f():

return x

print x

print f()

Problem 9: What will be the output of the following program?

x=1

def f():
x=2

return x

print x

print f()

print x

Problem 10: What will be the output of the following program?

x=1

def f():

y=x

x=2

return x + y

print x

print f()

print x

Problem 11: What will be the output of the following program?

x=2

def f(a):

x=a*a

return x

y = f(3)

print x, y

Functions can be called with keyword arguments.

>>> def difference(x, y):

... return x - y

...

>>> difference(5, 2)

>>> difference(x=5, y=2)

>>> difference(5, y=2)

3
>>> difference(y=2, x=5)

10 Chapter 2. Table of Contents

Python Practice Book, Release 2014-08-10

And some arguments can have default values.

>>> def increment(x, amount=1):

... return x + amount

...

>>> increment(10)

11

>>> increment(10, 5)

15

>>> increment(10, amount=2)

12

There is another way of creating functions, using the lambda operator.

>>> cube = lambda x: x ** 3

>>> fxy(cube, 2, 3)

35

>>> fxy(lambda x: x ** 3, 2, 3)

35

Notice that unlike function defination, lambda doesn’t need a return. The body of the lambda is a
single

expression.

The lambda operator becomes handy when writing small functions to be passed as arguments etc.
We’ll see

more of it as we get into solving more serious problems.

Built-in Functions

Python provides some useful built-in functions.

>>> min(2, 3)

>>> max(3, 4)

4
The built-in function len computes length of a string.

>>> len("helloworld")

10

The built-in function int converts string to ingeter and built-in function str converts integers and
other type of

objects to strings.

>>> int("50")

50

>>> str(123)

"123"

Problem 12: Write a function count_digits to find number of digits in the given number.

>>> count_digits(5)

>>> count_digits(12345)

Methods

Methods are special kind of functions that work on an object.

For example, upper is a method available on string objects.

>>> x = "hello"

>>> print x.upper()

HELLO

2.1. Getting Started 11

Python Practice Book, Release 2014-08-10

As already mentioned, methods are also functions. They can be assigned to other variables can be
called separately.

>>> f = x.upper

>>> print f()

HELLO

Problem 13: Write a function istrcmp to compare two strings, ignoring the case.

>>> istrcmp('python', 'Python')

True

>>> istrcmp('LaTeX', 'Latex')


True

>>> istrcmp('a', 'b')

False

Conditional Expressions

Python provides various operators for comparing values. The result of a comparison is a boolean
value, either

True or False.

>>> 2 < 3

False

>>> 2 > 3

True

Here is the list of available conditional operators.

• == equal to

• != not equal to

• < less than

• > greater than

• <= less than or equal to

• >= greater than or equal to

It is even possible to combine these operators.

>>> x = 5

>>> 2 < x < 10

True

>>> 2 < 3 < 4 < 5 < 6

True

The conditional operators work even on strings - the ordering being the lexical order.

>>> "python" > "perl"

True

>>> "python" > "java"

True

There are few logical operators to combine boolean values.

• a and b is True only if both a and b are True.


• a or b is True if either a or b is True.

• not a is True only if a is False.

>>> True and True

True

>>> True and False

False

>>> 2 < 3 and 5 < 4

12 Chapter 2. Table of Contents

Python Practice Book, Release 2014-08-10

False

>>> 2 < 3 or 5 < 4

True

Problem 14: What will be output of the following program?

print 2 < 3 and 3 > 1

print 2 < 3 or 3 > 1

print 2 < 3 or not 3 > 1

print 2 < 3 and not 3 > 1

Problem 15: What will be output of the following program?

x=4

y=5

p = x < y or x < z

print p

Problem 16: What will be output of the following program?

True, False = False, True

print True, False

print 2 < 3

The if statement

The if statement is used to execute a piece of code only when a boolean expression is true.

>>> x = 42

>>> if x % 2 == 0: print 'even'

even
>>>

In this example, print ’even’ is executed only when x % 2 == 0 is True.

The code associated with if can be written as a separate indented block of code, which is often the
case when

there is more than one statement to be executed.

>>> if x % 2 == 0:

... print 'even'

...

even

>>>

The if statement can have optional else clause, which is executed when the boolean expression is
False.

>>> x = 3

>>> if x % 2 == 0:

... print 'even'

... else:

... print 'odd'

...

odd

>>>

The if statement can have optional elif clauses when there are more conditions to be checked. The
elif

keyword is short for else if, and is useful to avoid excessive indentation.

>>> x = 42

>>> if x < 10:

... print 'one digit number'

... elif x < 100:

... print 'two digit number'

... else:

2.1. Getting Started 13

Python Practice Book, Release 2014-08-10

... print 'big number'


...

two digit number

>>>

Problem 17: What happens when the following cod

You might also like