Tag Archives: Python
Many of us are still in coronavirus lockdown. We are very proud of all of you for staying healthy and safe.😀
In today’s google classroom, we ordered some pizza for lunch. 🍕 We made a Python program to calculate 👩💻 which pizza size gives us the best value.
Our goal: to find the lowest price per bite. To get that, we divide cost by size.
def pizzaBitePrice(radius,price): area=radius**2*3.14 unit_price = price/area return(unit_price) # small pizza pizzaBitePrice(7,12) 0.07799298063174313 # medium pizza pizzaBitePrice(8,14.75) 0.06593351910828026 # large pizza pizzaBitePrice(9,14.75) 0.057993237398757565
👍The conclusion is: the large size one gives us the best value with price of 5.7 cents (plus tax) per bite.
Hello everybody!😃 Welcome back to Magic Math Mandarin. Since we are staying home because of covid-19, we brought our classes to google classroom so our students can keep on learning even during this pandemic👩🏫. Now we can all communicate with each other online💻. In this classroom we will be learning Chinese🈷, math➗, and programming👩💻. Our teachers will put new assignments everyday about each topic. If you would also like to join our wonderful classrooms then here is the class code mtxl6j4
Remember to stay home and don’t get sick!😷 Please join our classroom today!👍💖
In this past weekend’s coding class we practice keyboard ⌨ shortcuts in the interactive Python shell (Ipython).
Since most of the students are learning typing ⌨, practicing these keyboard shortcuts actually help them learn typing by reinforcing memory of where the keys are.
The keyboard shortcuts allows you to minimize finger movements on the keyboard, which means you don’t even have to lift your hand ✋ for the “Backspace”, ‘Home’, ‘End’ or arrow keys (most of the time). Spend 15 minutes per day practicing these. Pretty soon, you will be a fast coder.
||Move cursor to the beginning of the line|
||Move cursor to the end of the line|
||Move cursor back one character|
||Move cursor forward one character|
|Backspace key||Delete previous character in line|
||Delete next character in line|
||Cut text from cursor to end of line|
||Cut text from beginning of line to cursor|
||Yank (i.e. paste) text that was previously cut|
||Transpose (i.e., switch) previous two characters|
||Access previous command in history|
||Access next command in history|
||Reverse-search through command history|
||Clear terminal screen|
||Interrupt current Python command|
||Exit IPython session|
We are really proud of our co-founder(联合创始人 lián hé chuàng shǐ rén), Sarah Chen, for her new book release, which is now public. Congratulations!
Her book is available in various outlets:
Here is her pre-release announcement on LinkedIn in July this year.
This Saturday class we went over indexing and ordering a group of items by their sorted indices. For those who are more advanced, please go over the section on quick sort.
>> import numpy as np
>>> packpack =np.array([‘snack’,’book’,’pen’,’eraser’,’apple’])
# Position of the biggest word (alphabetically)
# Position of the smallest word (alphabetically)
# Position of the words if we are to sort them alphabetically
[out]: array([4, 1, 3, 2, 0], dtype=int64)
Now let us sort them:
[out]: array([‘apple’, ‘book’, ‘eraser’, ‘pen’, ‘snack’], dtype='<U6′)
Then we tried sorting numbers:
numbers = np.array([2,3,5,7,1,4,6,15,5,2,7,9,10,15,9,17,12])
[out]: array([ 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 9, 9, 10, 12, 15, 15, 17])
Finally we dig deeper: how do you really sort things fast systematically? Using quick sort!
def quick_sort(data): """快速排序""" if len(data) >= 2: # 递归入口及出口 mid = data[len(data)//2] # 选取基准值，也可以选取第一个或最后一个元素 left, right = ,  # 定义基准值左右两侧的列表 data.remove(mid) # 从原始数组中移除基准值 for num in data: if num >= mid: right.append(num) else: left.append(num) return quick_sort(left) + [mid] + quick_sort(right) else: return data numbers= [2,3,5,7,1,4,6,15,5,2,7,9,10,15,9,17,12] Backpack = ['snack','book','pen','eraser','apple'] print(quick_sort(Backpack)) print(quick_sort(numbers))
%save sessionName linesToKeep
This will save script in line numbers you specify in linesToKeep,for example, 1-20 34-50 64 into a file called “sessionName.py” in your current working directory.
If you are not so specific on which lines you want to keep, you can save everything.
%save sessionName ~0/
This saves everything from the current session, denoted as “~0″ into file “sessionName.py” in your cwd (current working directory).
%save pastSession ~1/
This saves everything from the past session, denoted as “~1” into file “pastSession.py” in your cwd.
It has been over a year since I last used tensorflow. Not only library versions have changed, but also the syntax. My old jupyter notebook was throwing errors all over the place this morning. It is time to update everything.
For tensorflow, I am using Python 3.6.3 (I used to use Python 3.5), and numpy 1.16.1. TensorFlow has a few dependencies. numpy is one of them. Note the mnist dataset has 70,000 images.
python -c ‘import tensorflow as tf; print(tf.__version__)
Here is a really great collection of Python notebooks with lots and lots of links. We start with some appetizers:
- matplotlib – 2D and 3D plotting in Python
- Basic Python tutorial
- Numeric Computing is Fun
- Python for Developers
- Exploratory computing with Python
But there are so many and so much more! You can find them from this page:
- Linear algebra with Cython. A tutorial that styles the notebook differently to show that you can produce high-quality typography online with the Notebook. By Carl Vogel.
|Python type||NumPy type||pandas dtype||Usage|
|int||int_, int8, int16, int32, int64, uint8, uint16, uint32, uint64||int64||Integer numbers|
|float||float_, float16, float32, float64||float64||Floating point numbers|
|datetime64[ns]||datetime64||Date and time values|
|timedelta[ns]||Differences between two datetimes|
|category||Finite list of text values|
Note that what is “str” in Python is called “object” in pandas. This is a potential source of confusion (isn’t everything in Python an object?).