Python Course Slides
Python Course Slides
Python Course Slides
•
Welcome to Python!
1. Python is more straightforward than other programming languages
and ideal for beginners. Most programs require considerably fewer
lines of code to perform the same task compared to other languages
2. Python is an efficient language. Python’s syntax helps you write
“clean” code, easy to read, easy to debug, and easy to extend and
build upon.
3. Python has a wide range of use. You can make games, build web
applications, and solve business problems. Python is also used
heavily in scientific fields for academic research.
4. Python has a large number of third-party tools, such as Pyinstaller
that allow us to package our Python code into stand-alone
executable programs for operating systems like Windows and Mac
OS
5. Python has a great Community. Having a supportive community is
critical in solving problems, especially in your first programming
steps.
List of the most common uses for Python
Designing mathematic, scientific, and
engineering applications
If you need to work with a Web service, the primary method for
exchanging information on the Internet (or any other XML
intensive application), Python is a great choice.
1. Fonts/Tabs
2. Highlights
3. Keys
4. General
5. Extensions
Hello World! - Writing our First Program
Hello World!
Adding Comments
COMMON REASONS TO USE COMMENTS
IN YOUR CODE
❑ Reminding yourself about what the code does, and why you
wrote it
❑ Telling others how to maintain your code
❑ Making your code accessible to other developers
❑ Listing ideas for future updates
❑ Maintaining a list of improvements, you’ve made
How to Find Help
TWO MAIN KINDS OF ERRORS
❑ www.pythontutor.com
Five (5) Beginner Tips for Learning python
Programming
Coding Every Day
❖ Pomodoro Technique
❖ Timetable breaks into your schedule
❖ Sleep is important for memory formation
❖ Fresh eyes spot ‘obvious’ errors
Ask “Good” Questions
I’ve tried:
Tried everything print(f”{var}”)
print(f”{:03.1f var}”)
Ask “Good” Questions
O: Offer the best guess as to what the problem might be.
This helps the person who’s helping you not only to know
what you’re thinking, but also to know that you’ve done
some thinking on your own.
var=34.6
print(“{:06.1f}”.format(var))
# this works, but it's not an f-
string
Variables
List of reserved keywords in Python 3.9
❖ Addition
❖ Subtraction
❖ Multiplication
❖ Division
❖ Floor division (also called Integer Division)
❖ Modulo
❖ Exponentiation
❖ Unary Positive
❖ And Unary Negation
Comparison Operators
Logical and Chained Comparison Operators
Logical Operators
BASIC CONCEPTS RELATED TO BOOLEAN
LOGIC IN PYTHON
The + Operator
➢ Concatenates strings
➢ Returns a string consisting of the
operands joined together
STRING OPERATORS
The * Operator
➢ Creates multiple copies of a string
➢ Returns a string consisting of n
concatenated copies of a string
STRING OPERATORS
The in Operator
❖ A membership operator that can be used
with strings
❖ Returns True if the first operand is
contained within the second
❖ Returns False otherwise
❖ Also can be used as not in
BUILT-IN STRING FUNCTIONS
ESCAPE CHARACTERS
1 SOH HT DC1 EM ! ) 1 9 A I Q Y a i q y
5 ENQ CR NAK GS % - 5 = E M U ] e m u }
a p p l e
0 1 2 3 4
String Indices
STRING INDEXING
-5 -4 -3 -2 -1
a p p l e
0 1 2 3 4
String Indices
String Slicing
STRING SLICING
-7 -6 -5 -4 -3 -2 -1
a v o c a d o
0 1 2 3 4 5 6
d='avocado'
d[1:4]
Dates and Times
datetime provides three classes that make up the high-level
interface that most people will use:
➢ From zero
➢ To as many as your computer’s memory will allow
➢ A list with a single object is sometimes referred to as a
singleton list
List elements can be accessed by index
0 1 2 3
LISTS - INDEXING AND SLICING
➢ List Indexing
a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
0 1 2 3 4 5
List Indices
LISTS - INDEXING AND SLICING
➢ Negative Indexing
-6 -5 -4 -3 -2 -1
'spam' 'egg' 'bacon' 'tomato' 'ham' 'lobster'
0 1 2 3 4 5
List Indices
LISTS - INDEXING AND SLICING
➢ List Slicing
a[2:5]
-6 -5 -4 -3 -2 -1
'spam' 'egg' 'bacon' 'tomato' 'ham' 'lobster'
0 1 2 3 4 5
List Indices
LISTS - INDEXING AND SLICING
0 1 2 3 4 5
List Indices
Tuples
Python Tuple Basics
2, 4, 6, 8 4 1, 4, 9
WHAT IS A SET IN PYTHON?
➢ if condition 1 is met:
➢ do A
➢ elif condition 2 is met:
➢ do B
➢ elif condition 3 is met:
➢ do C
➢ else: do D
for Loops
FOR LOOPS
for an in iterable:
code
while Loops
WHILE LOOPS
Creating Stacks Using Lists
LIFO — LAST IN, FIRST OUT
A B C D A
D
“PUSHING” TO THE STACK
A D
B C
“PUSHING” TO THE STACK
A
“POPPING” THE STACK
A
“POPPING” THE STACK
B
C
A
“POPPING” THE STACK
B C
A
“POPPING” THE STACK
A D
B C
Ask user for birth date Calculate # of days old Display the result
Properties Behaviors
• Name • Walk
• Age • Talk
• Address • Breathe
Classes in Python
CLASSES
• Classes are used to create objects
• We can create many, unique objects from a single class
• Classes define a type
• The process of creating an object from a class is called
instantiation
Enemy Class
Properties:
• name
• health
• Power_level
Behaviors:
• attack()
• takedamage()
• defeat()
EXAMPLE 2: WEB BROWSER
Tab Class
Properties:
• title (str)
• is_current (bool)
• page (Page)
Behaviors:
• close()
• reload()
EXAMPLE 2: WEB BROWSER
Class Dog:
pass
INSTANCE ATTRIBUTES
Person Baby
OBJECT INHERITANCE
Person Baby
CLASS HIERARCHY All classes inherit from object,
including the built-in types like
int and str
• Python classes can inherit
from one another Object
• Here, a baby is a person, and
a person is an object.
str int Person
Therefore, a baby is also an
object. Built-in types
• All objects are of type
Baby
objects, as well as any more
specific types like Person Custom types
Inheritance Example
CONCLUSION
● Simplicity
● Maintainability
● Reusability
● Scoping
PYTHON MODULES AND PACKAGES
❖ Writing a Module
❖ The Module Search Path
❖ The import Statement
❖ The dir() Function
❖ Executing a Module
❖ Reloading a Module
❖ How to Install a Package in Python using PIP
❖ Python Packages
❖ Create Graphics & Shapes Using Python’s Turtle
Module
Writing a Module
WRITING A MODULE
pkg
mod1.py
mod2.py
Python's open, read from, and write to a file
The open function
open(file_name, mode)
Copying:
❑ shutil.copy(src, dst) copies a file (but not its metadata) from the src path
to the dst path — if you need the metadata, use copy2
❑ shutil.copytree(src_dir, dst_dir) copies full directory trees
Moving/Renaming:
❑ shutil.move(src, dst) moves a file or directory from src to dst
❑ os.rename(old, new) renames a file or directory
Dealing with errors
DEALING WITH ERRORS
MATH GAME
Part 1 Part 2
math-Functions.py math-Game.py
Thank you !!
Project:
Guessing Game: version 2
Check out my other courses!
Please leave your feedback
and an honest review of the course