Beginner·18 min·0/47 done

Python Variables

Create variables, check types, and use operators. These are the building blocks of every script.

Why it matters: Learning rates, batch sizes, and scores are ordinary Python values.

Basics

Creating Variables

A variable is a name bound to a value.

Common types:

  • int - whole numbers like 32
  • float - decimals like 0.001
  • bool - True or False
  • None - no value

Use type(x) to check the type. Common mistake: treating None like the string "None".

Tiny example

A name holds a value.

score = 0.91
print(score)
print(type(score))

Basics

Operators

Arithmetic: + - * / // % **

Comparisons return bools: == != < > <= >=

Logic: and, or, not

/ always returns a float. Use // for a whole number. Common mistake: using / when you need an int index.

Tiny example

Compare a score to a threshold.

score = 0.82
threshold = 0.7
print(score >= threshold)

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: inspect types

type() shows what Python thinks a value is.

In AI / ML: Hyperparameters mix ints, floats, bools, and None.

example.py

Python

epochs = 10
learning_rate = 3e-5
enabled = True
print(type(epochs))
print(type(learning_rate))
print(type(enabled))

AI / ML example 2

AI / ML: threshold check

A comparison turns a score into keep or drop.

In AI / ML: Classifiers often keep predictions only above a threshold.

example.py

Python

score = 0.82
threshold = 0.7
print(score >= threshold)

Takeaways

  • 1Values have types. Names point at values.
  • 2Use / for float divide and // for whole numbers.
  • 3True, False, and None are capitalized.
Certificate