Академический Документы
Профессиональный Документы
Культура Документы
Strings
Strings
● A string is a sequence of characters
a string can be expressed using within single quotes or doubel quotes.
“hello” or ‘hello’
● You can access the characters one at a time using bracket operator.
name = “hello”
● name[0] will return ‘h’
● name[-1] will return ‘o’
– The number inside the bracket is called an index
– The value of the index has to be an integer.
● len(): a built in function which returns number of characters in a string.
– len(name) will return 5
String slices
● A segment of a string is called a slice.
>>> dept = ‘Information Technology’
>>> print dept[0:11]
Information
>>> print dept[12:22]
● The operator [n:m] returns the part of the string
from the “n th” charachter to “m th” character ,
including the first and excluding the last.
String slices
● If you omit the first index(before the colon) the slice starts
at the beginning of the string.
>>> college = “vasavi”
>>> print college[:2]
‘va’
● If you omit the last index(after the colon) the slice goes to
the end of the string.
Print college[1:]
‘asavi’
Strings are immutable
● Strings are immutable, i.e you can’t change an existing
string.
>>> greeting = ‘Hello, World!’
>>> greeting[0] = ‘B’
● The best you can do is create a new string which is that
is a variation on the original.
>>> greeting = ‘Hello, World!’
>>> new_greeting = ‘B’ + greeting[1:]
>>> print new_greeting
Strings methods
● upper()
● lower()
● find()
find(arg1,start_index,stopping_index)
String methods which return
boolean values
Lists
List
● Like a string, A list is a sequence of values.
● In a string, the values are characters.
● In a list, they can be any type.
● The values in the list are called elements or
sometimes items.
Creating a list
There are several ways to create a list
– Simplest is to enclose the elements in a square
brackets [ ]
● [10, 20, 30, 40, 50]
● [‘sun’, ‘mon’, ‘tue’, ‘wed’, ‘thu’, ‘fri’, ‘sat’]
– The elements of the list don’t have to be of same type.
● [‘ram’, ‘kumar’, 19, 5.3 , ‘m’, [9812387321, 7077132323]]
– A list within another list is nested
– A list that contains no elements is called as an empty
list.
● []
assigning list values to variables
● days = [‘sun’, ‘mon’, ‘tue’, ‘wed’, ‘thu’, ‘fri’, ‘sat’]
● emp_details = [‘ram’, ‘kumar’, 19, 5.3 , ‘m’, [9812387321, 7077132323]]
● marks = [33,44,55,66,77]
● empty = [ ]
Accessing elements of a list
● Syntax of accessing the elements of a list is the same as for
accessing the characters of a string.
● Use bracket operator to access the elements of a list
● The expresssion inside the bracket specifies the index. Indices start
from 0
● days = [‘sun’, ‘mon’, ‘tue’, ‘wed’, ‘thu’, ‘fri’, ‘sat’]
– print (days[0])
● emp_details = [‘ram’, ‘kumar’, 19, 5.3 , ‘m’, [9812387321, 7077132323]]
– age = emp_details[2]
– contact_info = emp_details[5]
Lists are mutable
● Unlike strings, lists are mutable
– emp_details = [‘ram’, ‘kumar’, 19, 5.3 , ‘m’, [9812387321, 7077132323]]
– emp_details[2] = 20
print(emp_details)
● You can think of a list as a relationship between indices and elements.
● This relationship is called mapping.
● Each index ‘maps to’ one of the elements.
● List indices work the same way as string indices
– Any integer expression can be used as an index.
– If you try to read or write an element that does not exist, you get an
IndexError.
– If an index has a negative value, it counts backwards from the end of the list.
in operator on lists
● The in operator also works on lists
– days = [‘sun’, ‘mon’, ‘tue’, ‘wed’, ‘thu’, ‘fri’, ‘sat’]
● ‘sun’ in days will return True
● ‘may’ in days will return False
Traversing a list...reading
● Most common way to traverse a list is by using for loop
● days = [‘sun’, ‘mon’, ‘tues’, ‘wednes’, ‘thurs’, ‘fri’,
‘satur’]
for day in days:
print(day)
● The above code works well if you only need to read the elements of a
list.
● A for loop over an empty list never executes the body:
for z in [ ]:
print(‘This never happens’)
● A list can contain another list, the nested list still counts as a single element.
emp_details = [‘ram’, ‘kumar’, 19, 5.3 , ‘m’, [9812387321, 7077132323]]
– The length of the above list is 6
Traversing a list...read, write/update
● If you want to write or update the elements, you need the indices.
● A common way to do this is to combine the functions range and len:
for i in range(len(days)):
days[i] = days[i] + ‘day’
● len returns the number of elements in the list.
● range returns a list of indices from 0 to n1, where n is length of the
list.
● Each time through the loop i gets the index of the next element.
● The assignment statement in the body uses i to read the old value of
the element and assign the new value.
List Operations
● The + operator concatenates lists:
bowlers = [‘shami’, ‘chanal’]
batsmen = [‘raina’, ‘gambhir’, ‘kohli’, ‘rahane’]
all_rounders = [‘ashwin’, ‘jadeja’, ‘pathan’]
wkt_keepers = [‘dhoni’, ‘karthik’]
cricket_team = bowlers + batsmen + all_rounders +
wkt_keepers
List Operations
●
The * operator repeats a list a given number of times:
marks = [0]
marks = marks * 4