Programming Fundamentals
Programming Fundamentals
Programming Fundamentals
1. Start
2. Read the first number (num1)
3. Read the second number (num2)
4. Add num1 and num2 and store the result in sum
5. Display sum
6. Stop
START -> Input num1 -> Input num2 -> Add num1 and num2 -> Store the result in
sum -> Display sum -> END
Sometimes, you may need to convert a value from one data type to
another. This is known as type conversion or casting. The two main
types of casting are implicit casting (also known as coercion) and
explicit casting.
int num1 = 5;
float num2 = (float) num1; // Explicitly cast num1 to a float
if condition:
# Code to be executed if condition is true
if condition:
# Code to be executed if condition is true
else:
# Code to be executed if condition is false
if condition1:
# Code to be executed if condition1 is true
if condition2:
# Code to be executed if both condition1 and condition2 are true
else:
# Code to be executed if condition1 is true and condition2 is false
while condition:
# Code to be executed while the condition is true
do:
# Code to be executed
while condition
Control flow statements allow you to control the flow of your program.
They include:
Functions are blocks of reusable code that perform specific tasks. They
allow you to break down your program into smaller, more manageable
parts, making your code modular and easier to understand. Functions can
take inputs (arguments) and produce outputs (return values).
def function_name(parameters):
# Function code
return value # (optional)
Arrays and lists are data structures that allow you to store multiple
values of the same or different data types. They provide a convenient
way to work with collections of data.
Arrays and lists have a wide range of algorithms and operations that
can be performed on them, such as sorting, searching, and traversing.
Some common algorithms include:
print("Hello, World!")
In addition to console input and output, programs can read from and
write to files. File input and output operations are essential for data
storage and retrieval.
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception
finally:
# Code to be executed regardless of an exception
Error handling and exception handling are essential for building robust
and resilient programs that can gracefully handle unexpected
situations.
class Circle:
def __init__(self, radius):
self.radius = radius
def calculate_area(self):
return 3.14 * self.radius * self.radius
class Animal:
def __init__(self, name):
self.name = name
def sound(self):
pass # Placeholder method
class Dog(Animal):
def sound(self):
return "Woof!"
class Cat(Animal):
def sound(self):
return "Meow!"
dog = Dog("Buddy")
cat = Cat("Whiskers")
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Person:
def __init__(self, name):
self.name = name
def __del__(self):
print("Destructor called")
person = Person("John")
del person # Destructor called
print(variable_name)
import pdb
pdb.set_trace() # Set a breakpoint
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("Debug message")
logging.error("Error message")
class TestAddNumbers(unittest.TestCase):
def test_add_numbers(self):
result = add_numbers(2, 3)
self.assertEqual(result, 5)
if __name__ == '__main__':
unittest.main()
import unittest
class TestMultiply(unittest.TestCase):
def test_multiply(self):
result = multiply(2, 3)
self.assertEqual(result, 6)
if __name__ == '__main__':
unittest.main()
Testing your code thoroughly helps identify and address issues early,
leading to more robust and stable software.
Version Control Systems (VCS) are tools that help manage changes to
source code and facilitate collaboration among developers. VCS allows
you to track modifications, revert to previous versions, and merge
changes made by multiple developers.
Version control systems manage code repositories, which store the code
and its history. Repository management involves tasks such as creating,
cloning, and managing branches.
git merge <branch_name> # Merge changes from branch_name into the current
branch
# Git will mark conflicts in the file. Edit the file to resolve conflicts and
choose the desired changes.
git add <file_name> # Stage the resolved file
git commit -m "Merge conflict resolution" # Commit the merged changes
Project management tools help manage and track the progress of software
development projects. They provide features for task tracking, team
collaboration, and overall project management. Some popular project
management tools include: