💻

Introduction to Basic Programming Concepts

Sep 10, 2024

Lecture Notes: Introduction to Programming Concepts

Basic Operators

  • Operators include addition (+), subtraction (-), multiplication (*), and division (/).
  • Examples of using integers:
    • a = 10
    • b = 20*

Input and Output

  • The input() function can be used to take user inputs for name and age.
    • Example: name = input("Enter your name:")
  • Print statements can display outputs such as:
    • print("My name is", name)
    • Format: print(f"My age is {age}")

Arithmetic Operations

  • Functions can take inputs and perform operations like addition and multiplication.
  • Example for multiplying three variables:
    • c = a * b * d

Conditional Statements

  • Boolean Values: True or False conditions.
  • If-Else Statements:
    • Example: if income > 7000: print("Eligible for scholarship") else: print("Not eligible for scholarship")

Modulus Operator

  • Used to find the remainder.
  • Example: a % b

Even or Odd Number Check

  • To check if a number is even or odd:
    • if num % 2 == 0:
    • Print results accordingly.

Using Loops

  • For Loops:
    • Example: for i in range(1, 11): to iterate from 1 to 10.
  • While Loops:
    • Example: while i < 5: to continue until i reaches 5.

Lists and Arrays

  • Create lists to store multiple items.
    • Example: numbers = [1, 2, 3, 4, 5]
  • Access elements by index: numbers[0]

Functions

  • Define functions using def keyword to encapsulate code.
    • Example: def add(a, b): return a + b

Classes and Objects

  • Creating Classes:
    • Example: class Dog: def bark(self): print("Woof!")
  • Object Instantiation:
    • my_dog = Dog()

Exception Handling

  • Use try and except for error handling.
    • Example: try: x = int(input("Enter a number:")) except ValueError: print("Invalid input, please enter a number.")

File Operations

  • Reading and writing files using open(), read(), and write().
    • Example: with open('file.txt', 'w') as f: f.write('Hello World')

Summary

  • Understanding these concepts is crucial for programming.
  • Practice by writing small programs to reinforce learning.