Introduction to Python - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Introduction to Python

Description:

Introduction to Python. Data Structures. List. Tuple ... append. Syntax: list.append(element) Add element to end of list. Example: list1 = [3, '10', 2] ... – PowerPoint PPT presentation

Number of Views:201
Avg rating:3.0/5.0
Slides: 29
Provided by: chadh8
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Python


1
Introduction to Python
  • Data Structures

2
Data Structures
  • List
  • Tuple
  • Dictionary

3
Lists
  • Construction
  • Syntax elem1, elem2,
  • Heterogeneous, ordered sequence
  • Mutable
  • Example
  • gtgtgt list1 1, 'hello', 42j, 123.12
  • gtgtgt list1
  • 1, 'hello', (42j), 123.12
  • gtgtgt list10 'a'
  • gtgtgt list1
  • 'a', 'hello', (42j), 123.12

4
Lists - Operations
  • Concatenation ()
  • Syntax list1 list2
  • Example
  • gtgtgt 1, 'a', 'b' 3, 4, 5
  • 1, 'a', 'b', 3, 4, 5
  • Repetition ()
  • Syntax list number
  • Example
  • gtgtgt 23, 'x' 4
  • 23, 'x', 23, 'x', 23, 'x', 23, 'x'

5
Indexing
  • Indexing operator
  • Positive indices count from the left
  • Negative indices count from the right

0 1 2 3 4 5 6
a b c d e f g
-7 -6 -5 -4 -3 -2 -1
sequence0 a sequence-7 a sequence6
g sequence-1 g sequence2 c sequence-5
c
6
Slices
  • Two indices separated by a colon
  • Available for both strings and lists
  • Example
  • gtgtgt sequence 0, 1, 2, 3, 4, 5, 6, 7
  • gtgtgt sequence14
  • 1, 2, 3
  • gtgtgt sequence2-1
  • 2, 3, 4, 5, 6
  • Missing Index implies end point
  • gtgtgt sequence2
  • 0, 1
  • gtgtgt sequence3
  • 3, 4, 5, 6, 7

7
Tuples
  • Immutable version of list
  • Syntax (elem1, elem2, )
  • Items in tuple can not be altered
  • Example
  • gtgtgt tuple1 (1, 5, 10)
  • gtgtgt tuple12 2
  • Traceback (most recent call last)
  • File "ltpyshell136gt", line 1, in ?
  • tuple12 2
  • TypeError object doesn't support item
    assignment

8
Built-in Function len
  • Syntax len(object)
  • Return the length of object
  • Example
  • gtgtgt list1 1, 2, 3, 4, 5
  • gtgtgt len(list1)
  • 5
  • gtgtgt string1 "length of a string"
  • gtgtgt len(string1)
  • 18

9
Dictionaries
  • Mapping
  • Associate a key with a value
  • Each key must be unique

keys
values
10
Dictionaries
  • Construction
  • Syntax key1 value1, key2 value2
  • Unordered map
  • Example
  • gtgtgt dict1 'a' 1, 'b' 2
  • gtgtgt dict1
  • 'a' 1, 'b' 2
  • gtgtgt dict1'a'
  • 1
  • gtgtgt dict1'b'
  • 2

11
Controlling Flow
  • Loops - while
  • Syntax while ltboolean expressiongt
  • body
  • Execute body until looping condition is met
  • Example
  • gtgtgt i 0
  • gtgtgt while i lt 4
  • print i
  • i 1
  • 0
  • 1
  • 2
  • 3

12
Controlling Flow
  • Loops - for
  • Syntax for ltvargt in ltsequencegt
  • body
  • Execute body once for every element in sequence
  • Example
  • gtgtgt for i in 0, 1, 2, 3
  • print i
  • 0
  • 1
  • 2
  • 3

13
Built-in Function range
  • Syntax range(start, stop, step)
  • Generate a list of numbers from start to stop
    stepping every step
  • start defaults to 0, step defaults to 1
  • Example
  • gtgtgt range(5)
  • 0, 1, 2, 3, 4
  • gtgtgt range(1, 9)
  • 1, 2, 3, 4, 5, 6, 7, 8
  • gtgtgt range(2, 20, 5)
  • 2, 7, 12, 17

14
Controlling Flow
  • Using range with for
  • Generate list used by for with range
  • Example
  • gtgtgt for i in range(4)
  • print i
  • 0
  • 1
  • 2
  • 3

15
Controlling Flow
  • The continue statement
  • Continue to next iteration of loop, skipping
    remainder of body
  • Example
  • gtgtgt for x in range(8)
  • if x2 0
  • continue
  • print x

