Code Block

// code block cheat sheet

Quick reference for adding syntax-highlighted code to your Blogger posts. Always use HTML view when inserting code.

Basic Template

Copy this structure and replace language-xxx with your language and add your code inside:

<pre class="line-numbers"><code class="language-python"> # your code here print("hello world") </code></pre>
line-numbers on the <pre> tag = show line numbers. Remove it if you don't want them.

Language Classes

Replace language-python with the right one for your post:

language-pythonPython — for algorithms, ML, data science
language-javascriptJavaScript — web, Node.js
language-typescriptTypeScript
language-cssCSS
language-htmlHTML / markup
language-javaJava
language-cC
language-cppC++
language-csharpC#
language-rR — statistics, data analysis
language-sqlSQL — database queries
language-bashBash / terminal / shell commands
language-jsonJSON — data structures, APIs
language-yamlYAML — config files
language-markdownMarkdown
language-latexLaTeX source
language-pseudocodePseudocode (plain text fallback)

Ready-to-Copy Examples

Python — function

<pre class="line-numbers"><code class="language-python"> def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(5)) # 120 </code></pre>

Python — ML / NumPy style

<pre class="line-numbers"><code class="language-python"> import numpy as np X = np.array([[1, 2], [3, 4], [5, 6]]) mean = np.mean(X, axis=0) print(mean) # [3. 4.] </code></pre>

JavaScript

<pre class="line-numbers"><code class="language-javascript"> const fibonacci = (n) => { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); }; console.log(fibonacci(10)); // 55 </code></pre>

CSS

<pre class="line-numbers"><code class="language-css"> .container { display: flex; align-items: center; gap: 1rem; background-color: #0a192f; } </code></pre>

SQL

<pre class="line-numbers"><code class="language-sql"> SELECT name, COUNT(*) AS total FROM students WHERE grade >= 85 GROUP BY name ORDER BY total DESC; </code></pre>

Bash / Terminal

<pre class="line-numbers"><code class="language-bash"> # Install dependencies pip install numpy pandas matplotlib scikit-learn # Run script python train_model.py --epochs 50 --lr 0.001 </code></pre>

R — Statistics

<pre class="line-numbers"><code class="language-r"> data <- c(12, 15, 18, 22, 25, 30) mean(data) # 20.33 sd(data) # 6.28 summary(data) </code></pre>

Inline Code (inside a sentence)

For short code references inside a paragraph — variable names, function names, commands:

<code>variable_name</code>

Example: "The <code>len()</code> function returns the number of items in a list."

⚠ Characters to Escape in HTML View

These characters break HTML if typed directly inside a code block. Use the escaped version:

&lt;< less than sign — very common in Python, JS comparisons
&gt;> greater than sign — arrow functions, comparisons
&amp;& ampersand — logical AND in some languages
&quot;" double quote — use inside HTML attributes
Tip: If your code block looks broken after saving, a stray < or > is usually the culprit. Replace them with &lt; and &gt;.

Combining Code + Math in One Post

This is common for CS/AI posts. You can mix both freely — just use the right wrapper for each:

/* Math equation */ $$f(x) = \frac{1}{1 + e^{-x}}$$ /* Then right after, the implementation */ <pre class="line-numbers"><code class="language-python"> import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) </code></pre>

Pseudocode / Algorithm Steps

For writing algorithms before implementing them — use plain text with no language class, or language-text:

<pre class="line-numbers"><code class="language-text"> Algorithm: Binary Search Input: sorted array A, target value t Output: index of t in A, or -1 1. Set low = 0, high = len(A) - 1 2. While low <= high: a. mid = floor((low + high) / 2) b. If A[mid] == t: return mid c. If A[mid] < t: low = mid + 1 d. Else: high = mid - 1 3. Return -1 </code></pre>

0 comments