Python
Programming
Foundations
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Course Intro
What is Python?
Where can Python
be used?
What this course offers
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Installing and Configuring Python
Python 2.x or 3.x?
Installing Python
Adding Python to the PATH
Python in Interactive Mode (terminal)
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Hello World!
Simplistic Hello World program in
Python Command Line (Python CLI)
Basic syntax rules
Saving program to a file, and
executing with Python
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Variables
(x)
Variable Assignments
Legal and Illegal variable
naming conventions
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Data Types
Strings
Integer
Float
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Data Types
Boolean
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Checking datatypes
Type function
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Strings
Accessing string values
- indexing
- slicing
Formatting strings
- Format method
Changing strings
- type function
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Strings
String specific operators
- Concatenation
Built-in string methods
- index()
- isupper()
- islower()
https://www.w3schools.com/python
/python_ref_string.asp
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Strings
User Input
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Lists
Creating lists
- similar to creating variables
Accessing list values
- Indexing
- Slicing
Appending to lists
- append()
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Lists
Matrixes
- List of lists
Built-in functions and methods
https://www.w3schools.com/python
/python_ref_list.asp
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Tuples
Creating tuples Removing elements
Accessing tuple values Tuple operations
Updating tuples Built-in methods
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Tuples
What is the difference between
Tuples and list?
Why would you want to use a
tuple in place of a list?
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Int, Long, Float, Complex
Assigning a number to a variable
Type conversion
Random numbers and their functions
Mathematical functions
Math based constants
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Basic Operators
Arithmetic Operators
Relational Comparison
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Conditional Flow
How the concepts of Boolean
applies to Conditionals.
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Conditional Flow
Single if statements
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Flowchart for simple if statement
True False
Condition
Statement 1 Statement 2
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Conditional Flow
Nested if statements
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Flowchart of nested Conditional
False x>y True
No x>y True Statement_a
Statement_c Statement_b
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Loops
For loops
While loops
Nesting loops
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Control Statements
Continue
Break
Pass
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Exercises
Create a program “calculator.py” that performs
some basic math.
Your calculator program should be able to:
1. Add two numbers
2. Subtract two numbers
3. Divide two numbers
4. Multiple two numbers
5. Calculate 8% tax on a value
6. Calculate monthly payments, given a total
amount and a number of years
7. Quit
It should run in an infinite loop. Use the
following skeleton code:
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
while (true):
#print out the menu
#use the input() function to determine
the user’s choice…
#userChoice = input(“Please choose an
option from 1-6)
if userChoice = 1:
#add numbers
elif userChoice = 2:
#subtract numbers
… (you will have to fill in the rest) …
elif userChoice == 7:
break
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Expected Output
Calculator v1.0
MENU
1. Add two numbers
2. Subtract two numbers
3. Divide two numbers
4. Multiple two numbers
5. Calculate 15% tax on a value
6. Calculate monthly payments, given a
total amount and a number of years
7. Quit
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Expected Output (cont.)
Please enter your choice: 1
Number 1: 5
Number 2: 3
Result: 8
MENU
1.Add two numbers
2.Subtract two numbers
3.Divide two numbers
4.Multiple two numbers
5.Calculate 15% tax on a value
6.Calculate monthly payments, given a total amount
and a number of years
7.Quit
# And it continues till option 7 is selected to quit.
Please enter your choice:
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Dictionaries
Construction of Dictionaries
- Curly braces
- creating a dictionary
Why dictionaries and not tuples
and lists?
- Speed
Changing dictionary elements
- update method
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Dictionaries
Removing dictionary elements
- del statement
- pop method
Key properties
- More than one entry per key
not allowed
- Keys are immutable
- keys are case sensitive
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Dictionaries
Built-in functions and methods
https://www.w3schools.com/pyt
hon/python_ref_dictionary.asp
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Functions
Defining a function
- def
Calling a function
- function()
Arguments: Reference
vs Value
Keyword Arguments
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Functions
Default Arguments
Anonymous functions
- lambda functions
Return keyword
Variable scopes
(Global/ Local
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Functions Exercises
def introduction(name, height=165):
#Greets user with 'name' from 'input box' and ‘height', if
available, default height is used
print('Hello ' + name + ', you are ' + str(height) +’cm!’)
print(f'Hello {name}, you are {height}cm!')
name = input('Enter your name: ‘)
height = input('Enter your height: ')
greeting(name, 165)
# 1. Add new print statement - on a new line
# which says 'We hear your hair color is xxx! xxx is a string with
color
# 2. extend the function with another input parameter ‘hair_color',
that defaults to 'red'
# 3. Capture the hair color via an input box as variable:hair_color
# 4. Capitalize first letter of the 'name', and rest are small caps
# 5. Hair color should be in lowercase
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Modules
What is a module?
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Modules
Importing a module
Module location
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Date and Time
The time module
The calendar module
TimeTuple
Getting current time
Getting formatted time
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Input Output
Print to the screen
Gathering keyboard input
Reading and writing files
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Exceptions
Standard exceptions
Raising an exception
Catching and handling exceptions
Defining custom exceptions
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Classes and Objects
Syntax
class ClassName:
def __init__(self, attribute1, attribute2…):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method1(self):
#do something…
#Classes are blueprints
#Objects are the actual things you built
#variables => attributes
#functions => methods
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Classes and Objects
OOP Terminology
Object-oriented programming is a programming paradigm that
provides a means of structuring programs so that properties and
behaviors are bundled into individual objects.
Creating classes
class ClassName:
Instance objects
object = ClassName(attribute1, attribute2,…)
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Classes and Objects
Garbage Collection Inheritance
Python deletes unwanted objects (built-in Inheritance allows us to define a class that
types or class instances) automatically to inherits all the methods and properties
free the memory space. The process by from another class.
which Python periodically frees and Parent class is the class being inherited
reclaims blocks of memory that no longer from, also called base class.
are in use is called Garbage Collection Child class is the class that inherits from
another class, also called derived class.
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Inheritance Example
Parent class Child class
class Person: class Student(Person):
def __init__(self, fname, lname): pass
self.firstname = fname
self.lastname = lname x = Student(’Tom’, ‘Ford‘)
x.printname()
def printname(self):
print(self.firstname, self.lastname)
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Overrides
When you add the __init__() function, the child class will no
longer inherit the parent's __init__() function.
Therefore the child overrides the inheritance of the Parent’s __init__()
function.
To keep the inheritance of the parent's __init__() function, add a call to
the parent's __init__() function.
class Student(Person):
def __init__(self, fname, lname): #overrides parent’s inheritance
Person.__init__(self, fname, lname) #keeps parent’s inheritance
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
GUI
GUI Concepts
Popular GUI libraries and toolkits
tklnter
Build a GUI (code available)
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.
Building a Complete App
Using what we’ve learned, we are
going to build a functional currency
converter application.
©2021 by StormWind LLC. All rights reserved.
No part of this book may be reproduced in any written, electronic, recording, or photocopying without written permission of StormWind LLC.