100% found this document useful (1 vote)
39 views

Unit 3 Powerpoint

The document provides an overview of various data structures in Python, focusing on strings, lists, tuples, sets, and dictionaries. It details string manipulation techniques, including slicing, concatenation, and built-in methods, as well as list operations such as indexing, mutability, and various list methods. Additionally, it highlights the differences between mutable and immutable data types, emphasizing the properties of strings and lists.

Uploaded by

sach1n0496as
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
39 views

Unit 3 Powerpoint

The document provides an overview of various data structures in Python, focusing on strings, lists, tuples, sets, and dictionaries. It details string manipulation techniques, including slicing, concatenation, and built-in methods, as well as list operations such as indexing, mutability, and various list methods. Additionally, it highlights the differences between mutable and immutable data types, emphasizing the properties of strings and lists.

Uploaded by

sach1n0496as
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Unit- III DATA STRUCTURES

Strings – Slicing – Immutability– Built in Methods and Functions-


Concatenating, appending and Multiplying Strings – -String Modules
– List Creation – Accessing Values- Slicing– List Methods – In built
functions for List – Tuples – Creation-Operations on Tuples-
Traversing, Indexing and Slicing – Tuple Assignment– In built
functions for Tuples– Sets- Creation- Operations- Dictionaries-
Operations and Methods
Python - Strings
Strings are amongst the most popular types in Python. We
can create them simply by enclosing characters in quotes.
Python treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a
variable. For example:
var1 = 'Hello World!'
var2 = "Python Programming"
Accessing Values in Strings:

Python does not support a character type; these are treated as strings of
length one, thus also considered a substring.
To access substrings, use the square brackets for slicing along with the
index or indices to obtain your substring:
Example:
var 1 = 'Hello World!'
var2 = "Python Programming"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]

This will produce following result:


var1[0]: H
var2[1:5]: ytho
Updating Strings:

You can "update" an existing string by (re)assigning a variable


to another string. The new value can be related to its previous
value or to a completely different string altogether.
Example:
var1 = 'Hello World!'
print "Updated String :- ", var1[:6] +
'Python'

This will produce following result:


Updated String :- Hello Python
String Special Operators: Assume string variable a
holds 'Hello' and variable b holds 'Python' then:
Operator Description Example
+ Concatenation - Adds values on a + b will give
either side of the operator HelloPython
* Repetition - Creates new strings, a*2 will give -HelloHello
concatenating multiple copies of the
same string
[] Slice - Gives the character from the a[1] will give e
given index
[:] Range Slice - Gives the characters a[1:4] will give ell
from the given range
in Membership - Returns true if a H in a will give 1
character exists in the given string
not in Membership - Returns true if a M not in a will give 1
character does not exist in the given
string
r/R Raw String - Suppress actual print r'\n' prints \n and
meaning of Escape characters. print R'\n' prints \n
% Format - Performs String formatting See at next section
String Formatting Operator:
Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E
Other supported symbols and functionality are
listed in the following table:
Symbol Functionality
* argument specifies width or precision
- left justification
+ display the sign
<sp> leave a blank space before a positive number
# add the octal leading zero ( '0' ) or hexadecimal
leading '0x' or '0X', depending on whether 'x' or 'X'
were used.
0 pad from left with zeros (instead of spaces)
% '%%' leaves you with a single literal '%'
(var) mapping variable (dictionary arguments)
m.n. m is the minimum total width and n is the number
of digits to display after the decimal point (if appl.)
Triple Quotes:
Python's triple quotes comes to the rescue by allowing strings to span
multiple lines, including verbatim NEWLINEs, TABs, and any other
special characters.
The syntax for triple quotes consists of three consecutive single or double
quotes.
para_str = """this is a long string that is made up
of several lines and non-printable characters such as
TAB ( \t ) and they will show up that way when
displayed. NEWLINEs within the string, whether
explicitly given like this within the brackets [ \
n ], or just a NEWLINE within the variable assignment
will also show up. """
print para_str;
Raw String:

Raw strings don't treat the backslash as a special


character at all. Every character you put into a
raw string stays the way you wrote it:
print 'C:\\nowhere'
This would print following result:
C:\nowhere
Now let's make use of raw string. We would put
expression in r'expression' as follows:
print r'C:\\nowhere'
This would print following result:
C:\\nowhere
Unicode String:

Normal strings in Python are stored internally as 8-bit ASCII, while


Unicode strings are stored as 16-bit Unicode. This allows for a more
varied set of characters, including special characters from most
languages in the world. I'll restrict my treatment of Unicode strings to
the following:
print u'Hello, world!'
This would print following result:
Hello, world!
Built-in String Methods:
capitalize()
1
Capitalizes first letter of string
2 center(width, fillchar)
Returns a space-padded string with the original string centered to a total of width
columns
3 count(str, beg= 0,end=len(string))
Counts how many times str occurs in string, or in a substring of string if starting
index beg and ending index end are given
3 decode(encoding='UTF-8',errors='strict')
Decodes the string using the codec registered for encoding. encoding defaults to the
default string encoding.
4 encode(encoding='UTF-8',errors='strict')
Returns encoded string version of string; on error, default is to raise a ValueError
unless errors is given with 'ignore' or 'replace'.
5 endswith(suffix, beg=0, end=len(string))
Determines if string or a substring of string (if starting index beg and ending index
end are given) ends with suffix; Returns true if so, and false otherwise
6 expandtabs(tabsize=8)
Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not
provided
7 find(str, beg=0 end=len(string))

