Fundamentals of Python Programming

Sep 15, 2024

Lecture Notes

Introduction

  • Discussion on various programming concepts and operations.

Data Types

  • Integer: Basic data type for whole numbers.
  • String: Text data type represented in double quotations.

Operations on Data Types

  • Operations: Addition, Subtraction, Multiplication, Division.
  • Example: a = 10, b = 20.

Getting User Input

  • Use input() function to get name and age.
    • Example: name = input('Enter your name: ')
  • Printing formatted strings using print().

Mathematical Operations

  • Get input for three integer variables a, b, c and perform operations: multiply and add.
    • Example: result = a * b * c

Conditional Statements

  • If-Else Statements: Used for decision-making.
    • Example: if score > 70: print('Good Student')

Boolean Values

  • True/False values used in conditions.
  • Example: if condition: checks for a true condition.

Loops

  • For Loops: Iterating over a range or a list.
    • Example: for i in range(1, 11): print(i) prints numbers 1 to 10.
  • While Loops: Execute as long as a condition is true.
    • Example: while i < 5: print(i).

Functions

  • Define using def keyword. Can take parameters and return values.
    • Example: def add(a, b): return a + b
  • Call functions with arguments: add(10, 20).

Classes and Objects

  • Class: Blueprint for creating objects.
    • Example: class Student: defines a student class with attributes.
  • Objects: Instances of classes.
    • Example: student1 = Student().

Inheritance

  • Mechanism to create new classes based on existing ones.
    • Example: class Teacher(Student): inherits from Student.

Error Handling

  • Exceptions: Handle errors that may occur during execution.
    • Example: try: ... except ValueError: to catch specific errors.

File Operations

  • Read and write operations with files.
    • Example: with open('file.txt', 'r') as f:.

Summary

  • Covered the fundamentals of programming with Python including variables, operations, control structures, functions, and object-oriented programming concepts.
  • Understanding these concepts is crucial for effective programming.