Final Notes CSC202
Final Notes CSC202
Textbooks:
Topics to be cover:
• Critical reading
• Analytical thinking
• Creative synthesis
Computers are incredibly stupid. They do exactly what you tell them
to do: no more, no less-- unlike human beings. Computers can't
think by themselves. In this sense, they differ from human beings.
For example, if someone asks you, “What is the time?”, “Time
please?” or just, “Time?” you understand anyway that he is asking
the time but computer is different. Instructions to the computer
should be explicitly stated. Computer will tell you the time only if you
ask it in the way you have programmed it.
• Avoid duplicating code. If you find yourself doing the same thing in
multiple places, consider refactoring it into a reusable function or
class.
9. Version Control:
10. Security:
Remember that these principles are not rigid rules but guidelines to
help you make informed decisions while writing code. Balancing
these principles according to the context of your project and team
dynamics is crucial for producing effective and maintainable
software.
∞ input: Get data from the keyboard, a file, the network, or some
other device.
∞ output: Display data on the screen, save it in a file, send it over the
network, etc.
Believe it or not, that’s pretty much all there is to it. Every program
you’ve ever used, no matter how complicated, is made up of
instructions that look pretty much like these. So you can think of
programming as the process of breaking a large, complex task into
smaller and smaller subtasks until the subtasks are simple enough to
be performed with one of these basic instructions.
:
Program Design Recipe
All of these are activities that are useful, not only for a programmer
but also for a businessman, a lawyer, a journalist, a scientist, an
engineer, and many others.
Now divide the problem into small segments and calculations. Also
include examples in all segments. In this problem, we should take an
employee with his details from each category. Let’s say, Mr. Ahmad
is a permanent employee working as Finance Manager. His salary is
N200,000 and benefits of medical, car allowance and house rent are
N40,000 and there is a deduction of N12,000. Similarly, we should
consider employees from other categories. This will help us in
checking and testing the program later on.
SOFTWARE CATEGORIES
• System Software
• Application Software
System Software
o Operating system
o Device drivers
o Utilities
Operating system
Device drivers
device drivers e.g. CD Rom driver, Sound Card driver and Modem
driver. Normally manufacturer of the device provides the device
driver software with the device. For scanners to work properly with
the computers we install the device driver of the scanner. Nowadays
if you have seen a scanner, it comes with TWAIN Drivers. TWAIN
:
stands for Technology Without an Interesting Name.
Utility Software
Application software
7. Readability counts.
13. There should be one— and preferably only one —obvious way to
do it. 14. Although that way may not be obvious at first unless you're
Dutch. 15. Now is better than never.
rather than all users having that functionality but never using it.
First of all, we need a tool for writing the code of a program. For this
purpose, we used Editors in which we write our code. We can use
word processor too for this, but word processors have many other
features like bold the text, italic, coloring the text etc., so when we
save a file written in a word processor, lot of other information
:
including the text is saved on the disk. For programming purposes,
we don’t need these things we only need simple text. Text editors
are such editors which save only the text which we type. So, for
programming we will be using a text editor.
and can see whether our program is producing the correct results.
RUNNING PYTHON
Arithmetic operators
[7]. Documentation.
[8]. Maintenance
[8]. Maintenance: All the activities that occur after the completion of
the program come under the program maintenance. Program
maintenance includes the following: Finding and correcting the
errors; Modifying the program to enhance it – i.e., adapting to some
new concepts or when there is a change in the hardware or
operating system; Update the documentation; Add new features and
functions; Remove useless or redundant parts of code.
Len (String)
>>> len(fruit) = 6
Lists
String slices
‘Monty’
>>> s[6:12]
‘Python’
print(my_list[2:5])
Dictionaries
# Output: 26
print(my_dict.get(‘age’))
Tuples
Students should learn how to access a file, Open, Read and Write.
# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)f print ("Closed or not : ", fo.closed)
print ("Opening mode : ", fo.mode) fo.close()
Slicing
['b', 'c']
:
>>> t[ :4]
>>> t[3: ]
>>> t[ : ]
>>> t
['a', 'c']
>>> t.remove('b')
>>> t
['a', 'c']
Adding elements
>>> t.append('d')
>>> t
:
['a', 'b', 'c', 'd']
>>> t1.extend(t2)
>>> t1
Deleting Sliced elements >>> t = ['a', 'b', 'c', 'd', 'e', 'f'] >>> del t[1:5]
>>> t
['a', 'f']
>>> t = list(s)
>>> t
Sorting elements
>>> t
Deleting element
:
>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> t
['a', 'c']
>>> x
'b'
Spliting string into words >>> s = 'pining for the jobs' >>> t = s.split()
>>> t
Boolean expressions
x != y # x is not equal to y
x == y # x is equal to y
print(‘x is even’)
else:
print(‘x is odd’)
>>> remainder
45
Logical operators
Nested conditionals
if x == y:
else:
if x < y:
else:
Iteration
while n > 0:
print(n)
n=n–1
print(‘Blastoff!’)
For Loop
print(height)
Break
var = 10
if var == 5:
:
break
Functions
>>> type(42)
<class 'int'>
>>> float(32)
import math
def my_function():
print(pow(2, 4))
print(sqrt(x))
my_function()
:
Functions with Arguments: (Need to understand Local and Global
Variable)
return area
triarea(4, 5) = 3
Variables Scope:
Local Variables: These are variables which are declared inside the
function, compound statement (or block).
Why functions?
Variable names
The abstract data type is special kind of data type, whose behavior is
defined by a set of values and set of operations. The keyword
“Abstract” is used as we can use these data types, we can perform
different operations. But how those operations are working that is
totally hidden from the user. The ADT is made of primitive data
:
types, but operation logics are hidden.
name = "Python"
print(name)
Multiline String:
message = """
:
Never gonna give you up Never gonna let you down """
print(message)
Output:
print(str1 == str2)
print(str1 == str3)
Output:
False
True
name = "Jack"
# using + operator
print(result)
:
Output:
Hello, Jack
Methods Description
Recursion in Python
Syntax:
| (recursive call) |
func() ----
Tail-Recursion:
Example:
if n == 1:
return n
else:
return n * recursive_factorial(n-1)
# user input
num = 6
if num < 0:
else:
Output:
A value is one of the basic things a program works with, like a letter
or a number. Some values we have seen so far are 2, 42.0, and
'Hello, World!'.
Assignment statements
>>> n = 17
>>> pi = 3.141592653589793
>>> 42
42
>>> n
17
:
>>> n + 25
>>> n = 17
>>> print(n)
Order of operations
Debugging:
Runtime error: The error does not appear until after the program has
started running. These errors are also called exceptions because
they usually indicate that something exceptional (and bad) has
happened.
• Logging:
Testing:
In addition to unit tests, integration and system tests are crucial for
verifying the interactions and behavior of components within a
larger system. Integration tests focus on testing the integration
points between different modules or components, while system
tests verify the functionality and behavior of the complete system as
a whole.
• Test Automation:
AREA OF CIRCLE
radius = float(input (“Input the radius of the circle : “)) print (“The
area of the circle with radius “ + str(radius) + “ is: “ + str(pi
* radius**2))
attendanceMark = int(input())
assignmentMark = int(input())
testMark = int(input())
examMark = int(input())
else:
print(“Invalid Input!”)
nums = []
for i in range(n):
nums.insert(i, int(input()))
sum = 0
for i in range(n):
sum = sum+nums[i]
avg = sum/n
print(“\nAverage = “, avg)
:
Course Lecturer: Mr. A. A. Kadams 25 | P a g e
PROGRAMMING PARADIGMS
1. Imperative paradigm/languages.
2. Declarative paradigm/languages.
3. Functional paradigm/languages.
5. Procedural paradigm/languages.
6. Logic paradigm/languages.
__init__() sets the initial state of the object by assigning the values of
the object’s properties. That is, .__init__() initializes each new
instance of the class.
class Dog:
self.name = name
self.age = age
# Instance method
def description(self):
>>> miles.description()
1. Encapsulation,
2. Abstraction,
3. Polymorphism and,
4. Inheritance.
Class PrivateMethod:
def __init__(self):
self.__attr = 1
def __printhi(self):
print("hi")
Def printhello(self):
print("hello")
a = PrivateMethod()
a.printhello()
a.__printhi()
a.__attr
Output:
Hello
Same thing with a class! All the 'behind the scenes' mechanisms
should be encapsulated and left private, thus creating less
confusion when dealing with many classes at once.
# parent class
class Bird:
def
__init__(self):
print("Bird is
ready")
def Continues…
whoisThis(self):
peggy = Penguin()
print("Bird")
:
def swim(self): peggy.whoisThis()
print("Swim peggy.swim()
faster")
peggy.run()
# child class
OUTPUT
class
Penguin(Bird): Bird is ready
def
whoisThis(self):
print("Penguin")
def run(self):
print("Run
faster")
def flying_test(bird):
class Parrot:
bird.fly()
def fly(self):
#instantiate objects
print("Parrot can fly")
blu = Parrot()
def swim(self):
peggy = Penguin()
print("Parrot can't swim")
# passing the object
class Penguin:
flying_test(blu)
def fly(self):
flying_test(peggy)
print("Penguin can't fly") def swim(self):
OUTPUT
print("Penguin can swim")
Parrot can fly