Determine if str occurs in string, or in a substring of string if starting index beg and
ending index end are given; returns index if found and -1 otherwise
8 index(str, beg=0, end=len(string))

Same as find(), but raises an exception if str not found

9 isa1num()

Returns true if string has at least 1 character and all characters are alphanumeric
and false otherwise
10 isalpha()

Returns true if string has at least 1 character and all characters are alphabetic and
false otherwise
11 isdigit()

Returns true if string contains only digits and false otherwise

12 islower()

Returns true if string has at least 1 cased character and all cased characters are in
lowercase and false otherwise
13 isnumeric()

Returns true if a unicode string contains only numeric characters and false otherwise

14 isspace()

Returns true if string contains only whitespace characters and false otherwise
15 istitle()

Returns true if string is properly "titlecased" and false otherwise


16 isupper()

Returns true if string has at least one cased character and all cased characters are in
uppercase and false otherwise
17 join(seq)

Merges (concatenates) the string representations of elements in sequence seq into a


string, with separator string
18 len(string)

Returns the length of the string


19 ljust(width[, fillchar])

Returns a space-padded string with the original string left-justified to a total of width
columns
20 lower()

Converts all uppercase letters in string to lowercase


21 lstrip()

Removes all leading whitespace in string


22 maketrans()

Returns a translation table to be used in translate function.


23 max(str)

Returns the max alphabetical character from the string str


24 min(str)

Returns the min alphabetical character from the string str

25 replace(old, new [, max])

Replaces all occurrences of old in string with new, or at most max occurrences if max
given
26 rfind(str, beg=0,end=len(string))

Same as find(), but search backwards in string

27 rindex( str, beg=0, end=len(string))

Same as index(), but search backwards in string

28 rjust(width,[, fillchar])

Returns a space-padded string with the original string right-justified to a total of width
columns.
29 rstrip()

Removes all trailing whitespace of string

30 split(str="", num=string.count(str))

Splits string according to delimiter str (space if not provided) and returns list of
substrings; split into at most num substrings if given
31 splitlines( num=string.count('\n'))

Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs
removed
32 startswith(str, beg=0,end=len(string))

Determines if string or a substring of string (if starting index beg and ending index
end are given) starts with substring str; Returns true if so, and false otherwise
33 strip([chars])

Performs both lstrip() and rstrip() on string

34 swapcase()

Inverts case for all letters in string

35 title()

Returns "titlecased" version of string, that is, all words begin with uppercase, and the
rest are lowercase
36 translate(table, deletechars="")

Translates string according to translation table str(256 chars), removing those in the
del string
37 upper()

Converts lowercase letters in string to uppercase

38 zfill (width)

Returns original string leftpadded with zeros to a total of width characters; intended
for numbers, zfill() retains any sign given (less one zero)
39 isdecimal()

Returns true if a unicode string contains only decimal characters and false otherwise
What is Not a “Collection”?

Most of our variables have one value in them - when we put a


new value in the variable, the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of Collection

• A collection allows us to put many values in a single


“variable”

• A collection is nice because we can carry all many values


around in one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


Sequences
• Sequence: an object that contains multiple items of data
The items are stored in sequence one after another
• Python provides different types of sequences, including lists and tuples
The difference between these is that a list is mutable and a tuple is immutable
Introduction to Lists
• List: an object that contains multiple data items
Element: An item in a list
Format: list = [item1, item2, etc.]
Can hold items of different types
• print function can be used to display an entire list
• list() function can convert certain types of objects
to lists
Introduction to Lists (cont’d.)
The Repetition Operator and
• Iterating over a List
Repetition operator: makes multiple copies of a list
and joins them together
The * symbol is a repetition operator when applied to a
sequence and an integer
• Sequence is left operand, number is right
General format: list * n
• You can iterate over a list using a for loop
Format: for x in list:
Indexing
• Index: a number specifying the position of an element in a list
Enables access to individual element in list
Index of first element in the list is 0, second element is 1, and n’th element is n-1
Negative indexes identify positions relative to the end of the list
• The index -1 identifies the last element, -2 identifies the next to last element,
etc.
The len function
• An IndexError exception is raised if an invalid
index is used
• len function: returns the length of a sequence such
as a list
Example: size = len(my_list)
Returns the number of elements in the list, so the index of last
element is len(list)-1
Can be used to prevent an IndexError exception when
iterating over a list with a loop
Lists Are Mutable
• Mutable sequence: the items in the sequence can be
changed
Lists are mutable, and so their elements can be changed
• An expression such as
• list[1] = new_value can be used to
assign a new value to a list element
Must use a valid index to prevent raising of an IndexError
exception
Concatenating Lists
• Concatenate: join two things together
• The + operator can be used to concatenate two lists
– Cannot concatenate a list with another data type, such as a number
• The += augmented assignment operator can also be used to concatenate
lists
List Slicing
• Slice: a span of items that are taken from a sequence
List slicing format: list[start : end]
Span is a list containing copies of elements from start up to,
but not including, end
• If start not specified, 0 is used for start index
• If end not specified, len(list) is used for end index
Slicing expressions can include a step value and negative
indexes relative to end of list
Finding Items in Lists with the
in Operator
• You can use the in operator to determine whether an
item is contained in a list
General format: item in list
Returns True if the item is in the list, or False if it is not in
the list
• Similarly you can use the not in operator to
determine whether an item is not in a list
List Methods and Useful Built-
in Functions
• append(item): used to add items to a list – item is
appended to the end of the existing list
• index(item): used to determine where an item is
located in a list
Returns the index of the first element in the list containing
item
Raises ValueError exception if item not in the list
List Methods and Useful Built-

in Functions (cont’d.)
insert(index, item): used to insert item at position index in the
list
• sort(): used to sort the elements of the list in ascending order
• remove(item): removes the first occurrence of item in the list
• reverse(): reverses the order of the elements in the list
List Methods

>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
[... 'append', 'count', 'extend',
'index', 'insert', 'pop', 'remove',
'reverse', 'sort']
>>>
List Methods and Useful Built-
in Functions (cont’d.)
• del statement: removes an element from a specific
index in a list
General format: del list[i]
• min and max functions: built-in functions that
returns the item that has the lowest or highest value
in a sequence
The sequence is passed as an argument
Copying Lists
• To make a copy of a list you must copy each element of the list
Two methods to do this:
• Creating a new empty list and using a for loop to add a copy of each element
from the original list to the new list
• Creating a new empty list and concatenating the old list to the new empty list
Copying Lists (cont’d.)
Processing Lists
• List elements can be used in calculations
• To calculate total of numeric values in a list use loop
with accumulator variable
• To average numeric values in a list:
Calculate total of the values
Divide total of the values by len(list)
• List can be passed as an argument to a function
Lists are Mutable
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
Traceback
TypeError: 'str' object
does not
Strings are “immutable” - we support item assignment
cannot change the contents of a >>> x = fruit.lower()
string - we must make a new >>> print(x)
string to make any change banana
Lists are “mutable” - we can >>> lotto = [2, 14, 26, 41,
change an element of a list 63]
>>> print(lotto)
using the index operator
[2, 14, 26, 41, 63]
>>> lotto[2] = 28
>>> print(lotto)
[2, 14, 28, 41, 63]
total = 0
count = 0
while True : Enter a number: 3
inp = input('Enter a Enter a number: 9
number: ') Enter a number: 5
if inp == 'done' : break Enter a number: done
value = float(inp) Average: 5.66666666667
total = total + value
count = count + 1
numlist = list()
while True :
average = total / count inp = input('Enter a number:
print('Average:', average) ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)

average = sum(numlist) /
len(numlist)
print('Average:', average)
Best Friends: Strings and Lists
>>> abc = 'With three words' >>> print(stuff)
>>> stuff = abc.split() ['With', 'three', 'words']
>>> print(stuff) >>> for w in stuff :
['With', 'three', 'words'] ... print(w)
>>> print(len(stuff)) ...
3 With
>>> print(stuff[0]) Three
With Words
>>>

Split breaks a string into parts and produces a list of strings. We think of these
as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces']
>>>
>>> line = 'first;second;third'
>>> thing = line.split()
>>> print(thing)
['first;second;third'] ● When you do not specify a
>>> print(len(thing))
1 delimiter, multiple spaces are
>>> thing = line.split(';') treated like one delimiter
>>> print(thing)
['first', 'second', 'third']
>>> print(len(thing)) ● You can specify what
3 delimiter character to use in
>>>
the splitting
The Double Split Pattern
From [email protected] Sat Jan
5 09:14:16 2008

words = line.split()
email = words[1]
print pieces[1]

[email protected]
The Double Split Pattern
From [email protected] Sat Jan
5 09:14:16 2008
words = line.split()
email = words[1] [email protected]
pieces =
email.split('@')
print pieces[1]
['stephen.marquard', 'uct.ac.za']
The Double Split Pattern
From [email protected] Sat
Jan 5 09:14:16 2008

[email protected]
words = line.split()
email = words[1]
['stephen.marquard', 'uct.ac.za']
pieces =
email.split('@')
print(pieces[1]) 'uct.ac.za
'
List Summary

• Concept of a collection • Slicing lists

• Lists and definite loops • List methods: append, remove

• Indexing and lookup • Sorting lists

• List mutability • Splitting strings into lists of words

• Functions: len, min, max, sum • Using split to parse strings

You might also like