Python
Python
· Because of its popularity, many peoples, companies etc makes projects using python
Library packages
1. in built packages
· A mix up language
Python+html
Python+java
· Faster execution
· www.python.org
· 3.10.0 version
· Input operations-
· Syntax – input()
A=7
B=7
Print(a+b)
Output = 14
A=”hari”
B=”krishnan”
Print(a+b)
Output = harikrishnan
· eg. 3 .
A=8
B=9
Print(“a+b”)
Output= a+b
· eg. 4.
output
2040
· eg. 5.
Print(a+b)
Output
60
#To perform with decimal numbers, we can use the instruction – float
(absolute numbers)
· eg.6.
Print(a+b)
Output
· Output operations -the command which we use for getting the things printed when we enter in
idle or command line interpreter is the print function which is the output function of python
print()
eg:- print(“hareena”)
· Idle .> file > new file > new window > enter program > run > run module 5 > save > output
Examples
2. Print(‘nitheesh’) - nitheesh
Eg:-
Print(anupama)
Output = 4
· If we give without quotes, then results will be processed, if there is any value, it will be
processed and output will be given
Eg:-
r=4
Print(“r”)
Output – r
· In what always python accepts input or in what always we can give input
1. list
2. tuple
3. set
4. dictionary
5. string
6. int
7. float
List
· eg.
1. A=["bus”,”car”,”truck”,”motorcycle”]
Print(type(a))
#A is a variable which is used to store items such as bus, truck, car and
motorcycle
2. raj=["apple”,”orange”,”cherry”]
Print(type(raj))
1. A=["apple”,”car”,”orange”,”cherry”]
Print(len(a))
Output=4
Tuple
· Eg:-
1. A=("eye”,”ear”,”nose”,”head”)
Print(type(a))
Output=<class ‘tuple’>
A=("apple”,”orange”,”cherry”,”mango”)
Print(len(a))
Output= 4
Set
· It is represented using { }
· Eg:-
1. A={“truck”,”car”,”bus”}
Print(type(a))
A={“car”,”bus”,”truck”}
Print(len(a))
Output= 3
Dictionary
· To create tables
· Eg:-
A={“name”:”sandra”,”age”:”21”,address”:”poolakkal house”} -
Print(type(a))
Output=<class ‘dict’>
1. Decision making
2. Looping structure
Decision making
1. If condition
· Syntax
If condition :
statement
· Eg:-1.
A=3
If a>0:
Print(“a is greater”)
Output = a is greater
A is positive number
· eg.2.
Vincy=-4
If vincy>0:
Print(“vincy is greater”)
Output= no output
2. if else statement
· Syntax
If condition:
Statement
Else :
Statement
Eg:-1.
A=-8
If a>0:
Print(“a is greater”)
Else :
Print(“a is lesser”)
Output: a is lesser
eg.2.
a=6
If a=0:
Print(“a is equal”)
Else:
Print(“a is lesser”)
eg.3
A=6
If a==6:
Print(“a is equal”)
Else:
Print(“a is lesser”)
Output= a is equal
· Syntax
If condition:
Statements
Elif condition:
Statements
Else:
statements
Eg:- 1. eg.2
A=3.5 a=-4
If a>0: if a>0:
Else: else:
4. Nested if statement
· Syntax
If condition:
If condition:
Statement
Else:
Statement
Else:
statement
Eg:- 1.
If a>=0:
if a==0:
print(“a is zero”)
else:
Else:
· It is used for iterating over a sequence, means it travels throughout the sequence
Eg:_
A=["apple”,”cat”,”dog”]
For x in a:
Print(x)
Output:- apple
Cat
Dog
· When we give multiple inputs and we need to get multiple outputs then we use for loop
Eg:-
Hareena=["car”,”bus”,”truck”]
Output- car
Bus
Truck
For x in “nadeeka”
Print(x)
Output=n
Eg:-’
a=["car”,”bus”,”cat”,”tiger”,”truck”]
for x in a:
print(x)
if x==”cat”:
break
output = car
bus
cat
A=["car”,”bus”,”tiger”,”truck”]
For x in a:
If x==”tiger”:
Break
Print(x)
Output= car
Bus
21/06/2023
CONTINUE STATEMENT
· In this where we apply continue that item will not come in output
· Eg:-1. list
Hareena=["nitheesh”,”nadeeka”,”maria”,”anu”,”meera”]
If ancy==”maria”:
Continue
Print(ancy)
Output= nitheesh
Nadeeka
Maria
meera
If vincy==”i”:
continue
Print(vincy)
Output= m
RANGE FUNCTION
· It returns a sequence of numbers starting from zero and increments by 1 and ends at a specified
number.
· Syntax
Range()
Eg.1.
For x in range(9):
Print(x)
Output= 0
7
8
Eg2.
For x in range(2,6):
Print(x)
Output= 2
Eg.3.
For x in range(2,10,2):
Print(x)
Output = 2
Eg.4.
For x in range(4,20,5):
Print(x)
Output = 4
14
19
For x in range(8):
Print(x)
Else:
Print(“the-end”)
Output= 0
the-end
· Eg.
A=["red”,”yellow”,”white”,”orange”]
B=["car”,”bus”,”truck”]
For x in a:
For d in b:
Print(x,d)
Output=
Red car
Red bus
Red truck
Yellow car
yellow bus
yellow truck
White car
white bus
white truck
Orange car
orange bus
orange truck
PASS STATEMENT
· For loops cannot be empty, but for some condition or reason a for loop with no content will be
put in the pass statement to avoid getting an error
· Eg.
For x in (0,1,2):
Pass
· Eg.2.
For x in “raj”:
Pass
Output= blank
04/07/2023
· To create a file using python software, create content, to edit texts etc
· To create a .txt file, pdf, word etc
· Python has several function for creating, reading, updating and deleting files
· We need a function
Open()
-File name
-Mode
· Syntax
F=open()
F=open(“file name”,”mode”)
F=open(“raj.txt”,”r”)
· 4 types of mode
already we have some contents in a file, for adding extra contents we use
append
3. Write - “w”
it will open the file for writing and create a file if it doesn’t exist
Eg:-
F=open(“file source”,”r”)
Print(f.read())
Print(f.read(5))
F=open(“file source”,”r”)
Print(f.readline())
Print(f.readline())
· Append mode
F=open(“file source”,”a”)
F.close()
· to delete file
Import OS
OS.remove(“file source”)
STRINGS
· List of characters in order-immutable
· Accessing values in a string (In python sting index starts from zero)
S[0]
S[5]
S[18]
S[19]
Print(“s[3]”,s[3])
S[:]
A=’welcome’
A[1:5]
A[2:]
A[-1]
A[::-1] #reversing
A=’welcome’
Print(“w” in a)
Print(“s” in a)
K=”w” in a
· Not-in operator
Print(“s” not in a)
Eg:-
S[0]
‘D’ -----o/p
S[0:3]
‘Dat’ ---------o/p
S[0:4]
‘Data’ -------o/p
S[0:6]
‘Data S’ ---------o/p
A=’welcome’
L-len(a)
H=”hello"
Print(h.count(“l”))
· To find index of a given character in a string
Print(h.find(“l”))
Print(f.count(“ “))
02/08/2023
OPERATORS
· Arithmetic operators
· relational operators
· logical operators
· assignment operators
· bitwise operators
ARITHMETIC OPERATORS
· + ----------- addition
· _ ------------substraction
· *------------multiplication
· /------------float division
· //-----------floor division
· %-----------modulus/remainder
· **----------exponentiation
c=a+b
+ is the operator
a+b is the operation
eg:-
a=11
b=10
c=a+b
3. 3+6
4. 6-4
7. 10%3 # remainder
8. 2**3 # exponetiation
RELATIONAL OPERATOR
9. == equal to a==b
eg:-
a=9
b=7
c=a>b
output= true
· output will be true / false only
Eg:-
x=10
y=5
z=2
print(x>y) o/p--true
print(x>y>z) o/p--true
2. less than
print(x<y) o/p---false
print(y<x) o/p---true
3. equal to
print(x==y) #false
print(x==10) #true
4. not equal to
print(x!=y) #true
print(10!=x) #false
and so on
LOGICAL OPERATOR
OR
0+0 0
0+1 1
1+0 1
1+1 1
AND
0.0 0
0.1 0
1.0 0
1.1 1
eg:
1. a=5
b=0
c= a and b
output = 0
2. a=5
b=0
c=a or b
output= 5
3. a=1
not(a)|
4. a=0
not(a)
ASSIGNMENT OPERATOR
· To used to assign values to variables
a=7
print(a)
1. a=a+5
a+=5
a output = 17
2. a=a-5
a-=5
a output=7
3. a=a*5
a*=5
a output=175
BITWISE OPERATORS
· |---bitwise OR
· bit numbers
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
a=10
b=4
· bitwise multiplication(AND)
0.0 0
1.0 0
0.1 0
1.1 1
c=a & b
c #0000
· bitwise addition(AND)
0+0 0
0+1 1
0+1 1
1+1 0
1+1+1 1
e=a^b
e #1110
· bitwise negation
a=10
output = -11
· bitwise shift
a>>1 output= 2
a=5
a<<1 output=10
SPECIAL OPERATORS
· membership operator
1. a1=3
b1=3
2. a2='parvathy'
b2='renisha'
L1=[1,2,3]
L2=[1,2,3]
print(id(L1)) 2350281340416
print(id(L2)) 2350279332800
x='goodmorning'
print('G' in x) true
CONDITIONAL STATEMENT
1. simple if
in R {}
python intantation
x=1
if(x==10):
2. if-else
x=1
if(x==10):
else:
x=10
y=8
print("x is less than y" if(x<y) else "x is greater than y") # A if(condition) else B
#s= "x is less than y" if(x<y) else "x greater than y"
#print(s)
· in another way
x=10
y=8
if(x<y):
else:
number=int(input("enter a number"))
number
type(number)
number_flt=float(input("enter a number")
number_flt
hareena
type(x)
<class 'str'>
3. if-elif-else
number=int(input("enter a number"))
number
if(number>0):
print("number is positive")
elif(number<0):
print("number is negative")
else:
print("number is zeo")
eg:-
x=int(input("enter a numner"))
if(x>0):
print("number is positive")
elif(x<0):
print("number is negative")
else:
print("number is zero")
number is positive
number is negative
number=int(input("enter a number"))
number
if(number<0):
else:
if(number=10):
print("number is 10")
elif(number==20):
print("number is 20")
else:
eg:-
x=int(input("enter a number"))
if(x<0):
else:
if(x==10):
print("x is 10")
elif(x==20):
print("x is 20")
else:
x is greater than 20
if(ch.isupper()):
print(ch, "is UPPERCASE alphabet")
elif(ch,islower()):
elif(ch.isdigit()):
else:
eg:
if(ch.isupper()):
elif(ch.islower()):
elif(ch.isdigit()):
print(ch,"is a digit")
else:
321 is a digit
HOMEWORK
1. Accept a string as user input. check that string with your name, if both matches then print "names are
same". if they do not match then print"names are different"
x=input("enter a name")
if(x==hareena):
else:
2. write a program to check whether the number that is taken as user input is odd or even
x=int(input("enter a number"))
if((x%2)==0):
print("number is even")
else:
print("number is odd")
in all other cases the driver is not insured . If the marital status, sex and age of the driver are the inputs,
write a program to determine whether the driver is to be insured or not
marital_status = input("Enter marital status (married/unmarried): ")
if marital_status == "married":
print("Driver is insured.")
print("Driver is insured.")
print("Driver is insured.")
else:
else: