15
Python programming Lab Sequence 3 Prof B N Kshirsagar MIT Group , Aurangabad , MS, India

Python programming lab3 250215

  • Upload
    profbnk

  • View
    57

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Python programming lab3 250215

Python programmingLab Sequence 3

Prof B N KshirsagarMIT Group , Aurangabad , MS, India

Page 2: Python programming lab3 250215

Simple data types

Numbers

integer

floating-point

complex!

Strings

characters are strings of length 1

Booleans are 0/1 (or False/True)

Page 3: Python programming lab3 250215

Operators+ - * / %

+= -= etc. (no ++ or --)

Page 4: Python programming lab3 250215

Compound data typesListsPython knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

fruits = ['Banana', 'Apple', 'Lime']

students = [‘Suresh’, ‘Sachin’, ‘Neha’, ‘Ashish’, ‘Pritam’]

print fruits print students

Page 5: Python programming lab3 250215

LIST - Examples>>> squares = [1, 4, 9, 16, 25]

>>> squares

[1, 4, 9, 16, 25]

>>>squares[0] #indexing returns the item

1

>>>squares[-1]

25

>>>squares[-3:] #slicing returns a new list

[9,16,25]

Page 6: Python programming lab3 250215

LIST - Examples

>>> students =['Suresh', 'Sachin', 'Pritam']>>> students['Suresh', 'Sachin', 'Pritam']>>> type(students)<type 'list‘>>>> len(students)3>>>

Page 7: Python programming lab3 250215

>>> students[1:]

['Sachin', 'Pritam']

>>> students.append('Ashish')

>>> len(students)

4

>>> students[-2]

'Pritam‘

>>> students[:]

['Suresh', 'Sachin', 'Pritam', 'Ashish']

>>>

Page 8: Python programming lab3 250215

LIST - Examples>>> students[0] = 'Bhawar‘

>>> students

['Bhawar', 'Sachin', 'Pritam', 'Ashish‘]

>>> students[1:3] = ['S', 'Toshniwal', 'Lingayat']

>>> students

['Bhawar', 'S', 'Toshniwal', 'Lingayat', 'Ashish']

Page 9: Python programming lab3 250215

LIST - Examples>>> a = ['a', 'b', 'c']

>>> n = [1, 2, 3]

>>> x = [a, n]

>>> x

[['a', 'b', 'c'], [1, 2, 3]]

>>> x[0]

['a', 'b', 'c']

>>> x[0][1]

'b'

Page 10: Python programming lab3 250215

>>> # Fibonacci series:

... # the sum of two elements defines the next ...

a, b = 0, 1

>>> while b < 10:

print b

a, b = b, a+b

1

1

2

3

5

8

Page 11: Python programming lab3 250215

Control Flowif statements>>> x = int(raw_input("Please enter an integer: "))

Please enter an integer: 42

>>> if x < 0:

... x = 0

... print 'Negative changed to zero‘

... elif x == 0:

... print 'Zero'

... elif x == 1:

... print 'Single‘

... else:

... print 'More‘

...

More

Page 12: Python programming lab3 250215

for statement>>> # Measure some strings

: ... words = ['cat', 'window', 'defenestrate']

>>> for w in words:

... print w, len(w)

...

cat 3

window 6

defenestrate 12

Page 13: Python programming lab3 250215

range() function>>> range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> range(5, 10)

[5, 6, 7, 8, 9]

>>> range(0, 10, 3)

[0, 3, 6, 9]

>>> range(-10, -100, -30)

[-10, -40, -70]

Page 14: Python programming lab3 250215

To iterate over the indices of a sequence, you can combine range() and len() as follows:

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']

>>> for i in range(len(a)):

... print i, a[i]

...

0 Mary

1 had

2 a

3 little

4 lamb

Page 15: Python programming lab3 250215

Thanks ContactProf B N KshirsagarMIT Group of Academic & Research Institutions, Indiawww.mit.asiaprof.bnk at gmail dot com