Python
Python
C, C++ are platform dependent and Java, Python, VB are platform independent
Compiled – Compile the whole program and execute next (C, Java)
Interpreter – Read each line by line and if no error that will be executed (Python)
C & Java first run the whole syntax (that means compiled) and if no errors then it will execute but
Python runs and execute line at the same time and moves to next line – Interpreted
Runtime error – If program needs to use a file but file is not available
https://www.python.org
Double click .exe file and install – Check both the check boxes for installing
a=b+3
If a small number is divisible by big number we get remainder as same small number
7/8 = 7
Value taken by “input” function is string so need to convert the value if needed to integers using int
function like below
Int function takes only integers but not float but float can take integers as well. Please see below
print("welcome to python")
a=5
b=8
c = a+b
print("Sum=",c)
c=a+b
print("Sum=",c)
c=a+b
print("Sum=",c)
\n – New line
\t = Tab
# - For comment
Functions learned:
Type
Input
Int
Float
Str
a,b = 6,7
There is another option of not converting numbers into string in print function, can use the format
function as below
Floor division - //
5/4 = 1.25
5//4 = 1 (Gives only integer value and remove decimals not round off the value)
% (Mod) - Reminder
5%2 = 1
5%5 = 0
4%5 = 4
** Exponentiation
Comparison operators:
a=4
a=a+4
a+=4
b=70
b-=10 = Answer is 50
b*=3 (30)
b%=2 (0)
b+=70 (70)
b//=3 (23)
b**=2 (23 square = 529)
Logical operators
Delimers:
()[]{},
B=3
1. Sequential
2. Conditional / selection
3. Iterative / loop
Sequential: It means execution of statements specified in the program one after the other
If <condition> :
Above getting both statements since else is working when if condition is not satisfied. So need to use
else if.
Program to print 0 and 1 alternatively up to a limit. Eg: if the limit is 5 then 0 1 0 1 0
If any variable is used to do some arithmetic operation then it should be initialised first
Sum=0
Sum=sum+i
Break statement skips the rest of the statements within the loop and exit (loop will not
continue)
Continue statement skips the rest of the statements within the loop and continue with next
iteration
Range(x) – [0,1,2,…..x-1]
Range(x,y) – [x,x+1,x+2,…..y-1]
Rnage(x,y,z) – [x,x+z,x+z+z,x+3z,……]
range(5) - [0,1,2,3,4]
range(2,9) - [2,3,4,5,6,7,8]
range(2,9,3)-[2,5,8] // In this case from 2 to 9-1 increments with jump of 3
range(9,2,-2)-[9,7,5,3] // In this case from 9 to 2-1 decrements with jump of -2
if programmer knows in advance or prior to the execution, the total number of iterations required
for the loop then need to use “FOR LOOP”
if programmer doesn’t know the number of iterations before execution then use “WHILE LOOP”
Assignment:
First scenario
**
***
Second scenario
* *
* * *
* * *
* *
Third scenario
0 1
0 1 0
Fourth scenario
1 1
0 0 0
1 1 1 1 (if enter 4)
Tutorialspoint.com
W3schools
Khan academy
Function: A small block of code that can be executed any no. of times at any position in a program
If two functions having the same name it won’t throw any error but the first function will be
over written by the last one
Def sum(x,y)
Def – Keyword
Sum(a,b)
Arguments and parameters should be the same number otherwise will get error
We can’t method over loading (polymorphism) in Python. That means same function name with
different arguments since the last function is considered as the final one.
def sum(a):
print("Sum =",a+5)
def sum(a,b):
print("Sum =",a+b)
def sum(a,b,c):
print("Sum =",a+b+c)
Sum(*a)
Recursive function – Function statements called the same function.
Eg: factorial
Fact(n)
N*fact(n-1)
This can be used an alternative of loop control structures. There should be a terminating condition
inside the recursive function to terminate in order to avoid indefinite loop.
Stack – Stack follows Last in first out. Reverse of Queue (First in first out)
5!
5 * fact(4) - Bottom
In stack order it replaces from fact(1) to fact(4) and returns the final value
String can be defined in three types
A=’one’
B=”$%^”
C=’’’ It is a
Mutilined
String’’’
D=””” It is again a
Multilined
String “””
Slicing can be done with string
A=”WELCOME”
A[0]=W
A[1]=E
1. Program to check whether a string is palindrome or not
2. Program to find the number of occurrences of a character in a string.
3. Program to replace a string with another string
Collection types – are data types that can store multiple elements in a single variable
List
Set
Tuple
Dictionary
Print(i) – In this line need to enter tab then enter two times to exit from the loop
Values can be changed, here “Safwan” changed to “Mohammed Safwan”
Adding new values using append function. Duplicates can be added. (List element can be changed,
duplicated)
Inserting in a particular position by using insert function
Write a program to remove all the occurrences of a list item in the list
Can have spaces and comma at the end – no error will return
Here l3 is new variable getting the values of l1 copied not the actual memory location
Nested list example below
Declaration
Mt=(2,3,4)
Type(mt)
If we need to create a tuple with single value need mention comma ‘,’ otherwise it consider as int,
float, string
We can’t change the value in tuple as shown above. So to change the value create a list assigning
the values from tuple, convert the values in list and again change list to tuple as below
Ss = “is”
Inheritance:
Data base – Used to store information in structured manner.
File = open(‘swap.py’)
If the file “swap.py” is in the same path in which the program is saved
Print(file.read())
Print function will print all the coding in the file swap.py in command prompt
Below are used for exception handling
Try
Except
Else
Finally
Search for python 3 math module to see the list of all maths functions
All topics
W – if exists then entire data is cleared and the new data is added