Class XI IP - Basic Python Program Codes
Class XI IP - Basic Python Program Codes
STRING H e l l o W o r l d !
BACKWAR
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
D INDEX
770943099.docx 1
print(s) # Hello World!
print(s[0]) # H
s[8] or s[-4] # r
print(s[-1]) # !
print(s[2:5]) # llo #2-start 5-1=stop; 5 is excluded
print(s[-5:-2]) # orl
print(s[2:]) # llo World!
print(s[:2]) # He
print(s*3) # Hello World!Hello World!Hello World!
print(s+"Nainital") # Hello World!Nainital
print(len(s)) # 12
print(s.lower()) # hello world!
print(s.upper()) # HELLO WORLD!
print(s.isalpha()) # False
print(s.isdigit()) # False
print(s.isspace()) # False
print(s.find(s1)) # 6
print(s.startswith(s3)) # False
print(s.startswith(s4)) # True
print(s.endswith('!')) # True
print(s.endswith('.')) # False
print(s.join(s1)) # WHello World!oHello World!r
print(s.join(s1)) #WHello World!oHello World!rHello World!d
print(s1+s6.strip()+s5) # WorabcdBVM
print(s.replace(s1,s2)) # Hello 555ld!
print(s.split(delimiter)) # ['Hello', 'World!']
print(s.split(s1)) # ['Hello ', 'ld!'] #split at Wor
print(s.swapcase()) # hELLO wORLD!
print(s1.swapcase()) # hELLO wORLD!
#============================================================================
#07
#============================================================================
a = b = c = 10
print(a,b,c) #10 10 10
x, y, z = 10, 20, 30
print(x, y, z) #10 20 30
#============================================================================
#08
#============================================================================
name = input("Enter your name: ")
school = input("Enter your school name: ")
print(type(name),type(school))
print(name+" so, you study in "+ school+".")
----------------------------
Enter your name: Rajesh
Enter your school name: BVM
<class 'str'> <class 'str'>
Rajesh so, you study in BVM.
#============================================================================
#08
#============================================================================
p = float(input("Enter the amount of principal: "))
r = float(input("Enter the rate of interest: "))
t = int(input("Enter the time: "))
si = p*r*t/100
print(type(p),type(r),type(t))
print("The Simple Interest is ",si)
----------------------------
Enter the amount of principal: 1200
Enter the rate of interest: 10
Enter the time: 2
<class 'float'> <class 'float'> <class 'int'>
The Simple Interest is 240.0
#============================================================================
#09
#============================================================================
x = 2 * 3 ** 2 / 5 + 10 // 3 – 1
2 * 9 / 5 + 10 // 3 – 1
18 / 5 + 10 // 3 – 1
3.6 + 10 // 3 – 1
770943099.docx 2
3.6 + 3 – 1
6.6-1
5.6
1+1+1+1
32 => 3**2
()
**
* /
+ -
(2+3)*6
2*3/6
2/3*6
print(type(x))
print("The expression is evaluated to ",x)
-----------------------------
<class 'float'>
The expression is evaluated to 5.6
#============================================================================
#10
#============================================================================
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = num1 + num2
print(type(num3))
print("The sum of ",num1," and ",num2," is ",num3)
-----------------------------
Enter first number: 12
Enter second number: 15
<class 'float'>
The sum of 12.0 and 15.0 is 27.0
#============================================================================
#11
#============================================================================
so far we have worked with sequential statements
conditional statement
age = int(input("Enter your age: "))
if age >= 18:
print("You can cast your vote")
else:
print("You cannot cast your vote")
-----------------------------
Enter your age: 17
You cannot cast your vote
-----------------------------
Enter your age: 19
You can cast your vote
name = input("Enter your name")
# = is assignment means to store a value in a variable
# == is equality
if name == 'Aditya':
print("You are registered in the class")
else:
print("You cannot be registered in the class")
#============================================================================
#12
#============================================================================
number = int(input("Enter a number: "))
if number>=1 and number <=10:
print("Grade 1")
elif number>=11 and number <=20:
print("Grade 2")
else:
print("Grade 3")
770943099.docx 3
print("THREE")
elif digit == 4:
print("FOUR")
...
else:
print("INVALID NUMBER")
-----------------------------
Enter a digit: 4
INVALID NUMBER
-----------------------------
Enter a digit: 1
ONE
-----------------------------
Enter a digit: 2
TWO
#============================================================================
#13
#============================================================================
i = 1
while i <= 10:
print(i) # prints integers from 1 to 10 in different lines
i+=1 #i=i+1
#============================================================================
#14
#============================================================================
i = 0
while i < 10: #LOOP
i += 1
if i==3:
break
print(i)
------------------
1
2
#============================================================================
#15
#============================================================================
i = 0
while i < 10:
i += 1
if i==3:
continue
print(i)
------------------
prints integers from 1 to 10 in different lines except 3
#============================================================================
#16
#============================================================================
n=10
for i in range(1, n):
print(i) # prints integers from 1 to 9
excluding n i.e. 10
#============================================================================
#17
#============================================================================
n=10
for i in range(n):
print(i) # prints integers from 0 to 9
#============================================================================
#18
#============================================================================
n=10
for i in range(3, n):
print(i) # prints integers from 3 to 9
#============================================================================
#19
#============================================================================
n=10
for i in range(3, n, 3):
print(i) # prints integers 3, 6 and 9
770943099.docx 4
#============================================================================
#20
#============================================================================
def fun():
num1=int(input("Enter a number: "))
num2=int(input("Enter a number: "))
num3=num1+num2
print("Sum of ",num1," and ",num2," is ",num3)
fun()
#============================================================================
#21
#============================================================================
def fun(n):
for i in range(0, n):
for j in range(0, i+1):
print("* ", end="")
print("\r")
fun(4)
---------------------------
*
* *
* * *
* * * *
#============================================================================
#22
#============================================================================
PYTHON IS A CASE-SENSITIVE LANGUAGE
s="implementation"
count=0
vowel=set("aeiouAEIOU")
for ch in s:
if ch in vowel:
count = count+1
print("The number of vowels in the given text is ",count)
---------------------------
6
#============================================================================
#23
#============================================================================
s="Birla Vidya Mandir since 1947 @ Nainital million $ Institute."
lower = upper = digit = alpha = symbol = 0
for ch in s:
if ch.islower():
lower = lower + 1
elif ch.isupper():
upper = upper + 1
elif ch.isdigit():
digit = digit + 1
elif ch.isalpha():
alpha = alpha + 1
else:
symbol=symbol + 1
print("No. of Lowercase letters: ",lower)
print("No. of Uppercase letters: ",upper)
print("No. of Digits letters: ",digit)
print("No. of Alphabets letters: ",alpha)
-------------------------------------------
No. of Lowercase letters: 40
No. of Uppercase letters: 5
No. of Digits letters: 4
No. of Alphabets letters: 0
#============================================================================
#24
#============================================================================
#LIST
#min() and max() functions work on a list of homogeneous elements only
#though a list can a collection of heterogeneous elements
integer position = index which starts with 0
Forward Index 0 1 2 3
List 1 "ABCD" -3 49.5
Backward Index -4 -3 -2 -1
mylist=[1,2,3,4,6,7,8]
print(mylist[0])
770943099.docx 5
print(mylist)
minpos = mylist.index(min(mylist)) # works with list of int only
maxpos = mylist.index(max(mylist))
print("The smallest item is ",mylist[minpos]," and it is at position ",minpos)
print("The largest item is ",mylist[maxpos]," and it is at position ",maxpos)
mylist.append(-5)
print(mylist)
minpos = mylist.index(min(mylist)) # works with list of int only
maxpos = mylist.index(max(mylist))
print("The smallest item is ",mylist[minpos]," and it is at position ",minpos)
print("The largest item is ",mylist[maxpos]," and it is at position ",maxpos)
-----------------------------------------------------
1
[1, 2, 3, 4, 6, 7, 8]
The smallest item is 1 and it is at position 0
The largest item is 8 and it is at position 6
[1, 2, 3, 4, 6, 7, 8, -5]
The smallest item is -5 and it is at position 7
The largest item is 8 and it is at position 6
#============================================================================
#25
#============================================================================
mylist=['phy', 'chem', 'bio']
print(mylist[0])
print(mylist)
minpos = mylist.index(min(mylist)) # works with list of int only
maxpos = mylist.index(max(mylist))
print("The smallest item is ",mylist[minpos]," and it is at position ",minpos)
print("The largest item is ",mylist[maxpos]," and it is at position ",maxpos)
mylist.append("art")
print(mylist)
minpos = mylist.index(min(mylist)) # works with list of int only
maxpos = mylist.index(max(mylist))
print("The smallest item is ",mylist[minpos]," and it is at position ",minpos)
print("The largest item is ",mylist[maxpos]," and it is at position ",maxpos)
-------------------------------------------------------
phy
['phy', 'chem', 'bio']
The smallest item is bio and it is at position 2
The largest item is phy and it is at position 0
['phy', 'chem', 'bio', 'art']
The smallest item is art and it is at position 3
The largest item is phy and it is at position 0
770943099.docx 6