Network Programming
with Python3
Python Objects and
Data Structure Basics
Basic Data Types
Data Types
Name Type Description
Integers int Whole numbers, such as: 3 300 200
Floating point float Numbers with a decimal point: 2.3 4.6 100.0
Strings str Ordered sequence of characters: "hello" 'Sammy' "2000" "楽しい"
Lists list Ordered sequence of objects: [10,"hello",200.3]
Dictionaries dict Unordered Key:Value pairs: {"mykey" : "value" , "name" : "Frankie"}
Tuples tup Ordered immutable sequence of objects: (10,"hello",200.3)
Sets set Unordered collection of unique objects: {"a","b"}
Booleans bool Logical value indicating True or False
Numbers
Numbers
● There are two main number types we will
work with:
○ Integers which are whole numbers.
○ Floating Point numbers which are
numbers with a decimal.
● Let’s explore basic math with Python!
● We will also discuss how to create
variables and assign them values.
Numbers
Variable Assignments
Variable Assignments
○ my_dogs = 2
○ a = 6
○ B = ‘abc’
Variable Assignments
● Rules for variable names
○ Names can not start with a number.
○ There can be no spaces in the name, use
_ instead.
○ Can't use any of these symbols
:'",<>/?|\()!@#$%^&*~-+
Variable Assignments
● Rules for variable names
○ Avoid using words that have special
meaning in Python like "list" and "str"
Variable Assignments
● Python uses Dynamic Typing
● This means you can reassign variables to
different data types.
● This makes Python very flexible in assigning
data types, this is different than other
languages that are “Statically-Typed”
Variable Assignments
my_keys = 2
my_keys = [ “Golden” , “silver” ]
Variable Assignments
my_keys = 2
my_keys = [ “Golden” , “Silver” ]
Variable Assignments
int my_dog = 1;
my_dog = “Sammy” ; //RESULTS IN ERROR
Strings
Strings
● Strings are sequences of characters, using
the syntax of either single quotes or double
quotes:
○ 'hello'
○ "Hello"
○ " I don't do that "
Strings
● Because strings are ordered sequences it
means we can using indexing and slicing
to grab sub-sections of the string.
● Indexing notation uses [ ] notation after the
string (or variable assigned the string).
● Indexing allows you to grab a single
character from the string...
Strings
● These actions use [ ] square brackets and a
number index to indicate positions of what
you wish to grab.
Character : h e l l o
Index : 0 1 2 3 4
Strings
● These actions use [ ] square brackets and a
number index to indicate positions of what
you wish to grab.
Character : h e l l o
Index : 0 1 2 3 4
Reverse Index: 0 -4 -3 -2 -1
Strings
● Slicing allows you to grab a subsection of
multiple characters, a “slice” of the string.
● This has the following syntax:
○ [start:stop:step]
● start is a numerical index for the slice start
Strings
● Slicing allows you to grab a subsection of
multiple characters, a “slice” of the string.
● This has the following syntax:
○ [start:stop:step]
● start is a numerical index for the slice start
● stop is the index you will go up to (but not
include)
● step is the size of the “jump” you take.
String Formatting
for Printing
String Formatting
● Often you will want to “inject” a variable into
your string for printing. For example:
○ my_name = “Jose”
○ print(“Hello ” + my_name)
● There are multiple ways to format strings for
printing variables in them.
● This is known as string interpolation.
String Formatting
● Let’s explore two methods for this:
○ .format() method
○ f-strings (formatted string literals)
Lists
Lists
● Lists are ordered sequences that can hold a
variety of object types.
● They use [] brackets and commas to
separate objects in the list.
○ [1,2,3,4,5]
● Lists support indexing and slicing. Lists can
be nested and also have a variety of useful
methods that can be called off of them.
Dictionaries
Dictionaries
● Dictionaries use curly braces and colons to
signify the keys and their associated values.
{'key1':'value1','key2':'value2'}
● So when to choose a list and when to
choose a dictionary?
Comparison
● Dictionaries: Objects retrieved by key
name.
Unordered and can not be sorted.
● Lists: Objects retrieved by location.
Ordered Sequence can be indexed or sliced.
Tuples
Tuples
Tuples are very similar to lists. However they
have one key difference - immutability.
Once an element is inside a tuple, it can not be
reassigned.
Tuples use parenthesis: (1,2,3)
Sets
Sets
Sets are unordered collections of unique
elements.
Meaning there can only be one representative
of the same object.
Let’s see some examples!
Booleans
Booleans
Booleans are operators that allow you to
convey True or False statements.
These are very important later on when we
deal with control flow and logic!