Python Programming Course

Jun 7, 2024

Lecture Notes: Python Programming Course

Introduction to Python

  • Python's popularity and job market presence
  • Versatility: Automation, scripting, web development
  • Beginner-friendly: Minimal syntax, interpreted language
  • Course goals: Core concepts, practical examples

Setting Up Python Environment

  • Install Python
    • Website: \www.python.org/downloads\
    • Python 3.x vs Python 2.x
    • Choose Python 3.x for future-proof development
    • Installation steps for Windows and Mac
  • Install Text Editor (IDE)
    • Recommendation: PyCharm
    • Website: \jetbrains.com/pycharm\
    • Free Community version
    • Basic IDE configuration and theme settings

First Python Program

  • Writing and running a "Hello World" script
    • Use the print() function
    • Example: print('Hello, World!')
  • Introduction to basic syntax and concepts

Basic Python Concepts

  • Variables and Data Types
    • Strings, Numbers (integers and floats), Booleans
    • Example: ```python character_name = "John" character_age = 35 is_male = True
  • String Manipulation and Functions
    • Concatenation, length, indexing, and string methods (e.g., .lower(), .upper(), .replace())
    • Example: phrase = "Draft Academy"
  • Numbers and Mathematical Operations
    • Basic operators: +, -, *, /, %
    • Functions: abs(), pow(), max(), min(), round(), math.floor(), math.ceil(), math.sqrt()

Control Flow

  • Conditional Statements
    • if, elif, else
    • Comparison operators: ==, !=, >, <, >=, <=
    • Nested conditions and more complex logic
  • While Loops
    • Basic structure: ```python i = 1 while i <= 10: print(i) i += 1
    • Loop control and breaking conditions
  • For Loops
    • Looping over sequences like lists and strings
    • Using range() function
  • Break and Continue Statements
    • Altering loop execution flow

Functions

  • Definition and Invocation
    • Example: ```python def say_hello(name): print('Hello ' + name) say_hello('Alice')
  • Return Values
    • Example: ```python def cube(num): return num * num * num
  • Try/Except Blocks
    • Error handling and specific exception types
    • Example: ```python try: number = int(input('Enter a number: ')) except ValueError: print('Invalid input')

Data Structures

  • Lists
    • Definition, indexing, slicing, modifying
    • Example: ```python friends = ['Kevin', 'Karen', 'Jim']
    • List methods: .append(), .insert(), .remove(), .clear(), .pop(), etc.
  • 2D Lists and Nested Loops
    • Accessing elements in 2D arrays
    • Nested loops to iterate through 2D lists
  • Tuples
    • Immutable sequences
    • Example: ```python coordinates = (4, 5)
    • Tuple vs. List

Working with Files

  • Reading Files
    • Opening, reading, and closing files
    • Example: ```python employee_file = open('employees.txt', 'r') for employee in employee_file.readlines(): print(employee) employee_file.close()
  • Writing/Appending to Files
    • Modes: read (r), write (w), append (a)
    • Example: ```python employee_file = open('employees.txt', 'a') employee_file.write('\nToby - Human Resources') employee_file.close()

Introducing Objects and Classes

  • Class Definition and Objects
    • Example: ```python class Student: def init(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation student1 = Student('Oscar', 'Accounting', 3.1, False)
  • Class Functions
    • Methods within a class
    • Example: ```python class Student: def on_honor_roll(self): return self.gpa >= 3.5
  • Inheritance
    • New classes inheriting attributes and methods
    • Example: ```python class ChineseChef(Chef): def make_special_dish(self): print('The chef makes orange chicken')
  • Multiple Choice Quiz
    • Example application using classes
    • Constructing a class for questions, creating an array of question objects, and evaluating answers

Modules and External Libraries

  • Using Modules
    • Importing and using custom and built-in modules
    • Example: ```python import useful_tools print(useful_tools.roll_dice(10))
  • PIP Package Manager
    • Installing and managing external libraries
    • Example: ```sh pip install Python-Docx

Final Thoughts

  • Python Interpreter
    • Using Python shell for quick tests
    • Basic usage: Run commands directly in the interpreter
  • Continued Learning
    • Practice coding regularly
    • Explore advanced topics such as web development, data science, and automation in Python