0% found this document useful (0 votes)
19 views

IT Python

Can download and learn

Uploaded by

cybergenius15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

IT Python

Can download and learn

Uploaded by

cybergenius15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ICT Chapter 9

Programming
Problem Solving
 Programming is a process of problem solving
 Problem solving techniques
◦ Analyze the problem
◦ Outline the problem requirements
◦ Design steps to solve the problem

Problem solving process


 Understanding the problem
 Defining the problems and boundaries
 Planning the solution
 Check the solutions and select the best one

Modularization
 breaking a large program into modules
Advantages of Modularization
- lack of bugs
- easy troubleshoot
- several developers can work in each part separately
- can use the developed part of software wherever needed
Disadvantages
o same variable usage in the code can be a problem

Top-down Design and Stepwise Refinement


 Computer programmers use a divide and conquer approach to problem solving:
◦ a problem is broken into parts
◦ those parts are solved individually
◦ the smaller solutions are assembled into a big solution
◦ These techniques are known as top-down design and modular development

Structure Chart
 shows the breakdown of the system into its lowest manageable levels by dividing bigger
complex system into small units.
◦ Model  represents the process or the task of the system

Student Registration

◦ Arrow line  used to show the connection between the models

Algorithms
 A set of well-defined instructions in sequence to solve a problem
 It is also a representation of solution to a problem
 There are 2 ways:
◦ Flow chart
◦ Pseudo codes
Flow charts
 A graphical representation of the sequence of operations in a program

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

 It shows the sequence of instruction in a single program or a subroutine

Subroutine

Rules of Flow charts


 All boxes should be connected with arrows
 No lines only arrows
 Decision box has 2 output points
 Formulate the main line of logic and then incorporate the details
 Maintain consistency
 No deeper details
 Use common statements
 Use suitable variable names
 Keep simple as possible
 Use connectors if flowchart is drawn to another page
Pseudocodes
 An informal way to express the design of a computer program
 Provides idea to programmer on how to build the software step by step
 Written in simple English terms so even a non-programmer can understand
 Can be converted into any programming language
 Component of pseudocode:
◦ Variables used to identify the memory location with the value
◦ Assignment place a value to the variable
◦ Input/output get input from the user and print the result
◦ Control structure control statements based on conditions
▪ Sequence
▪ Selection
▪ Repetition
Advantages of pseudocodes
 Readable and understandable
 Explains each line

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

 Bridge between programmer and program


 Helps to document everything
Evolution of Programming Languages
 1st programmer – Madam Ada Augusta Lovelace
 She prepared it for Analytical Engine created by Charles Babbage (Father of Computer)
Generations of Computer Languages
1. First Generation
 Machine language – 0 and 1
 Instructions given through front panel switches directly to CPU
 No translators
 Machine dependent
 High speed as no translations needed
2. Second Generation
 Assembly language – Symbols were used to minimize complexity of binary
 Short words called mnemonics were used
 Assemblers were used for the translation
3. Third Generation
 As in 2GL many instructions were given for a simple task they had to reduce it, so they
introduced high level languages
 Codes can be used without having to be rewritten unlike assembly language
 Machine independent
 Compiler and interpreter used to convert from high level to machine language
 Ex: C, C++, Java, JavaScript, Visual Basic
4. Fourth Generation
 Can be prepared easily than 3GL
 Machine independent
 Compiler and interpreter used to convert from high level to machine language
 Ex: Perl, PHP, Python, SQL
Programming Paradigms
Imperative Languages
 Giving step by step procedure to solve a problem
 Controlling the steps of instructions in action and keeping track of step-by-step changes in
variables and instances
 Ex: C++, C, Java, PHP, Python
Declarative Languages
 Giving instructions prioritizing the output
 Ex: SQL, Prolog
Object Oriented Languages
 All instructions are given in a single file so it was difficult to find any error, if any
 Addition of new instructions was also difficult
 OOL was used to identify the tasks and separate them in different files
Characteristics of high-level languages
 Easy to learn
 Easy to detect errors
 Machine independent
 Availability of liberty functions
 Shorter program

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

 Well defined syntax and standard


 Other programmers also can understand code
Need of Program Translators
 To convert high level to low level languages, translators like compilers and interpreters are
used
Source program
 Set of instructions written by a programmer using computer language.
 Readable to human but cannot be executed by the CPU
Object program
 Both compiler and interpreter work differently
 Compilers reads the source code and creates a file by compiling the code into machine
language called as object program.
 If any errors are found then the error report will be displayed without translating the code
Program translators
 There are 3 types of translators
◦ Compiler
◦ Interpreter
◦ Assembler
Interpreters
 Translates line by line
 No precompiled program like compilers
 Slower than compiler
 When error found, it will stop in that line and display the error message
Advantages of interpreter
- Troubleshooting is easy
Disadvantages of interpreter
- Slow as it converts line by line
- Every time you run it, interpreter will have to translate again and again
Compiler
 Compilers reads the source code and creates a file by compiling the code into machine
