🖥️

Programming Basics and Control Structures

Jun 14, 2024

Programming Lecture Notes

Basic Concepts

Data Types and Variables

  • Integer: Whole numbers without a decimal. Example: a = 10, b = 20. Operations: +, -, *, /.
  • String: Text enclosed in double quotes. Example: name = "EMC", age = "Porter".*

Input and Output

  • Get input from users using input(). Example: name = input(), age = input().
  • Print statements to output data. Example: print("My name is", name), print("My age is", age).

Conditional Statements

If-Else Statements

  • Evaluates expressions and executes code blocks based on the truth value.
  • Example: if a > 10: print("Greater than 10") else: print("10 or less")
  • Nested If-Else for complex conditions.

Boolean Values

  • Represented by True and False.
  • Used in conditional expressions.

Logical Operators

  • and, or, not: To combine multiple conditions.
  • Example: if a > 10 and b < 20: print("Condition met")

Loops

For Loop

  • Iterate over a sequence (e.g., list, range).
  • Example: for i in range(5): print(i)
  • Can be nested and combined with If-Else conditions.

While Loop

  • Repeats as long as a condition is True.
  • Example: i = 0 while i < 5: print(i) i += 1

Functions

Defining Functions

  • Use def keyword to define a function.
  • Example: def add(a, b): return a + b
  • Function can call other functions and take parameters.

Return Statement

  • Returns a value from a function.
  • Example: return a + b

Input to Function

  • Functions can take user input and process it.
  • Example: def print_name(name): print("My name is", name)

Collections

Lists

  • Ordered and changeable collection. Allow duplicate members.
  • Example: a = [1, 2, 3, 4]
  • Methods: append(), insert(), remove(), etc.

Set

  • Unordered collection. No duplicate members.
  • Example: a = {1, 2, 3}
  • Methods: add(), remove(), etc.

Dictionary

  • Collection of key-value pairs. Ordered since Python 3.7.
  • Example: a = {"name": "EMC", "age": 25}
  • Methods: items(), keys(), values()

Classes and Objects

Defining Classes

  • Use class keyword.
  • Example: class MyClass: x = 5

Object Instantiation

  • Creating an instance of a class.
  • Example: p1 = MyClass()

Methods

  • Functions that belong to an object.
  • Example: class MyClass: def __init__(self, name): self.name = name def greet(self): print("Hello,", self.name)

Inheritance

Single Inheritance

  • One class inherits from another class.
  • Example: class Parent: def display(self): print("Parent class") class Child(Parent): pass

Multiple Inheritance

  • A class can be derived from more than one base class.
  • Example: class A: pass class B: pass class C(A, B): pass

Overriding Methods

  • A derived class can override methods of the base class.
  • Example: class Parent: def show(self): print("Parent method") class Child(Parent): def show(self): print("Child method")

Error Handling

Exceptions

  • Errors that occur during execution are called exceptions.
  • Try-Except block to handle exceptions.
  • Example: try: a = int(input("Enter a number: ")) b = int(input("Enter another number: ")) print(a / b) except ZeroDivisionError: print("Cannot divide by zero") except ValueError: print("Invalid input")

Logical Errors

  • Bugs in the program that produce incorrect results.
  • Not caught during compilation but cause problems during execution.