Summary of "Google Python Class Day 1 Part 1"
Summary of "Google Python Class Day 1 Part 1"
Main Ideas and Concepts
-
Introduction to the Class and Python
- Instructor: Nick Parlante from Google’s engEDU group and Stanford lecturer.
- The course is a 2-day introduction to basic, useful Python.
- No advanced programming background required; just familiarity with basic programming concepts like variables.
- The class alternates between lecture and coding exercises, emphasizing hands-on practice.
- Python is a friendly, well-designed, interpreted scripting language, ideal for quick tasks and small projects.
- Python’s popularity has grown due to its simplicity and quick turnaround for coding experiments.
- Differences between Python versions (2.4, 2.5, 2.6, and 3.x) are minor; the course focuses on Python 2.4 but notes Python 3 is emerging.
-
Python Interpreter and Interactive Development
- Python is an interpreted language with a Read-Eval-Print Loop (REPL).
- Variables do not require explicit declaration or type definition; types are dynamic and checked at runtime.
- Example: variable
acan be assigned an integer, then a string later. - Python is case-sensitive; undefined variables cause immediate errors.
- Encouragement to experiment with the interpreter to explore Python features and test code snippets.
- Use of
str()function to convert data types explicitly (e.g., string + integer concatenation requires conversion).
-
Running Python Programs
- Python scripts can be run via the interpreter command line (
Python hello.py) or directly if executable permission is set. - Explanation of the typical Python file structure:
- Shebang line (
#!/usr/bin/Python) to specify interpreter. - Function definitions using
def. - The conventional
main()function as the program entry point. - The
if __name__ == "__main__":guard to allow a file to be run as a script or imported as a module without executing the main code.
- Shebang line (
- Introduction to importing modules with
import(e.g.,import sys). - Using
sys.argvto access command-line arguments as a list, wheresys.argv[0]is the script name and subsequent elements are the arguments.
- Python scripts can be run via the interpreter command line (
-
Exploring Modules and Documentation
- Two methods to explore Python modules and functions:
- Using the interpreter’s built-in functions:
dir(module)to list symbols in a module.help(module_or_function)to get documentation.
- Using Google search to find official documentation on Python.org.
- Using the interpreter’s built-in functions:
- Emphasis on using documentation and exploration to learn and find functionality.
- Two methods to explore Python modules and functions:
-
Common Python Errors
- Example of forgetting to import a module (
NameError: global name 'sys' is not defined). - Importance of importing modules before usage.
- Python errors are clear and immediate, helping catch bugs early.
- Example of forgetting to import a module (
-
Defining Functions and Syntax
- Function definition syntax:
def function_name(parameters): - Python uses indentation (usually 2 spaces at Google) instead of braces to define blocks of code.
- Indentation is significant and controls code structure.
- Explanation of the rationale behind Python’s indentation style.
- Example of a simple function
hello(name)that prints a greeting. - Use of commas in
printto separate items with spaces. - String concatenation with
+operator to avoid added spaces.
- Function definition syntax:
-
Conditional Statements
- Python
ifstatements useif condition:followed by an indented block. else:clause for alternative execution.- Equality test uses
==. - Logical operators spelled out:
and,or,not. - Parentheses around conditions are optional and typically omitted in Python style.
- Truthiness in Python:
- Zero, empty strings, and
Noneare considered false. - Non-zero numbers and non-empty strings are true.
- Zero, empty strings, and
- Python
-
Python’s Runtime Behavior
- Python executes code line-by-line and only checks for errors when a line is run.
- Example: calling an undefined function inside an
elseblock that never executes does not cause an error. - This late binding means less compile-time checking but faster iteration.
- Emphasis on the importance of unit testing in Python due to this behavior.
-
Strings in Python
- Strings can be enclosed in single
'...'or double quotes"...". - Quotes inside strings can be handled by using the opposite quote or escaping with backslash
\. - Strings are immutable (cannot be changed after creation).
- String methods (e.g.,
.lower(),.find()) return new strings or results without modifying the original. - String concatenation with
+. - Printing multiple items separated by commas inserts spaces automatically.
- String formatting
- Strings can be enclosed in single
Category
Educational