IT Python
IT Python
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
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
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
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
Subroutine
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
◦ Intwhole numbers Ex: 25, 500, -1024
◦ Longlarge numbers Ex: 841981619848
◦ Floatdecimal numbers Ex: 45.3, -855.33
◦ Complexvarious types of numbers Ex: 65.9J, 900.6J
String
List
Tuple
Dictionary
Operators
Arithmetical operators
+ * ** %
- / //
Relational Operators
< <= ==
> >= != or <>
Assignment Operators
And
Or
Not
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 errorsviolation of python rules in coding
2. Runtime errorserrors occurring during execution
3. Logical errorserrors in the logical instruction of the code
Checking functions
isnumeric()
islower()
isupper()
List
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()
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 searchstep 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
IF ELIF ELSE
WHILE loop
FOR loop
Functions