Why Python for AI
- Python is the most predominant language for data science, machine learning and AI
- Rich ecosystem with robust libraries and modules
- Simple syntax compared to Java, C++, or other languages
- Emphasizes code readability and reduced development costs
- Cross-platform compatibility
- Strong community support and open source ecosystem
- Companies using Python: Netflix, Spotify, Google, and many tech startups
- Python integrates easily with other technologies
How AI is Changing Code
AI is reshaping the coding landscape through four key areas:
- Code generation - Give natural language instructions to AI to generate code
- Demonstrated with Gemini analyzing California housing dataset
- AI generated complete data analysis pipeline from simple prompt
- Bug detection and debugging - AI can identify and fix code errors
- Low-code and no-code platforms - Drag and drop interfaces without writing code
- Predictive coding and smart suggestions - AI completes code as you type
- Similar to autocomplete but for entire code blocks
- AI models are probabilistic, not deterministic
- Can make mistakes and hallucinate (create false information)
- Solutions include prompt engineering, fine-tuning, and governance strategies
Python IDE
Three main IDEs covered for this class:
- Visual Studio Code - Local installation, lightweight
- Jupyter Notebook - Through Anaconda, heavy installation
- Google Colab - Browser-based, cloud hosted, recommended
Install and setup VSCode
Installation process:
- Download from official website based on operating system
- Accept agreement and follow installation prompts
- Optional: Check box to create desktop icon
- Sign in with GitHub or Google for AI features (Copilot)
- Can skip registration initially
- Extensions available for enhanced functionality
Installing Jupyter
- Requires Anaconda installation (heavy, requires significant disk space)
- Alternative: Use web version or stick with VSCode/Colab
- Not mandatory if comfortable with other IDEs
- Provides notebook experience similar to Colab but locally hosted
Collab by Google
- Browser-based, cloud environment
- No local installation required
- Free tier includes:
- 12.67 GB RAM
- 107 GB disk space
- Built-in Gemini AI assistant
- Access through Gmail account → Google Drive
- Recommended for class due to:
- No package conflicts
- No local storage requirements
- Persistent access across devices
Python: Identifier
Naming rules for variables, functions, classes, and modules:
Valid characteristics:
- Combination of letters (upper/lowercase), digits (0-9), underscores
- Any length but cannot start with digits
- Case sensitive (lowercase ‘a’ ≠ uppercase ‘A’)
Invalid characteristics:
- Special symbols (@, #, $, %) cannot be used
- Keywords (global, class) cannot be used as identifiers
Examples:
- Valid: myClass, var_1, count, first_name
- Invalid: 1variable, class@new, global
Collab using Gemini w errors
- Gemini integrated directly into Google Colab
- Can explain error messages and suggest fixes
- Demonstrated indentation error example:
- Python requires proper indentation after if statements
- IDE automatically handles indentation when typing colons
Comments
Two types of comments for human-readable code documentation:
- Single line comments - Use # (hash/pound sign)
- Explain what the following line of code does
- Example: # Import modules and packages
- Multiple line comments - Use triple quotes (“”" or ‘’')
- For longer explanations spanning multiple lines
- Comments are ignored by program execution, meant for human readers
Input and Output
Python provides two primary functions:
- input() - Takes user input
- Waits for user to enter data
- Example: name = input("Enter your name: ")
- print() - Displays output
- Shows results of program processing
- Example: print("Hello", name)
- Demonstrated interactive program taking first name, last name, and displaying greeting
Python variables
Variables store data values with different types:
Assignment examples:
- x = 10 (integer)
- name = "Alice" (string)
- price = 99.99 (float)
- is_active = True (boolean)
Multiple variable assignment:
- Single line: a, b, c = 1, "hello", 3.14
- Separate with commas for efficiency
Data Types
Scalar data types:
- Integer: 123, 10, 23
- Float: 1.2, 99.99 (decimal numbers)
- Boolean: True, False
- Complex: 2+6j
Aggregate data types:
- String: “ABC”, “Alice”
- Set: {2, 3, 4, ‘abc’}
- List: [12, ‘a’, 9.9]
- Tuple: (3.3, ‘a’, 45)
- Dictionary: {‘a’: 1, ‘b’: 3}
- Data type of any object can be checked using built-in function type()
No comments:
Post a Comment