Академический Документы
Профессиональный Документы
Культура Документы
Python Types
Objectives
This session is about Python types:
• Basic Python Literals
• Numbers
• Strings
• More complex Python types
• Tuples
• Sets
• Lists
• Dictionaries
Numbers
• Integers – normal vs long
• Floats – 16 digits of precision
• Other numeric types include hexdecimal, octal, complex numbers
Operators and functions for numbers will work on all the above
Numbers : Assignments
a=10
a=b=c=20
a,b,c=10,20,30 # a=10 b=20 c=30
a,b,c=10,5.3,20 # Incorrect!
q,r=divmod(5,3)
q becomes 1
r becomes 3
Functions in python can return multiple values in form of a sequence and the
returned values can be captured as above. The same can be implemented in
user defined functions as well.
Numbers : Functions
round( ) rounds off a float number to the digits needed.
a=round(4.3333,2) # returns 4.33
a=round(4.33) # returns 4
a=round(4.33,0) # returns 4.0
Note: The string methods would return a new string rather than modifying
the current one
Strings : Methods
Testing conditions in a string
s.startswith(“he”)
s.endswith(“run”)
s.isalnum(), s.isalpha(), s.isdigit(), s.isupper(), s.islower(), s.isspace()
Sequences
len( ) # gives the total number of elements in a sequence
in # can be used to check for existence of a value in the sequence
not in
[] # can be used to access an element or a slice of a sequence
# most sequences allow this operator
in # used to iterate through the sequence using for statement
Sequences - Example
A string is an ordered immutable sequence
s=“Hello”
len(s) # gives the total elements in the sequence/string
if ‘e’ in s: # use of in operator to check for existence
s[1] # accesses character ‘e’, indexes start from 0
s[1:4] # gives a slice of the sequence ‘ell’
for c in s: # iterates one character at a time from s into c
s[1]=‘c’ # not allowed, as string is immutable
Sequences
• Sequences are collection of items
• Sequence items can be of any python type
• Sequences may be ordered or unordered
• Ordered sequences maintain order of elements
• Unordered sequences do not guarantee order of elements
• Sequences may be mutable or immutable
• Mutable sequences allow elements to be modified
• Immutable sequences do not allow elements to be modified
Slices in detail
• A slice is a part of a sequence, returned as a new sequence
• The original sequence remains unchanged
• The [ ] brackets are used as a slice operator
b=a[1] # b is a single element slice of a
b=a[2:5] # slice is a[2],a[3] and a[4]
b=a[2:7:2] # slice is a[2], a[4], a[6] the index gap is 2 for elements
a[start,end,stride]
slice starts from the index start, does not include the index end, and stride is
the gap after which the elements are selected. By default stride is 1
Slices in detail
• Negative index implies from the end of the sequence
• Index -1 refers to the last element
• Index -2 refers to the second last element and so on
s=‘ABCDEF’
print(s[-2]) # would print ‘E’
print(s[2:-2]) # prints ‘CD’ as the ending index is not included
Slices in detail
• Negative stride implies traversing backwards
• The start index and end index should be logically correct
s=‘ABCDEF’
print(s[-1:0:-1])
-- DEMONSTRATION
Lists
• Lists are ordered mutable sequences
• Lists can be compared to heterogeneous arrays
• Lists can be increased or decreased dynamically
-- DEMONSTRATION
Lists building by comprehension
A=[ x for x in range(2,10,2) ]
• Get a list of words with their count in a sentence. The words should be
in sorted order alphabetically. Words should not be repeated. Assume
space as a separator for words.
-- DEMONSTRATION
Lab Exercises
• Input a sentence and display the longest word
• Input a sentence and input a word. We have to find out how many times
the word occurred in the sentence.
• Display all the words that are contained in a sentence. Words that come
multiple times should be displayed only once.
• We wish to display only those words from the sentence that do not occur in
a set of excluded words ( like : this, and, is, not etc. ). The set of excuded
words would be maintained in your code, and the sentence is input from
the keyboard. We have to display all words that do not exist in this set, and
we have to take care that multiple occurring words are displayed only once.
• Display the density of each word in a long sentence. The displayed words
should not exist in the set of excluded words. However to calculate the
density, we would use the total number of words contained in the
sentence.
Thank you