Python Programming: Lesson 65 - JSON Objects

Опубликовано: 05 Февраль 2025
на канале: youmils03
61
0

PLEASE SUBSCRIBE!!!

In the previous two videos, we characterized matrices, which are lists of tuples of lists or tuples with elements that can be added, subtracted, or sorted. Any class that implements the proper magic methods, or more basically the ints floats and strings, can be placed into a matrix and dealt with using the static methods
Lesson 63:    • Python Programming: Lesson 63 - Techn...  
Lesson 64:    • Python Programming: Lesson 64 - More ...  

To add two matrices together, add their corresponding entries
To subtract two matrices, subtract their corresponding entries
To find the minimum or maximum element in a matrix, make sure that you have a _lt_ magic method in your class. Then, find the minimum in each list or tuple within the matrix, collect it into a list of minimum elements, and then find the minimum within that. Maximum is similar.
The number of entries in a matrix is given by the number of rows times the number of columns.
The transpose of a matrix swaps the rows and columns.

We've also built up at least a small understanding of the exec function and the eval function, as well as iterators.
exec(x) accepts a String x and executes it as Python code
eval(x) accepts a String x and gives the literal value of it
y = iter(x) accepts an iterable x and returns an iterator on it
z = next(y) returns the next element in the data structure x

MAIN TOPIC IN VIDEO

In this final video, we learn about JSON objects. Software engineers often use this data type, but it is essentially just data wrapped into strings. We import a module called json, learn how to rename a module, and then operate with JSON objects in this video.

import m imports a module
from m import f imports a function within a module
from m import f as g imports and renames a function within a module
import m as n Imports and renames a module

import the json module

json.loads(x) accepts the string representation of a dictionary x and returns the actual dictionary
json.dumps(x) accepts a Python object and generally returns the JSON string representation of it
True comes back as true
False comes back as false
a list or tuple comes back as a JSON array, which has square brackets
None comes back as null, not discussed in this video

As a followup for more research, study
json.dumps( x , indent = y , separators = z )
indent and separators are keyword arguments
y is the number of spaces to insert before each key-value within the JSON string returned after accepting a dictionary x
z is a tuple with two strings. One is the separator between each key-value pair in the JSON string, and the other is the string appearing between each key and value, usually a colon :

0:54 Last Look at Frozen Sets, call, contains, Iterators, exec, eval, and Matrices
2:48 Example with Iterators
5:27 Example with eval(x)
6:25 Example with Matrices
11:03 NEW: Intro to JSON
12:19 NEW: Importing a Module by a Different Name (import m as w, then call w.f( ) for some function f( ) in the module m)
13:32 NEW: json.loads( s ) accepts a JSON string and returns a Python dictionary
15:58 NEW: json.dumps( x ) accepts a dictionary x and returns a JSON string representing that dictionary. It can also accept an int or float and return a number. It can also accept True or False and return "true" or "false". It can also accept a list or tuple and return an array in square brackets, in double quotes.

Ready for the next lesson? The show goes on, even though you weren't thinking it would!    • Python Programming: Lesson 66 - Froze...  

Thanks for watching, and PLEASE SUBSCRIBE!!!