Lecture_TWP_Python_A03_1a_General_Features_of_Python
Lecture_TWP_Python_A03_1a_General_Features_of_Python
Nirjhar Dhang
√
Version : 1.732 : 3
Created on : July 21, 2016
Last revision : January 11, 2025
Overview
1 Features of Python
2 Summary
3 References
Features of Python
Comments
’’’
l-value and r-value
a=5
b=a+2
c=a+b
print(a,b,c)
Output
5 7 12
Input from console
X=raw_input("Enter number :")
Y=raw_input("Enter number :")
Xi=int(X)
Xf=float(X)
Xc=complex(X)
Yi=int(Y)
Yf=float(Y)
Yc=complex(Y)
Z=X+Y
Zi=Xi+Yi
Zf=Xf+Yf
Zc=Xc+Yc
print(’X:’,X,’Xi:’,Xi,’Xf:’,Xf,’Xc:’,Xc)
print(’Y:’,Y,’Yi:’,Yi,’Yf:’,Yf,’Yc:’,Yc)
print(’Z:’,Z,’Zi:’,Zi,’Zf:’,Zf,’Zc:’,Zc)
Input from console
Output
>>>
Enter number :10
Enter number :20
X: 10 Xi: 10 Xf: 10.0 Xc: (10+0j)
Y: 20 Yi: 20 Yf: 20.0 Yc: (20+0j)
Z: 1020 Zi: 30 Zf: 30.0 Zc: (30+0j)
>>>
Arithmetic operators
Program Output
a=30 a= 30
b=10 b= 10
print(’a=’,a) a+b= 40
print(’b=’,b) a-b= 20
print(’a+b=’,a+b) a*b= 300
print(’a-b=’,a-b) a/b= 3.0
print(’a*b=’,a*b) a//b= 3
print(’a/b=’,a/b) a%b= 0
print(’a//b=’,a//b) a**3= 27000
print(’a%b=’,a%b)
print(’a**3=’,a**3)
Relational operators
Program Output
a=30 a==b False
b=10 a!=b True
a>b True
print(’a==b’,a==b) a<b False
print(’a!=b’,a!=b) a>=b True
print(’a>b’,a>b) a<=b False
print(’a<b’,a<b)
print(’a>=b’,a>=b)
print(’a<=b’,a<=b)
Bitwise operators
Program Output
x=57 x= 57 0b111001
y=44 y= 44 0b101100
x&y 40 0b101000
print(’x=’,x,bin(x)) x|y 61 0b111101
print(’y=’,y,bin(y)) x^y 21 0b10101
print(’x&y’,x&y,bin(x&y)) ~x -58 -0b111010
print(’x|y’,x|y,bin(x|y)) x<<2 228 0b11100100
print(’x^y’,x^y,bin(x^y)) x>>2 14 0b1110
print(’~x’,~x,bin(~x))
print(’x<<2’,x<<2,bin(x<<2))
print(’x>>2’,x>>2,bin(x>>2))
Conditional statements
Program
x=5
if x >= 0:
print(’x is positive’)
Output
x is positive
Conditional statements
x=5
y=6
if x >= 0:
if y >= 0:
print(’x and y are in first quadrant’)
else:
print(’x and y are fourth quadrant’)
else:
if y >= 0:
print(’x and y are in second quadrant’)
else:
print(’x and y are third quadrant’)
Output
x and y are in first quadrant
Iterative computations
for i in range(5):
print(i,i*i)
Output
0 0
1 1
2 4
3 9
4 16
Iterative computations
i=0
while i < 5:
print(i,i*i)
i=i+1
Output
0 0
1 1
2 4
3 9
4 16
String
print(’First character:’,string1[0])
print(’Last character:’,string1[-1])
print(’Last six characters:’,string1[-6:])
print(’Characters 10-18 :’,string1[10:18])
First character: T
Last character: s
Last six characters: quotes
Characters 10-18 : string d
String
print(’First character:’,string2[0])
print(’Last character:’,string2[-1])
print(’Last six characters:’,string2[-6:])
print(’Characters 10-18 :’,string2[10:18])
First character: T
Last character: s
Last six characters: quotes
Characters 10-18 : string d
Lists
a=[1,2,3,4,5,6,7,8,9]
print(a)
print(a[::2])
print(a[3:0:-1])
fruits=[’mango’,’orange’,’apple’,
’banana’, ’guava’,’papaya’]
print(len(fruits))
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
[4, 3, 2]
6
Membership operators
Program Output
a=30 a is not in c
b=10 b is in c
c=[1,2,3,4,5,6,7,8,9,10]
if a in c:
print(’a is in c’)
else:
print (’a is not in c’)
if b not in c:
print(’b is not in c’)
else:
print (’b is in c’)
Function
def absolute(x):
if x >= 0:
return x
else:
return -x
print(’absolute(-5):’,absolute(-5))
print(’absolute(-10.1):’,absolute(-10.1))
print(’absolute(20.3):’,absolute(20.3))
absolute(-5): 5
absolute(-10.1): 10.1
absolute(20.3): 20.3
Function
The syntax *args in function definitions in python is used to pass
a non-keyworded, variable-length argument list.
def sum(*args):
sumn=0
for n in args:
sumn=sumn+n
return sumn
print(sum(1,2,3,4,5,6,7,8,9,10))
55
Function
The syntax **kwargs in function definitions in python is used to
pass a keyworded, variable-length argument list.
def Person(**kwargs):
for key,value in kwargs.items():
print(key,’:’,value)
print(p)
test.out
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
Introduction to modules
import math
print(’sqrt(2):’,math.sqrt(2))
print(’pi:’,math.pi)
print(’sin(pi/3):’,math.sin(math.pi/3))
sqrt(2): 1.4142135623730951
pi: 3.141592653589793
sin(pi/3): 0.8660254037844386
Introduction to modules
import random
random.seed(10)
print(’random()’,random.random())
print(’uniform:’,random.uniform(1,10))
print(’randint:’,random.randint(1,10))
random() 0.5714025946899135
uniform: 4.860001492076032
randint: 10
Iterative computations
x3 x5 x7
sin(x ) =x − + − + ...
3! 5! 7!
(1)
∞
X (−1)n 2n+1
= x
n=0 (2n + 1)!
Iterative computations
print(x,math.sin(x))
Iterative computations
Output
0 1 1.0 1.0
1 3 -0.16666666666666666 0.8333333333333334
2 5 0.008333333333333333 0.8416666666666667
3 7 -0.0001984126984126984 0.841468253968254
4 9 2.7557319223985893e-06 0.8414710097001764
5 11 -2.505210838544172e-08 0.841470984648068
6 13 1.6059043836821613e-10 0.8414709848086585
7 15 -7.647163731819816e-13 0.8414709848078937
8 17 2.8114572543455206e-15 0.8414709848078965
9 19 -8.22063524662433e-18 0.8414709848078965
1.0 0.8414709848078965
Iterative computations
print(x,math.sin(x))
Iterative computations
Output
0 1 2.0 1 2.0 2.0
1 3 8.0 6 -1.3333333333333333 0.6666666666666667
2 5 32.0 120 0.26666666666666666 0.9333333333333333
3 7 128.0 5040 -0.025396825396825397 0.90793650793650
79
4 9 512.0 362880 0.0014109347442680777 0.909347442680
776
5 11 2048.0 39916800 -5.130671797338464e-05 0.9092961
359628027
6 13 8192.0 6227020800 1.3155568711124266e-06 0.90929
74515196738
7 15 32768.0 1307674368000 -2.5058226116427174e-08 0.
9092974264614476
8 17 131072.0 355687428096000 3.685033252415761e-10 0
.909297426829951
Bubble sort
L=[3,7,5,4,10,1,2]
print(L)
n = len(L)
swapped = True
x = -1
while swapped:
swapped = False
x = x + 1
for i in range(1, n-x):
if L[i - 1] > L[i]:
L[i-1],L[i]=L[i],L[i-1]
swapped = True
print(L)
[3, 7, 5, 4, 10, 1, 2]
[1, 2, 3, 4, 5, 7, 10]
Recursion
def factorial(n):
if n == 0:
return 1
elif n == 1:
return n
else:
return n * factorial(n-1)
for n in range(10):
print(n,’!:’,factorial(n))
test.out
0 !: 1
1 !: 1
2 !: 2
3 !: 6
Summary
Summary