Python Programming Basics

Aug 26, 2025

Overview

This lecture provides a beginner-friendly introduction to Python programming, covering installation, basic syntax, variables, data types, operators, control flow, loops, data structures (lists and tuples), and essential built-in functions.

Getting Started with Python

  • Python is a multi-purpose language used for web development, data science, machine learning, and automation.
  • Download Python from python.org and install the latest version, ensuring you add Python to your system PATH.
  • Use a code editor like PyCharm Community Edition for writing Python code.

Writing Your First Program

  • Create a new Python project and file, then write print("Hello World") to output a message.
  • Strings are sequences of characters surrounded by single or double quotes.
  • The print function displays output in the terminal.

Variables and Data Types

  • Variables store data temporarily in memory (e.g., age = 20).
  • Variable names should use underscores to separate words for readability.
  • Python supports numbers (integers, floats), strings, and booleans (True/False).
  • Python is case-sensitive (Truetrue).

User Input and Type Conversion

  • Use input() to receive user data as strings.
  • Convert input strings to integers or floats using int(), float(), or to other types as needed.
  • String concatenation combines strings with +.
  • Convert other data types to strings using str().

String Methods

  • Strings are objects with methods such as .upper(), .lower(), .find(), .replace().
  • Strings are immutable; methods return new strings.
  • Use in to check if a substring exists in a string.

Arithmetic and Comparison Operators

  • Use +, -, *, / (float), // (integer), % (modulus), and ** (exponent) for arithmetic.
  • Augmented assignment operators (+=, -=, etc.) simplify variable updates.
  • Operator precedence follows math rules; parentheses alter order.
  • Comparison operators: >, >=, <, <=, == (equality), != (not equal).

Logical Operators and Control Flow

  • Logical operators: and, or, not.
  • if, elif, else statements execute code blocks conditionally.
  • Indentation defines code blocks in Python.

Loops

  • while loops repeat code as long as a condition is true; increment loop variables to avoid infinite loops.
  • for loops iterate over sequences like lists or ranges.

Lists

  • Lists store ordered collections of items, defined with square brackets (e.g., [1, 2, 3]).
  • Access items by index (zero-based), including negative indices for reverse order.
  • Methods: .append(), .insert(), .remove(), .clear(), and the in operator.
  • Use len() to get the number of items.

Tuples

  • Tuples are immutable sequences defined with parentheses (e.g., (1, 2, 3)).
  • Support .count() and .index() methods, but not modification.
  • Use tuples when data should not change.

Key Terms & Definitions

  • Variable — A label for a memory location storing data.
  • String — A sequence of characters (text).
  • Boolean — A data type with two values: True or False.
  • List — An ordered, mutable collection of items.
  • Tuple — An ordered, immutable collection of items.
  • Function — A reusable block of code (e.g., print()).
  • Method — A function associated with an object (e.g., str.upper()).
  • Operator — A symbol performing operations on variables and values.

Action Items / Next Steps

  • Download and install Python from python.org.
  • Install PyCharm Community Edition or another code editor.
  • Complete practice exercises: check-in patient variables, basic calculator, and weight converter program.
  • Explore string, list, and tuple methods in your own code.