language
 If any errors are found then the error report will be displayed without translating the code
Advantages of compiler
- Compiler compiles only once
- Low memory used as it is compiled only once
Compiler Interpreter
 Faster  Slower
 More memory needed for object code  No extra space needed
 Debugging is hard as the errors are  Displays error quickly as it finds the
displayed after checking the entire error
program
Assemblers
 Assemblers convert assembly language to machine language
Linkers
 A program that takes one or more object files generated by the compiler and combine them
as a single executable program
 A computer program consists of number of modules.

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

 Several files are generated when converting program into object program so all these files
should be operated linking together. This task is done by the linkers
Features of IDE
 Code editor  Debugger
 Translator  Execute
Python
Comment
#This is a comment
Advantages of comment
 Easy to understand the code
 Can refer the codes
 Another programmer can understand the code easily
Constants and Variables
 Variable –the name given to a memory space allocated in RAM to store the values of the
program which can change during execution of the program
 Constant –the name given to a memory space allocated in RAM to store the values of the
program which do not change during execution of the program
Rules for variables
 No reserved word
 Cannot start with a number
 No space
 No symbols except _
Local variable and global variable
 Local variable – a variable inside a function. This cannot be used in another function.
 Global variable – it can be used at any place of any function
Primitive data type
 Numbers
◦ Intwhole numbers Ex: 25, 500, -1024
◦ Longlarge numbers Ex: 841981619848
◦ Floatdecimal numbers Ex: 45.3, -855.33
◦ Complexvarious types of numbers Ex: 65.9J, 900.6J
 String
 List
 Tuple
 Dictionary
Operators
Arithmetical operators
 +  *  **  %
 -  /  //

Relational Operators
 <  <=  ==
 >  >=  != or <>
Assignment Operators
 And
 Or
 Not

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

Bitwise Operators
 Bitwise AND (&)  Bitwise XOR (^)  Right shift (>>)
 Bitwise OR (|)  Left shift (<<)
Operator precedence
 ()
 **
 *,/,**,//
 +,-
 <<,>>
 &
 ^
 |

Control structure
 Sequence
 Selection
o If
o If else
o If elif else
 Iteration
o While
o For
Break
 Condition inside a loop to stop the loop before completion
break
Continue
 Condition inside a loop to repeat the loop after completion
continue

Sub Program
 Functions help to break program into smaller parts
 Types of functions
- Built in functions functions included in python by default
- User defined functions built by the programmers according to their requirements
def printword():
print(“hello world!”)
return()
Types of errors in Python
1. Syntax errorsviolation of python rules in coding
2. Runtime errorserrors occurring during execution
3. Logical errorserrors in the logical instruction of the code

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

To handle these errors, we use,


try:
statement
except:
statement
String

Checking functions
isnumeric()
islower()
isupper()

List

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

 An array of data that can be used to store compound data (different types of data)
Ex:

Tuples
 Collection of elements separated by commas
 It is immutable can not be changed once created
Ex:
name = (“john”, “napier”, “newton”)
Dictionary
 An unordered collection of items which has a key and a value for a single element
 No duplicate key
 Case sensitive
 Any type for values
Ex:

File Handling
file_store = open(file_name,mode)
The modes
r = only read
r+ = read and write
w = only write / replace existing data
w+ = read and write / replace existing data
a = appends at end of file / if file not available then the file is created and data is added
a+ = read data and create file with append ability
file = open(“myfile.txt”, “w”)
file.write(“hello world\n”)
file.write(“this is the next line\n”)
file.close()

file = open(“myfile.txt”, “r”)

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

print(file.read())
print(file.readline())
print(file.readline(1))
file.close()
DBMS
 Types of databases
- MySQL - Interbase
- Oracle - Sybase
- GadFly - Microsoft SQL Server 2000
- PostgreSQL - Informix
import mysql.connector
conn = mysql.connector.connect(user= “root”,password= “1234”,host= “localhost”,database=
“student_db”)
new = conn.cursor()
new.execute(“select * from student”)
print(new.fetchall())
conn.close()

Searching techniques
 Sequential searchstep by step ahead in an array until the target is found
o Weakness – takes time if the number to be found is in the last (if more data then
efficiency is low, if less data, then efficiency is high)
 Binary search-should be in order, compares the searched number with the middle number
and searches the list accordingly
Sorting techniques
Arranging an unordered list is called sorting
 Selection sort-(Arranging in order) compares the 1 st block with the 2nd, if the 2nd data is small
then it swaps the data, then it checks 1st with 3rd and likewise arranges the whole set
 Bubble sort compares the adjacent two blocks and swaps
IF statement

IF ELSE

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

IF ELIF ELSE

WHILE loop

FOR loop

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

Functions

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK


ICT Chapter 9

M. A. F. Rameeza B. Sc. (Hons.) in Computer Science and Software Engineering - UK

You might also like