1
3
5
7
16
Controlling Flow
  • The break statement
  • Break out of inner most loop
  • Example
  • gtgtgt for number in range(10)
  • if number 4
  • print 'Breaking'
  • break
  • else
  • print number
  • 0
  • 1
  • 2
  • 3
  • Breaking

17
Using Data Structures
  • Data structures also have methods
  • Use built-in function dir to list all available
    methods
  • Example
  • gtgtgt lst 1, 3, 2
  • gtgtgt dir(lst)
  • '__add__', '__class__', '__contains__',
    '__delattr__', '__delitem__', '__delslice__',
    '__doc__', '__eq__', '__ge__', '__getattribute__',
    '__getitem__', '__getslice__', '__gt__',
    '__hash__', '__iadd__', '__imul__', '__init__',
    '__le__', '__len__', '__lt__', '__mul__',
    '__ne__', '__new__', '__reduce__', '__repr__',
    '__rmul__', '__setattr__', '__setitem__',
    '__setslice__', '__str__', 'append', 'count',
    'extend', 'index', 'insert', 'pop', 'remove',
    'reverse', 'sort'

18
Strings - Methods
  • split
  • Syntax string.split(sep)
  • Returns a list of strings
  • Example
  • gtgtgt text "1 2 4 5 1"
  • gtgtgt text.split()
  • '1', '2', '4', '5', '1'
  • gtgtgt test "a, b, c, d, e"
  • gtgtgt test.split(',')
  • 'a', ' b', ' c', ' d', ' e'

19
Strings - Methods
  • strip
  • Syntax string.strip()
  • Remove leading and trailing white space (tabs,
    new lines, etc)
  • Example
  • gtgtgt padded " stuff "
  • gtgtgt padded.strip()
  • 'stuff'
  • gtgtgt padded
  • ' stuff '
  • gtgtgt unpadded padded.strip()
  • gtgtgt unpadded
  • 'stuff'

20
Lists - Methods
  • append
  • Syntax list.append(element)
  • Add element to end of list
  • Example
  • gtgtgt list1 3, '10', 2
  • gtgtgt list1.append('new')
  • gtgtgt list1
  • 3, '10', 2, 'new'

21
Lists - Methods
  • pop
  • Syntax list.pop(index)
  • Remove and return item at position index from
    list
  • Default is to remove last item
  • Example
  • gtgtgt list1 3, '10', 2, 9, 11
  • gtgtgt list1.pop()
  • 11
  • gtgtgt list1
  • 3, '10', 2, 9

22
Lists - Methods
  • insert
  • Syntax list.insert(index, element)
  • Insert element into list at position index
  • Example
  • gtgtgt list2 0, 1, 2, 3, 4, 5
  • gtgtgt list2.insert(3, 'new')
  • gtgtgt list2
  • 0, 1, 2, 'new', 3, 4, 5

23
Lists - Methods
  • remove
  • Syntax list.remove(element)
  • Removes the first occurrence of element in list
  • Example
  • gtgtgt list2 0, 1, 3, 4, 3, 5
  • gtgtgt list2.remove(3)
  • gtgtgt list2
  • 0, 1, 4, 3, 5

24
Lists - Methods
  • sort
  • Syntax list.sort(cmpfunc)
  • Sort list in place
  • Example
  • gtgtgt list3 4, 12, 3, 9
  • gtgtgt list3.sort()
  • gtgtgt list3
  • 3, 4, 9, 12

25
Lists - Methods
  • reverse
  • Syntax list.reverse()
  • Reverse elements of list in place
  • Example
  • gtgtgt list3 4, 12, 3, 9
  • gtgtgt list3.reverse()
  • gtgtgt list3
  • 9, 3, 12, 4

26
Dictionaries - Methods
  • keys
  • Syntax dict.keys()
  • Return a list of all the keys in dict
  • Example
  • gtgtgt dict1 1 'a', 9 'cat', 2 2, 1
  • gtgtgt dict1.keys()
  • 1, 2, 9

27
Dictionaries - Methods
  • has_key
  • Syntax dict.has_key(key)
  • Determines if key is in dict
  • Example
  • gtgtgt dict1 'x' 1, 'y' 2
  • gtgtgt dict1.has_key('x')
  • 1
  • gtgtgt dict1.has_key('w')
  • 0

28
Dictionaries - Methods
  • values
  • Syntax dict.values()
  • Returns a list of all values in dict
  • Example
  • gtgtgt dict1 (1,2) 'a', (3,4) 'b', (9,8,7)
    'c'
  • gtgtgt dict1.values()
  • 'a', 'c', 'b'
Write a Comment
User Comments (0)
About PowerShow.com