Skip to content

Commit 0aadf1e

Browse files
committed
Update NB A.2 with f-strings; update .gitignore
1 parent f97c23c commit 0aadf1e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ download
2222
import-file-test.ipynb
2323
**/Untitled.ipynb
2424
student_feedback
25-
**/www.itl.nist.gov/
25+
**/www.itl.nist.gov/
26+
mindevfit.eps

notebooks/A.2-Minimum-Deviation-Fit.ipynb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@
416416
},
417417
"outputs": [],
418418
"source": [
419-
"print(\"Refractive index estimate: {0:.5f} ± {1:.5f}\".format(nOpt, nAlpha))"
419+
"print(f\"Refractive index estimate: {nOpt:.5f} ± {nAlpha:.5f}\")"
420420
]
421421
},
422422
{
@@ -427,7 +427,7 @@
427427
}
428428
},
429429
"source": [
430-
"The expressions in curly brackets `{}` are known as a *format specifiers*, which format the arguments to the `format` method appended to the end of the string. The `.format()` at the end tells the interpreter that formatting information is contained in the string; in each format specifier, the number the precedes the colon `:` is the position index of the argument to be formatted. The expression that follows the colon then tells the interpreter how to format the variable. For example, the specifier `.5f` will represent the argument in floating-point notation to five decimal places. Examples of the most common numerical format strings are below."
430+
"The `f` that precedes the expression in quotation marks indicates that this is a formatted string literal, or [f-string](https://docs.python.org/3/reference/lexical_analysis.html#f-strings). F-strings have the format `f\"Text {variable:format}\"`, where the curly brackets `{}` include instructions for printing the value of a variable with a specified format. For example, the specifier `.5f` will represent the argument in floating-point notation to five decimal places. Examples of the most common numerical format strings are below."
431431
]
432432
},
433433
{
@@ -440,10 +440,14 @@
440440
},
441441
"outputs": [],
442442
"source": [
443-
"print(\"Field width=10, precision=5, floating point format: {0:.5f} ± {1:.5f}\".format(nOpt, nAlpha))\n",
444-
"print(\"Field width=10, precision=5, scientific notation format: {0:.5e} ± {1:.5e}\".format(nOpt, nAlpha))\n",
445-
"print(\"Field width=10, precision=5, the more compact of e or f format: {0:.5g} ± {1:.5g}\".format(nOpt, nAlpha))\n",
446-
"print(\"Field width=10, signed integer format: {0:d} ± {1:d}\".format(nOpt.astype(int), nAlpha.astype(int)))"
443+
"print(\"Field width=10, precision=5, floating point format: \"\n",
444+
" f\"{nOpt:.5f} ± {nAlpha:.5f}\")\n",
445+
"print(\"Field width=10, precision=5, scientific notation format: \"\n",
446+
" f\"{nOpt:.5e} ± {nAlpha:.5e}\")\n",
447+
"print(\"Field width=10, precision=5, the more compact of e or f format: \"\n",
448+
" f\"{nOpt:.5g} ± {nAlpha:.5g}\")\n",
449+
"print(\"Field width=10, signed integer format: \"\n",
450+
" f\"{nOpt.astype(int):d} ± {nAlpha.astype(int):d}\")"
447451
]
448452
},
449453
{
@@ -505,7 +509,7 @@
505509
"res = delta_deg - model(theta_deg,nOpt,alpha_deg_const)\n",
506510
"normres = res/err_deg\n",
507511
"chisq = np.sum(normres**2)\n",
508-
"print(\"chi-squared = {0:.1f}\".format(chisq))"
512+
"print(f\"chi-squared = {chisq:.1f}\")"
509513
]
510514
},
511515
{
@@ -533,7 +537,7 @@
533537
"Ndata = np.size(delta_deg)\n",
534538
"Npar = np.size(nOpt)\n",
535539
"dof = Ndata - Npar\n",
536-
"print(\"dof = {0:d}\".format(dof))"
540+
"print(f\"dof = {dof:d}\")"
537541
]
538542
},
539543
{
@@ -558,8 +562,8 @@
558562
"outputs": [],
559563
"source": [
560564
"cdf = chi2.cdf(chisq,dof)\n",
561-
"print(\"Cumulative probability = {0:.3f}\".format(cdf))\n",
562-
"print(\"Significance: {0:.3f}\".format(1 - cdf))"
565+
"print(f\"Cumulative probability = {cdf:.3f}\")\n",
566+
"print(f\"Significance: {1 - cdf:.3f}\")"
563567
]
564568
},
565569
{
@@ -690,7 +694,7 @@
690694
}
691695
},
692696
"source": [
693-
"You can customize the layout with [`GridSpec`](https://matplotlib.org/users/gridspec.html) by using the the `subplot2grid` helper function."
697+
"You can customize the layout with [`GridSpec`](https://matplotlib.org/users/gridspec.html) by using the `subplot2grid` helper function."
694698
]
695699
},
696700
{

0 commit comments

Comments
 (0)