PyQuick Basic Python Class: Day 1

Jul 17, 2024

PyQuick Basic Python Class: Day 1

Introduction

  • Instructor: Nick Parlante
  • Affiliations: Google engEDU group (technical training), Stanford lecturer
  • Class Duration: 2 days, covering basic, useful Python
  • Prerequisites:
    • Some experience with any programming language
    • Basic understanding of programming concepts like variables

Class Format

  • Structure: Mix of lectures and coding sections
  • Materials: Written materials available on PyQuick page [link provided in the class]
  • Focus: Introduction to strings, lists, modules, etc.
  • Approach:
    • Lectures to cover basic ideas
    • Coding sections to practice hands-on learning

Overview of Python

  • History: Created by Guido van Rossum in 1990
  • Popularity: Recently gained momentum due to its easy and intuitive design
  • Category: Scripting Language
    • Similar to Perl, Bash, Ruby, JavaScript
    • No heavy type system
  • Interactive Nature: Python supports quick experimentation using interactive mode
  • Usages
    • Suitable for small tasks and scripts
    • Debate on its efficacy in large-scale projects

The Python Interpreter

  • Interactive Mode: Type python to start, can execute single lines of code
  • Quick turnaround: Ideal for testing small snippets
  • Commands Example:
    • Variable assignment: a = 6
    • Print value: print(a)
    • Dynamic typing: a = 'hello', print(a)

Basic Syntax

  • Functions: Defined using def keyword
    • Example: def main(): print('Hello')
  • Imports: Bringing in external modules using import keyword
    • Example: import sys, print(sys.argv)
  • Command-line arguments: Accessed via sys.argv
    • First element is the script name
  • Running Scripts:
    • python hello.py
    • ./hello.py (with execute permissions)

Python's Design Philosophy

  • Dynamic Typing: No need to declare type of variables
  • Indentation: Used for block delimitation instead of { }
    • Default: 2 spaces (Google standard)
    • Example function:
      def hello(name):
          print('Hello', name)
      
  • Error Handling:
    • Immediate feedback on errors
    • Encourages good testing practices
  • Comparisons: Deep, intuitive comparisons using ==
    • Example: if name == 'Alice': print('Hi Alice')
    • Logical operators: and, or, not

Strings

  • Declaration: Single or double quotes
    • Example: 'hello', "world"
  • Immutability: Strings cannot be changed once created
  • Common Methods:
    • Length: len(string)
    • Lowercase: string.lower()
    • Find: string.find('sub')
  • String Formatting:
    • Concatenation: a + b
    • Format string: "%s %d" % ('hello', 42)
  • Unicode: Handles Unicode, minor differences in creation
  • Indexing & Slicing:
    • Indexing: string[index]
    • Slicing: string[start:end]
      • Example: a[1:4] gets substring from index 1 to 3
      • Omitting start or end: a[1:], a[:3]
      • Negative indexing: From the end of the string a[-3:]

Modules & Documentation

  • Common Modules: sys, os, etc.
    • Importing: import module_name
    • Using Functions: module.function()
  • Documentation:
    • Use dir() and help() functions
    • Google search for quick answers

Exercises

  • Location: PyQuick page, day1/ directory
  • Exercise File: string1.py (basic string operations)
    • Write code to complete tasks, test with provided test cases
    • Optional additional exercises in string2.py
  • Time Allocation: Work on exercises for ~30 minutes, then break for lunch

Tips

  • Indentation: Use spaces, not tabs (use -tt flag to avoid mixing)
  • Exploration: Utilize interpreter for quick experiments

Conclusion

  • End of Lecture: Proceed to exercises and practical coding
  • Next Session: Continue after lunch and move to more exercises and advanced topics