Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

Python - Level Two

This document provides an overview of topics that will be covered in a Python Level Two course, including advanced scope rules, object-oriented programming, errors and exceptions, modules and packages, and regular expressions. The course will allow students to learn more advanced Python concepts to facilitate using the Django web framework. It introduces key Python concepts like the LEGB scope rule and using object-oriented programming to build a card game project.

Uploaded by

Oscar Cantarero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python - Level Two

This document provides an overview of topics that will be covered in a Python Level Two course, including advanced scope rules, object-oriented programming, errors and exceptions, modules and packages, and regular expressions. The course will allow students to learn more advanced Python concepts to facilitate using the Django web framework. It introduces key Python concepts like the LEGB scope rule and using object-oriented programming to build a card game project.

Uploaded by

Oscar Cantarero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Python Level Two

Let’s learn something!


Django Bootcamp

● Welcome to Python Level Two!


● In this section we will be covering more
advanced topics that will allow you to
use Django with ease!
● Knowing these topics will make your
transition to Django very smooth!
Let’s get started!
Let’s learn something!
Part 1 - Scope
Python - Level Two
Django Bootcamp

● We’ve discussed Scope a bit in the past,


but Python’s Scope rules can sometimes
confuse beginners, so let’s quickly go
over the key rules of Python’s Scope
Django Bootcamp

● Python Scope follows the LEGB Rule:


○ Local
○ Enclosing Function locals
○ Global
○ Built-in
Django Bootcamp

● L: Local — Names assigned in any way


within a function (def or lambda)), and
not declared global in that function.
● E: (EFLs) — Name in the local scope of
any and all enclosing functions (def or
lambda), from inner to outer.
Django Bootcamp

● G: Global (module) — Names assigned at


the top-level of a module file, or declared
global in a def within the file.
● B: Built-in (Python) — Names
preassigned in the built-in names
module : open,range,SyntaxError,...
Django Bootcamp

● Let’s walk through some simple


examples that make Python’s scope
clear!
Part 2 - Object Oriented
Programming
Python - Level Two
Django Bootcamp

● Object Oriented Programming is a way


to use Python to create our own Objects.
● It can be a point of great confusion for
beginners, mainly because often it is
taught poorly!
Django Bootcamp

● Let’s try our best to save you from any


confusion by systematically showing you
the thought process behind OOP and
why we would need it.
● We will use it quite a bit for Django, so
let’s get started!
Part 3 - OOP Project
Python - Level Two
Django Bootcamp

● OOP is fundamental to becoming a good


Python programmer, so let’s get some
extra practice by building a game!
● We will use OOP to create the Card
Game War!
Django Bootcamp

● The relevant file is:


○ Part3_OOP_Project.py
Django Bootcamp

● Feel free to either treat this as a


code-along project, or attempt it on your
own first!
● Let’s get a quick look at the project!
Part 3 - OOP Project
Solutions
Python - Level Two
Part 4 - Errors and
Exceptions
Python - Level Two
Django Bootcamp

● Often times our code isn’t perfect,


meaning we run into Errors!
● But how do we actually set-up our own
Error and Exception calls?
● Let’s find out!
Django Bootcamp

● We can use these keywords:


○ Try
○ Except
○ Finally
● To dictate our code logic in case of an
error!
Django Bootcamp

● To show how this works we will be


opening files, one way to open files is to
use the open() function:
○ open(“myfile.txt”,’r’)
Django Bootcamp

● The second parameter in the open()


function dictates whether you are
opening the file for just reading, just
writing, or to do both.
● If you use the wrong one, you may get an
error!
Django Bootcamp

● Let’s use this to show how we can handle


errors!
Part 5 - Decorators
Python - Level Two
Django Bootcamp

● Decorators are an advanced tool in


Python, feel free to skip this lecture and
come back to it at another time.
● We won’t encounter Decorators until
much further into the Advanced Django
material.
Django Bootcamp

● We leave this lecture in this section


because material wise this makes the
most sense but as far as using it in the
course, it is recommended you skip this
for now and come back to it when you
see decorators mentioned again!
Django Bootcamp

● Alright, let’s get started!


Part 6 -
Modules and Packages
Python - Level Two
Django Bootcamp

● You’ve seen Python import statements,


but have probably wondered, how do
they work and how do we create our
own?
● Let’s find out!
Part 7 -
Name and Main
Python - Level Two
Django Bootcamp

● An often confusing part of Python is a


mysterious line of code:
○ if __name__ == "__main__":
Django Bootcamp

● Sometimes when you are importing


from a module, you would like to know
whether a modules function is being
used as an import, or if you are using the
original .py file of that module.
Django Bootcamp

● Let’s explore this some more, but make


sure to check out the full explanatory
text file that is in this part’s folder!
Part 8 -
Regular Expressions
Python - Level Two
Django Bootcamp

● Regular Expressions allow us to search


for patterns in Python strings.
● They can seem incredibly intimidating at
first due to their strange syntax!
● We’ll walk through the basics of regular
expressions, we will use them in Django!

You might also like