Fundamentals of Python Programming

Sep 14, 2024

Python Programming Lecture Notes

Introduction to Python

  • Overview of Python as a programming language
  • Importance of hands-on projects to learn programming

Getting Started with Python

  • Install Python interpreter from python.org
  • Download IDEs: PyCharm or VS Code
    • For PyCharm: Community Edition is free.
    • For VS Code: Install Python extension.

Writing Your First Python Program

  • Create a new project in IDE.
  • Create a new Python file and write a print statement.
  • First program example: print("I like pizza").

Comments in Python

  • Use # to write comments in your code.

Variables and Data Types

  • Variables defined as containers for values.
  • Common data types: Strings, Integers, Floats, and Booleans.
  • Creating and using variables examples:
    first_name = "John"
    age = 25
    height = 5.9
    is_student = True
    

Type Casting

  • Converting between data types using functions.
  • Example: int(), float(), str(), bool().

User Input

  • Using input() function to get user input.
  • Need to type cast user input for mathematical operations.

Conditional Statements

  • Use if, elif, and else to control program flow.

Functions

  • Functions are blocks of reusable code.
  • Define a function using def keyword.
  • Example:
    def greet(name):
        print(f"Hello, {name}")
    

Exception Handling

  • Use try and except to handle exceptions.
  • Example of handling division by zero and value errors.

Object-Oriented Programming (OOP)

  • Basic concepts of OOP: Classes, Objects, Inheritance.
  • Creating a class using class keyword.
  • Example:
    class Dog:
        def bark(self):
            print("Woof!")
    

Decorators

  • Functions that extend the functionality of other functions.
  • Example: Adding sprinkles to an ice cream order.

File Handling

  • Reading and writing files using Python.
  • Use open() function to open files, with modes ('r', 'w', 'a').

Conclusion

  • Summary of key points in Python programming.
  • Encouragement to practice by building projects.