Python · Module 1
Getting Started
Take each lesson, answer its quiz, then do its practice. After the last lesson come the module quiz, the project and the interview questions.
Lessons
- 1.1Why Python?Say what Python is and why it is used for AI and machine learning, and know where Python code is written and run.
- 1.2Run Your First Python CodeRun Python in a notebook and know what happens when a cell executes.
- 1.3Variables, Keywords and IdentifiersBind names to objects, and name variables by Python's rules.
- 1.4Data TypesKnow the basic types and how they convert.
- 1.5Input, Output and FormattingRead input and print results readably.
- 1.6OperatorsCompute and compare with Python's operators, and know their precedence.
Module material
Take these after the last lesson, in this order. Next on each page leads through them.
- Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
- Project: Bill SplitterStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
- NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
- Interview questions25 questions interviewers ask on getting started, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.
Glossary
Every term the module introduces, with the lesson that introduces it.
- Colab
- Jupyter notebooks in the browser, run on Google's computers. Notebooks are saved to Google Drive, and nothing is installed locally. 1.1
- general purpose
- Used for many different kinds of programs: websites, automation, data analysis and machine learning. 1.1
- high level
- Written in words and symbols such as
print,ifand+, while Python handles the details of the computer's memory and hardware. 1.1 - interpreted
- Executed one statement at a time by a program called the interpreter, so each line's result can be seen straight away. 1.1
- library
- Published code your program can import and use: NumPy, pandas, Matplotlib, scikit-learn, TensorFlow, PyTorch. 1.1
- notebook
- A document made of cells. A code cell holds Python that can run; a text cell holds notes. Jupyter notebook files end in
.ipynb. 1.1 - program
- A set of instructions for a computer, written as text. 1.1
- programming language
- Precise rules for writing instructions a computer can carry out. 1.1
- comment
- Text after
#, skipped when the code runs. A comment over several lines starts each line with its own#. 1.2 - execution count
- The order in which cells ran: the number in square brackets beside a cell, such as
[3]. 1.2 - function
- A named piece of code that does one job.
print's job is to display a value. 1.2 - kernel
- The program that runs a notebook's cells and keeps the names they create. Restarting it deletes every name. 1.2
- statement
- One instruction. Python executes a program's statements one at a time, from top to bottom. 1.2
- string
- Text, written in quotes:
"Hello, world!". Its type isstr. 1.2 - traceback
- Python's report of a failed statement. Read it from the bottom: the last line names the error, and the arrow points at the line that failed. 1.2
- assignment
- The instruction to give a value a name:
total = 24.75. The=does not mean "equal"; it tells Python to evaluate the right hand side and bind the name on the left to it. 1.3 - bind
- To add a name to the namespace, with a reference to an object. 1.3
- identifier
- The name of a variable, function or class. It can use letters, digits and underscores, can not start with a digit, and can not be a keyword. Identifiers are case sensitive. 1.3
- keyword
- A word Python reserves, such as
if,for,class,TrueandNone. It can not be used as a name. Python 3.12 has 35. 1.3 - namespace
- The table of names that exist, each with a reference to an object. 1.3
- object
- A value in memory, with its type. Every object has an identity number, shown by
id(). 1.3 - rebinding
- Binding a name to a different object. The old object is unchanged, and any other name still refers to it. 1.3
- variable
- A name given to a value. In Python, a name that refers to an object. 1.3
- bool
- True or False. The type of
TrueandFalse, both keywords, written with a capital letter. 1.4 - complex
- A number with real and imaginary parts, such as
1 + 2j. Thejmarks the imaginary part;type(1 + 2j)is<class 'complex'>. 1.4 - dynamic typing
- A name can refer to an object of any type, and can be rebound to an object of a different type:
items = 3, thenitems = 3.5. 1.4 - float
- A number with a decimal point:
24.75,2.0. 1.4 - int
- A whole number:
3,-7,1000. 1.4 - None
- No value. The only value of the type
NoneType:rating = Nonefor an order not rated yet, which is different from a rating of0. 1.4 - NoneType
- The type whose only value is
None.type(None)gives<class 'NoneType'>. 1.4 - str
- Text, a sequence of characters, written in single, double or triple quotes. 1.4
- type
- Determines what values an object can hold, and what you can do with it.
type(x)shows it. 1.4 - type annotation
- The intended type, written after a colon:
items: int = 3. Python does not check it; editors and type checkers do. 1.4 - argument
- A value passed to a function. In
print("Total:", 20.75), the two arguments are"Total:"and20.75. 1.5 - "f" string
- A string with
fbefore the quote; each{expression}inside it is replaced by its value:f"The {item} costs {price}". 1.5 - format specifier
- After the colon in an "f" string's braces, how a value is shown:
{price:.2f},{52000:,},{name:<10}. 1.5 - input
input()shows a prompt and returns what the user types, always as astr. 1.5- comparison operator
- An operator that gives True or False:
==,!=,<,>,<=,>=. 1.6 - expression
- Any piece of code that Python evaluates to a value:
10 + 3,price * 2,age >= 18. 1.6 - floor division
//: divides, then rounds down toward negative infinity.10 // 3is3, and-7 // 2is-4. 1.6- modulo
%: the remainder after floor division.10 % 3is1, and-7 % 2is1. 1.6- operand
- A value an operator works on: in
10 + 3, the operands are10and3. 1.6 - operator
- A symbol or word that performs an operation:
+,//,==,and,in. 1.6 - precedence
- The order operators run in:
**, then unary-, then* / // %, then+ -, then comparisons, thennot,and,or. Round brackets override it. 1.6 - short circuit evaluation
andandorstop as soon as they know the answer: inFalse and xandTrue or x, thexis never evaluated. 1.6