Course: Introduction to Computer Science
Lecture Title: Introduction to Programming Concepts
Date: December 26, 2024
1. Introduction to Programming:
Programming is the process of creating a set of instructions that tell a computer
how to perform a task. Programming allows us to interact with computers and create
software for various applications. The language used to write these instructions is
known as a programming language.
Key Concepts:
Algorithm: A step-by-step procedure for solving a problem.
Code: Instructions written in a programming language that a computer can
execute.
Programming Language: A formal language that provides the syntax and semantics
for writing programs. Common languages include Python, Java, C++, and JavaScript.
2. Types of Programming Languages:
High-Level Languages: These languages are closer to human languages and are
easier to read and write.
Examples: Python, Java, JavaScript, Ruby.
Advantages: Easier syntax, less error-prone, portable across different
systems.
Low-Level Languages: These languages are closer to machine code and require
more effort from the programmer.
Examples: Assembly, C.
Advantages: Faster execution, more control over hardware.
3. Basic Programming Concepts:
Variables and Data Types:
Variables are used to store data that can be used and modified by the program. Each
variable has a data type, which determines what kind of data it can store.
Common Data Types:
Integer (int): Whole numbers (e.g., 5, -3).
Floating-point (float): Decimal numbers (e.g., 3.14, -0.001).
String (str): A sequence of characters (e.g., "Hello, World!").
Boolean (bool): Represents truth values (True or False).
Example:
age = 25 # integer variable
height = 5.9 # float variable
name = "Alice" # string variable
is_student = True # boolean variable
Operators:
Operators are symbols that perform operations on variables and values.
Arithmetic Operators:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
// (floor division)
% (modulus)
Comparison Operators:
== (equal to)
!= (not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Logical Operators:
and
or
not
Example:
x = 10
y = 5
result = x > y # result will be True
4. Control Flow:
Conditional Statements (If-Else):
Conditional statements allow a program to make decisions based on conditions.
If statement: Executes a block of code if the condition is true.
Else statement: Executes a block of code if the condition is false.
Example:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops:
Loops allow the repeated execution of a block of code.
For Loop: Used when you know how many times you want to repeat a block of code.
While Loop: Used when you want to repeat a block of code as long as a condition
is true.
Example (For Loop):
for i in range(5):
print(i) # Prints numbers 0 to 4
Example (While Loop):
count = 0
while count < 5:
print(count)
count += 1 # Increments count by 1
5. Functions:
A function is a block of code that performs a specific task and can be called by
name. Functions help organize code and make it reusable.
Defining a Function:
def greet(name):
print(f"Hello, {name}!")
Calling a Function:
greet("Alice") # Outputs: Hello, Alice!
Return Values: Functions can return values to the caller using the return
keyword.
Example:
def add_numbers(a, b):
return a + b
result = add_numbers(3, 4)
print(result) # Outputs: 7
6. Arrays and Lists:
An array (or list in Python) is a collection of items stored at contiguous memory
locations. It can store multiple values of different types.
Lists: Lists are ordered collections that can store multiple items.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Outputs: apple
Adding and Removing Items from a List:
fruits.append("orange") # Adds 'orange' to the list
fruits.remove("banana") # Removes 'banana' from the list
7. Object-Oriented Programming (OOP):
OOP is a programming paradigm that uses objects and classes. A class defines the
blueprint for creating objects, which are instances of that class.
Class: A blueprint for creating objects (a collection of attributes and
methods).
Object: An instance of a class.
Example:
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
print(f"{self.name} says woof!")
# Creating an object (instance of Dog)
my_dog = Dog("Buddy", "Golden Retriever")
my_dog.bark() # Outputs: Buddy says woof!
8. Key Takeaways:
Programming is all about creating algorithms and writing code to solve
problems.
Key concepts include variables, operators, control flow, loops, functions, and
object-oriented programming.
Python is an easy-to-learn programming language used in various applications,
including web development, data analysis, and automation.
Functions, loops, and conditionals help manage the flow of a program, making it
more efficient and organized.
Next Lecture: Introduction to Data Structures (Lists, Stacks, Queues)
Homework: Practice exercises on variables, operators, and control flow. Create a
small program using functions and conditional statements.