welcome to cs50 section! this is week 9. - brandon wang€¦ · you must know the basics of python...
TRANSCRIPT
![Page 1: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/1.jpg)
Welcome to CS50 section! This is Week 9.● Final project official proposals: due next Friday at noon
● Add to your calendar:○ Final project status report: Due Monday, Nov 28 at noon
(Halfway point; be at least ⅓ done)
● The “quiz” (aka the second midterm) is Monday 11/14 through Thursday 11/17○ Same “take home, no collaboration” policy
![Page 2: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/2.jpg)
Welcome to Python
Python lets us write smarter programs, faster.
Course timeline:Raw C codeDistribution C codeRaw Python codeFramework Python code (Flask)HTML/CSSJavaScriptJavaScript frameworks (jQuery)
![Page 3: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/3.jpg)
Before starting pset 7
● Conceptual basics of Python● Flask
○ Decorators and routes○ MVC in the context of Flask
● SQL queries
![Page 4: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/4.jpg)
Final projectroundtable
![Page 5: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/5.jpg)
Conceptual basics
Review Week 8 slides (http://brandon.wang/cs50)
● You must know the basics of Python to proceed!○ Syntax, structures, loops, data types
● Most people will make a final project in Python/Flask.
● Less important to carry over skills from C,More important to know how Python implements things
![Page 6: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/6.jpg)
Brief review
● Data types in Python○ Lists○ Tuples○ Dictionaries
● Function definitions○ Optional arguments
![Page 7: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/7.jpg)
Functions
In Python, functions are first-class objects.
● Data type like everything else● Can be overridden● Can be passed around (although try not to)
![Page 8: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/8.jpg)
![Page 9: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/9.jpg)
Functions
Why does this matter?
● Python’s flexible definition of a data type is a design choice● Can’t call a function the same name as a variable● Same scoping of a variable applies to a function
![Page 10: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/10.jpg)
Decorators
Decorators in Python are functions that modify the behavior of other functions, typically applying some extra functionality hereto.
![Page 11: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/11.jpg)
Decorators
Decorators in Python are functions that modify the behavior of other functions, typically applying some extra functionality hereto.
● Within the context of CS50/pset 7, decorators set the “route”, require users to be logged in, etc.
● Within Python, decorators are a simple way of adding wrapper functionality to a program.
![Page 12: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/12.jpg)
Decorators → Contrived example
def override(func):
def incr():
return func() + 1
return incr
@override
def one():
return 1
# What happens?
print(one())
![Page 13: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/13.jpg)
Routes
● Think of routes as pathways to functions● We’re mapping URLs to functions
○ It’s a many-to-one relationship
![Page 14: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/14.jpg)
Routes
● Think of routes as pathways to functions● We’re mapping URLs to functions
○ It’s a many-to-one relationship
● In Flask, routes are defined using a decorator:@app.route()
![Page 15: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/15.jpg)
Routes
A really simple Flask app might look like:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "You are at the index!"
![Page 16: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/16.jpg)
MVC
In pset 7, we use a loose form of MVC.
● Models and controllers go in application.py.● Set up your views (eg. visual layouts) through Jinja templates.
![Page 17: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/17.jpg)
MVC
What are the distinctions to know?
● Routes don’t map to URLs (many URLs to one route)● Routes do map to functions (many routes to one function)
● Views/templates don’t map to routes● Views/templates don’t map to anything● Views/templates are just things that your functions can call
![Page 18: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/18.jpg)
Jinja
Flask views are primarily structured through Jinja.
Jinja is a Python-inspired language for making templates, ways of showing things (rendering) in the browser.
![Page 19: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/19.jpg)
Jinja
Flask views are primarily structured through Jinja.
Jinja is a Python-inspired language for making templates, ways of showing things (rendering) in the browser.
● Cool things with Jinja○ Jinja templates cascade (i.e. extend)○ Jinja templates interweave HTML and Python
Not too much explanation here-- read problem spec.
![Page 20: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/20.jpg)
SQL
![Page 21: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/21.jpg)
Databases and SQL
![Page 22: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/22.jpg)
Databases and SQL
● Key elements of database design○ Databases have multiple tables○ Columns have data types○ Tables have primary keys○ Tables have commonalities
![Page 23: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/23.jpg)
SQL
SQL queries are statements that you send to a database server.The server responds according to your statement.
In Flask, use db.execute() to run SQL queries.
SQL = “Structured query language”
![Page 24: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/24.jpg)
SQL
Do programmers write SQL queries anymore? (Not really.)But the underlying mechanism generally continues to be SQL.
![Page 25: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/25.jpg)
SQL
SQL obeys the CRUD process:
● C: Create● R: Read● U: Update● D: Delete
![Page 26: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/26.jpg)
SQL
SQL obeys the CRUD process:
● C: Create○ INSERT INTO
● R: Read○ SELECT
● U: Update○ UPDATE
● D: Delete○ DELETE
![Page 27: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/27.jpg)
SQL → Base vocabulary
Insertions:“INSERT INTO <table> (<columns>) VALUES (<values>)”
Selections:“SELECT <columns> FROM <table>”
Updates:“UPDATE <table> SET <column1> = <value1>, <column2> = <value2>”
Deletions:“DELETE FROM <table>”
![Page 28: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/28.jpg)
SQL → More vocabulary
All of these are usually paired with conditions, etc.:
● WHERE is nearly always included○ Limits the scope of the query
● JOIN lets operations on multiple tables occur
![Page 29: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/29.jpg)
SQL example
![Page 30: Welcome to CS50 section! This is Week 9. - Brandon Wang€¦ · You must know the basics of Python to proceed! Syntax, structures, loops, data types Most people will make a final](https://reader034.vdocuments.site/reader034/viewer/2022052009/601eee089519bf3b837cdd2e/html5/thumbnails/30.jpg)
That’s all for today!