computer notes
computer notes
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.
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.
Example:
Operators:
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):
Example:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops:
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.
for i in range(5):
print(i) # Prints numbers 0 to 4
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:
Return Values: Functions can return values to the caller using the return
keyword.
Example:
result = add_numbers(3, 4)
print(result) # Outputs: 7
Lists: Lists are ordered collections that can store multiple items.
Example:
OOP is a programming paradigm that uses objects and classes. A class defines the
blueprint for creating objects, which are instances of that 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: