🐍

Python Programming Basics

Aug 25, 2025

Overview

This lecture covers the fundamental concepts of Python programming, including variables, data types, operators, collections, functions, object-oriented programming, file handling, loops, conditionals, and importing modules.

Introduction to Programming and Python

  • Programming involves writing code to give instructions to a computer’s processor.
  • Software is a collection of code or programs.
  • Python is a beginner-friendly programming language.

Variables and Data Types

  • A variable is a container to temporarily store data in a program.
  • Data types in Python include:
    • String (text, e.g., "Vamsi")
    • Integer (whole numbers, e.g., 10000)
    • Float (numbers with decimals, e.g., 3.14)
    • Complex (numbers with real and imaginary parts)
    • Boolean (True or False)
    • Sequence (list, tuple, range)
    • Mapping (dictionary)
    • Set (unordered collection of unique items)

Input and Output

  • The input() function takes input from the user.
  • The print() function displays output.
  • type() checks the variable's data type.

Operators in Python

  • Arithmetic operators: +, -, *, /, %, **, //
  • Assignment operators: =, +=, -=, *=, /=, %=
  • Comparison operators: >, <, >=, <=, ==, !=
  • Logical operators: and, or, not
  • Identity operators: is, is not (used for object comparison)
  • Membership operators: in, not in
  • Bitwise operators: &, |, ^, ~, <<, >>

Collections: List, Tuple, Set, Dictionary

  • List: ordered, changeable, allows duplicates, uses [ ].
  • Tuple: ordered, unchangeable, allows duplicates, uses ( ).
  • Set: unordered, changeable, no duplicates, uses { }.
  • Dictionary: ordered, changeable, no duplicates, key-value pairs, uses { }.

Collection Methods

  • List: .append(), .insert(), .extend(), .remove(), .pop(), del
  • Set: .add(), .remove()
  • Dictionary: access with keys, .get(), .keys(), .values()

Conditionals and Loops

  • if, elif, else control flow based on conditions.
  • while loop repeats code while a condition is true.
  • for loop iterates over sequences or with range().

Functions

  • Defined with def keyword; reusable blocks of code.
  • Arguments (parameters) are passed to functions.
  • return statement sends data back to the caller.
  • Lambda functions create anonymous, one-line functions.

Object-Oriented Programming

  • Class: blueprint defining data (attributes) and methods (functions).
  • Object: instance of a class with actual data.
  • __init__ method initializes object attributes.
  • Inheritance allows a class to use attributes and methods from another class.

File Handling

  • Files are opened with open(filename, mode).
  • Modes: 'r' (read), 'w' (write), 'a' (append), 'x' (create).
  • .read(), .readline(), .write() are used for file operations.
  • Files should be closed with .close().

Importing Modules

  • Use import module_name to use external modules (e.g., import math).
  • from module import component imports specific parts.
  • from module import * imports everything from a module.*

Key Terms & Definitions

  • Variable β€” temporary container to store data.
  • Data Type β€” categorizes data (e.g., int, float, str, bool).
  • Operator β€” symbol performing operations on data.
  • List β€” ordered, mutable sequence of items.
  • Tuple β€” ordered, immutable sequence of items.
  • Set β€” unordered, unique collection of items.
  • Dictionary β€” collection of key-value pairs.
  • Function β€” reusable block of code.
  • Class β€” blueprint for objects.
  • Object β€” instance of a class.
  • Inheritance β€” child class inherits from parent class.
  • File Handling β€” reading/writing files in Python.
  • Module β€” external Python file with reusable code.

Action Items / Next Steps

  • Practice writing and running Python code covering each concept.
  • Try creating and using lists, tuples, sets, and dictionaries.
  • Experiment with defining functions, classes, and file operations in Python.
  • Review the use of modules and try importing and using the math module.