🐍

Python Programming Core Features

Jul 14, 2024

Lecture Notes: Python Programming Core Features

Introduction to Python

  • Python is a versatile language used for data structures, algorithms, ML, AI, data science, and web development.
  • Easier to learn compared to other languages, good for coding interviews.
  • Interactive environment for learning through exercises and code editor.

Basic Printing

  • Hello, World! is the traditional first program.
  • Python uses the print function for displaying text.
  • Can use double quotes or single quotes for strings.
  • Case-sensitive and punctuation matters.
  • Example: print('Hello, World!')

Python Fundamentals

  • Python is an interpreted, high-level language created before JavaScript.
  • Popular due to its modern syntax and readability.
  • Used extensively in AI and ML.
  • Focuses on core concepts suitable for both beginners and experienced programmers.

Variables and Data Types

  • Variables are containers for data, defined using =.
  • Common Data Types: int, float, bool, string, list.
  • Dynamic typing: Variable types can change in Python.
  • Variable naming rules: alphabetic characters, numbers, underscores; no starting digit or keyword names.

Comments

  • Use # for single-line comments.
  • Multiple line comments can be done using ''' or """.
  • Comments are ignored during execution.

Control Flow

Conditional Statements

  • if, else, elif for decision making.
  • Example: if balance < 0: print('Overdrawn') else: print('In good standing')

Loops

  • While loops: while condition: block of code
  • For loops: Iterate over a sequence or range.
  • Nested loops: Loops inside loops for multi-dimensional iterations.
  • Example: for i in range(5): print(i)

Functions

  • Defined using def keyword.
  • Can accept parameters and return values.
  • Example: def greet(name): return f'Hello, {name}'
  • Type hints for readability, not enforced.
  • Scope of variables: local vs global.

String Manipulation

  • Strings are sequences of characters, immutable.
  • Common operations: concatenation (+), slicing (:), reversing ([::-1]).
  • Use len() for length, in for membership.
  • String methods: split(), join(), replace().

Lists

  • Lists are mutable sequences, defined using [].
  • Can contain mixed data types, though not recommended.
  • Common operations: append(), pop(), slicing, iteration.
  • Use in for membership check.

Tuples

  • Tuples are immutable sequences, defined using ().
  • Useful for fixed collections of items.

Sets

  • Unordered collections of unique elements, defined using {}.
  • Common operations: add(), remove(), membership checks.

Dictionaries

  • Collections of key-value pairs, defined using {key: value}.
  • Keys must be unique, values can be of any type.
  • Common operations: get(), pop(), iteration over keys or values.

Exception Handling

  • Use try, except blocks to handle runtime errors.
  • Specific exceptions can be caught using except ExceptionName:.
  • finally block for cleanup, always runs.
  • Example: try: result = 10/0 except ZeroDivisionError: print('Division by zero!') finally: print('This runs no matter what.')

Input/Output

  • Use input() to read input, print() to display output.
  • Convert input strings to other types using int(), float(), etc.
  • Example: age = int(input('Enter age: ')) print(f'You are {age} years old')