Module01 wk01b DataTypes
Module01 wk01b DataTypes
Types
Networking for Software Developers
Narendra Pershad
Agenda
• It is expected that you would have prior knowledge of C# primitive type and C# collections so you may start
using the python types immediately.
Python built-in types
• None:
• The Null object
• Numerics:
• Integral
• int, bool
• float, complex
• Sequences:
• Immutable
• Strings, Tuples and Bytes
• Mutable
• Lists, Byte Arrays
• Set types:
• Set (mutable) and Frozen sets (immutable)
• Mappings:
• Dictionary
• All categories are a part of the python language i.e. you don’t need separate libraries to use them
• P.S. to know more about any object type help(object) or dir(object)
Numeric
Numbers are immutable, this means that values in memory are not mutated, another spot is chosen with
the new value
• Float literals
3.14, 10., .001, 1e100, 3.14e-10
• Complex literals
3.14j, 10.j, 10j, .001j, 1e100j, 3.14e-10j
Conversions
>>> int(3.14)
3
>>> float(5)
5.0
>>> complex(3)
(3+0j)
>>> complex(1, 2)
(1+2j)
Bool
• This is a numeric sub-type
• There are only two literal bool values:
• True
• False
• Unlike C#, however any python object can be tested for truth value. The following values will be considered False:
• None
• False
• Reminder
• To see all the variables in scope, use the dir() command
• To get type information on a variable, use the type(var) command
• To facilitate the introspection of a variable, use the dir(var) command
• To get help on a topic, use the help(topic)
String
• Strings are text delimited by a matching pair of single, double or triple quotes
• In this course, we will use a single quote for strings with a few exception
>>> a = 'Hello world'
>>> a
'Hello world'
>>> type(a)
<class 'bytes'>
>>> ord(a[0])
196
>>> chr(196)
'Ä'
>>> a = 'the quick brown fox jumps over the lazy dog'
>>> a.split()
['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog’]
>>> a.capitalize()
'The quick brown fox jumps over the lazy dog’
>>> a.title()
'The Quick Brown Fox Jumps Over The Lazy Dog’
>>> a.find('dog')
40
Tuples
• A sequence of immutable python objects.
• This means that it may not be changed after assignment
tup1 = ( )
tup3 = ( 1, 2, 3, 4, 5, 3, 2, 1 )
lst3 = [1, 2, 3, 4, 5, 3, 2, 1]
Lst2[-3] # 'mathematics'
Common methods of List
• append()
• clear()
• copy()
• count()
• extend()
• index()
• insert()
• pop()
• remove()
• sort()
• reverse()
Traversing a sequence
• There are two way of traversing a sequence
for i in range(len(sequence)):
print(sequence[i]) # does not work with set, dictionary
append(item)
• Adds item to the end of the sequence (not available for sets or tuples)
>>> a = [2, 4, 6]
>>> a
[2, 4, 6]
>>> a.append(8)
>>> a
[2, 4, 6, 8]
• extend()
Common method of sequences
• index()
• Insert(«index», «item»)
• Inserts the item at the specified position
• pop([position])
• Removes and return the item at position from sequence
• remove(«item»)
• Removes the specified item from the list
• sort()
• reverse()
• Returns a reversed version of this sequence.
• Note the original sequence is changed
append()
• Adds a new entry to the end
clear()
• Removes all the entries of this sequence
Set
• A sequence of unique immutable python objects
• Sets, list or dict cannot be in a set
• Mimics a Mathematical set
set1 = { }
set2 = { 'programming', 'english', 'mathematics', 2018, 3.14 }
set3 = { 1, 2, 3, 4, 5, 3, 2, 1 } # only unique items will be in the resulting set
set4 = { 'Centennial', 'College' }
• Each key must be unique and immutable such as strings, numbers, or tuples. A list may not be a key
as elif if or yield
• Python has a much richer set of built-in types than most languages.
• There are also lots of functions that interacts or support these types.
• You can query the type of any variable via the dir() function.
• You can also get more information via the help() function