>> Module 01 / Day 02 / Lecture

Python Essentials I

Syntax & Functions
The language of data, AI, and — today — every modern blockchain analytics stack.
Duration
1 hr lecture  +  1 hr lab
Format
6 functions in one .py file
Maps to
CO2 · CO3
DAY 02 / 40
>> Where we are

Day 2 of a 40-day journey

▼ YOU ARE HERE · DAY 02
M1Python & Data
M2Cryptography
M3Blockchain
M4Polkadot
M5Polkadot · Py
Yesterday · Day 01
Course Kickoff & Python Setup

Installed Python 3.11+, VS Code, Git. Built a virtual env. Pushed a “hello blockchain” script to GitHub.

Today · Day 02
Python Essentials I

Variables, strings, control flow, functions. By the end of today: 6 working Python functions on your machine.

Tomorrow · Day 03
Python Essentials II — Collections

Lists, tuples, dicts, sets, comprehensions. We start handling sample transaction data.

>> Today's roadmap

Four building blocks, then a lab

01
Variables & Types

int, float, str, bool. Python's dynamic typing — what it lets you do, and what it hides.

02
Strings & f-strings

String methods you'll use daily. f-strings: the cleanest way to format text in modern Python.

03
Control Flow

if / elif / else. for and while loops. Indentation is syntax — not decoration.

04
Functions

Parameters, return values, default arguments. The unit of reuse in every Python codebase.

PRACTICAL LAB · 1 HR Six warm-up problems → six functions in one .py file.
>> 01 / 04 · Variables & Types

Python is dynamically typed

You don't declare types — Python infers them at runtime. Powerful, but you must still know what type a value is.

The four you'll use daily
int42 block numbers, nonces, counts
float3.14 prices, percentages, ratios
str"0xabc…" addresses, hashes, names
boolTrue flags: is_signed, has_funds
>> 02 / 04 · Strings & f-strings

f-strings: the only way you should format strings

THE OLD WAY (%)
Python 2 style. Avoid.
THE OK WAY (.format)
Works, but verbose.
THE PYTHONIC WAY (f'…')
Use this. Always.
METHODS YOU'LL USE .lower()  .upper()  .strip()  .split()  .replace()  .startswith()  .endswith()  len(s)
>> 03 / 04 · Control Flow · Conditionals

Decisions in code: if / elif / else

Indentation is the syntax. No braces. Four spaces per level — Python is strict about it.

Operators you'll chain
==equal
!=not equal
<  >less / greater
<= >=or equal
andboth true
oreither true
notflip the bool
inmembership test
>> 03 / 04 · Control Flow · Loops

for vs while — same idea, different shapes

for loop
known iterations
while loop
condition-driven
RULE OF THUMB · In Python you'll write 9 for-loops for every 1 while-loop.
>> 04 / 04 · Functions

Functions — the unit of reuse

If you write the same logic twice, it should be a function. Period.

def

Keyword that defines a function.

( )

Parameters — the values it accepts. rate=0.01 is a default argument.

docstring

The """…""" line — explains what the function does. Tools & IDEs read it.

return

Sends a value back to the caller. No return → returns None.

>> Watch out for

Five mistakes you'll make today

Knowing them in advance saves 30 minutes of stuck-on-this-error.

if x = 5:
if x == 5:
= assigns. == compares. Don't mix them.
if amount > "100":
if amount > 100:
Comparing a number to a string crashes — quotes matter.
for i in range(10): print(i)
for i in range(10):
    print(i)
Indentation is required. 4 spaces — be consistent.
print(n + " has " + age)
print(f"{n} has {age}")
Concatenating str + int throws TypeError.
def fee(amt):
  amt * 0.01
def fee(amt):
  return amt * 0.01
Forgot return? Function silently returns None.
>> Practical Lab · 1 hr

Six warm-ups, one .py file

One function per problem. Plain Python — no libraries. Push the file to your GitHub when done.

01Reverse a string
reverse_string(s) → str reverse_string("blockchain")
→ "niahckcolb"
02Find max in a list
find_max(nums) → int find_max([3, 7, 2, 9, 4])
→ 9
03Check even or odd
is_even(n) → bool is_even(7)   → False
is_even(10)  → True
04FizzBuzz
fizzbuzz(n) → list fizzbuzz(5)
→ [1,2,"Fizz",4,"Buzz"]
05Count vowels
count_vowels(s) → int count_vowels("polkadot")
→ 3
06Sum of digits
sum_digits(n) → int sum_digits(12345)
→ 15
DELIVERABLE · warmups.py with 6 functions · one print() demo per function · push to GitHub
>> Walkthrough · Problem 04

FizzBuzz — the everyone-has-to-know problem

Asked in 1 out of every 3 programming interviews. We solve it once, properly.

The spec

Given a number n, return a list from 1 to n where:

multiples of 3 → "Fizz"
multiples of 5 → "Buzz"
multiples of 15 → "FizzBuzz"
everything else → the number itself
Check the largest case first. Otherwise 15 hits “Fizz” before you ever reach “FizzBuzz”.
>> Recap

What you can now do

Declare and use the four primitive types int · float · str · bool — and inspect any value with type().
Format strings the modern way f-strings with embedded expressions and number formatting.
Control program flow Branching with if/elif/else. Looping with for and while.
Write reusable functions def, parameters, defaults, return — the unit of reuse anywhere.
Up next · Day 03
Python Essentials II
Collections
  • lists, tuples, dicts, sets
  • list & dict comprehensions
  • reading & writing files
  • first taste of transaction data
BEFORE TOMORROW · finish warmups.py · push to GitHub · read about Python lists on python.org

>>> questions?

Lab starts in 5 minutes. Open VS Code, create warmups.py, and let's solve six problems.

Python 3.11+ working?
VS Code with Python extension installed?
Virtual environment activated?
GitHub repo from Day 1 cloned locally?
FROM BITS TO BLOCKCHAIN · DAY 02 / 40
01 / 13
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF