Values and types
Every value is an object with a type
The type decides what a value can do. Text and numbers do not mix.
A Python program moves values around, and every value has a type. A whole number is an int. A number with a fraction is a float. Text in quotes is a string. True and False are booleans. And None is the value that means nothing is here yet. The type matters because it decides what you may do. Text plus a number is an error until you convert one of them.
Operators · Going deeper
/ always gives a float, // rounds down, and ** binds tightest
/ divides exactly, // rounds down, and ** is worked out first, from the right.
Names
A variable is a name tied to an object, not a box
b = a adds a second name to one object. Nothing is copied.
This is the idea that most often surprises people coming to Python. The first line creates a list and ties the name a to it. The second line does not copy the list. It ties a second name, b, to the same object. So when the third line appends through b, there is only one list to change. Print a, and you see four items.
Names · Going deeper
Rebinding a name is not changing an object
y = y + 1 makes a new object. x keeps the old one.
Conditions
The first true condition wins, and indentation is the syntax
Tests run in order. The first True runs its block; the rest are skipped.
A program decides with if. Here the processor load is eighty seven. The first test asks whether it is above ninety. It is not, so Python moves on. The second test asks whether it is above seventy five. It is, so that block runs and the state becomes warning. The else block is the fallback when nothing matched. This time it is never reached.
Loops
A for loop hands you each item in turn
The indented line runs once per item. The next line runs once.
Most scripts do the same thing to many items. On the first pass, the name s is tied to the first server, and the indented line prints it. On the second pass, s moves to the second server. And on the third pass to the last one. When the list runs out, the loop is over, and the line that is not indented runs exactly once.
Loops · Going deeper · 1 of 2
while: repeat until something changes
A while loop needs a condition that will become false.
Loop control · Going deeper · 2 of 2
break leaves, continue skips, and else means there was no break
break leaves the loop, continue skips one pass, and a loop's else runs only if there was no break.
Functions
A function is a named step with its own private names
Arguments go in, return sends one value back.
When a piece of logic is needed more than once, give it a name. This line calls the function state with the value eighty seven. Inside the call, the name cpu is tied to that argument. Limit was left out, so it takes its default of seventy five. These names exist only while the call runs. The function returns the string warning, and the caller ties the name result to it.
Arguments · Going deeper
A default value is created once, when def runs, not on each call
Never use a list or dict as a default: use None and create it inside.
Lesson complete
You finished “Programming basics with Python”
Nicely done.