Any code first will develop the logic
next will think about how to optimize it.
for example we already seen tax_cal per a user
it requires approx 5 lines
the same code i need to use for multiple employees
Two terms
same code
always think about functions
multiple
always think about loops
In [ ]: print('hello')
print('hello')
print('hello')
In [1]: eval(input())
eval(input())
eval(input())
Out[1]: 5
loop- iterator-rotation
initial point(start point)
Condition to stop (how many times to loop)
increment/decrement
In [ ]: range(10)
range(1,10)
range(1,10,2)
In [ ]: print(1)
print(2)
print(3)
#print(i)
case-1: range(stop)=range(5)
in this case start value by defualt = 0
in this case by defulat increment by postive one= +1
in this case the last= stop-1
because the counting/start value with 0
range(5) means : 0,1,2,3,4
In [6]: for i in range(5):
print(i)
0
1
2
3
4
In [8]: # print(0)
# print(1)
# print(2)
# print(i) range(3)
for i in range(3):
print(i)
0
1
2
In [10]: # print('hello')
# print('hello')
# print('hello')
for i in range(3):
print('hello')
hello
hello
hello
In [12]: # enter the number from keyboard 3 times
eval(input('enter the number:'))
eval(input('enter the number:'))
eval(input('enter the number:'))
Out[12]: 7
In [14]: for i in range(3):
eval(input('enter the number:'))
In [16]: def f1():
eval(input('enter the number:'))
In [18]: # f1()
# f1()
# f1()
for i in range(3):
f1()
case-2: range(start,stop)=range(2,10)
in this case start value by defualt = start
in this case by defulat increment by postive one= +1
whenever we see positive increment
always last= stop-1
range(2,10) means : 2,3,4,5,6,7,8,9
start=2 increment=1 last=10-1=9
In [22]: # print(2)
# print(3)
# print(4)
# print(5)
# print(i) i==== 2,6 range(2,6)
for i in range(2,6):
print(i)
2
3
4
5
In [32]: for i in range(2,10):
print(i,end=' ')
2 3 4 5 6 7 8 9
In [50]: print(2,end='**')
print(3,end='**')
print(4,end='*')
print(5)
2**3**4*5
In [ ]: for i in range(3):
print('hello')
for i in range(222,225):
print('hello')
print(i*i)
In [ ]: # wap ask the user take 5 numbers and find the square of the number
# wap ask the user print the 7th table
# 7x1=7
# 7x2=14
# ----
# 7x10=70
# wap ask the find the divisors of 75
# idea any number if we divide 75 the reminder shoud be zero
# 75/1 75/2 75/3 ... till what number we need to divid
# which condition we need to verify
# wap ask the user get a random number 5 times
# and print it is evern or odd
# wap ask the user get a random number1
# enter a number from keyboard number2
# if both numbers are match print won otherwise loss
# give 3 chances
In [52]: # wap ask the user take 5 numbers and find the square of the number
for i in range(5):
n=eval(input('enter a number:'))
print(f'the square of {n} is:{n*n}')
the square of 20 is:400
the square of 30 is:900
the square of 40 is:1600
the square of 50 is:2500
the square of 60 is:3600
In [62]: # wap ask the user print the 7th table
# 7x1=7
# 7x2=14
# ----
# 7x10=70
n=eval(input('which table:'))
for i in range(1,11):
print(f"{n}x{i}={n*i}")
7x1=7
7x2=14
7x3=21
7x4=28
7x5=35
7x6=42
7x7=49
7x8=56
7x9=63
7x10=70
In [66]: # 75%1==0 ?
# 75%2==0 ?
# 75%76==0
# 75%i==0 i range(1,76)
n=eval(input('which divsiors:'))
for i in range(1,n+1):
if n%i==0:
print(i)
1
2
4
5
10
20
25
50
100
In [68]: # wap ask the user get a random number 5 times
# and print it is evern or odd
import random
num=random.randint(1,100)
if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')
51 is odd
In [70]: for i in range(5):
num=random.randint(1,100)
if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')
65 is odd
76 is even
73 is odd
67 is odd
21 is odd
In [72]: def even_odd():
num=random.randint(1,100)
if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')
for i in range(5):
even_odd()
54 is even
14 is even
47 is odd
17 is odd
92 is even
In [74]: for i in range(3):
n1=random.randint(1,10)
n2=eval(input('enter a number:'))
if n1==n2:
print('won')
else:
print('lost')
won
lost
won
In [78]: for i in range(3):
n1=random.randint(1,10)
print(n1)
n2=eval(input('enter a number:'))
if n1==n2:
print('won')
break
else:
print('lost')
8
lost
1
lost
1
lost
In [ ]: # case-1: if you win code should stop : break
# case-2: every wrong answer it should display how many chances left
# case-3: if all the wrong answers and your chances are over it should print
# try after 24 hours
case-3: range(start,stop,step)=range(2,20,2)
in this case start value by defualt = start
if step size is postive value increment by step value
whenever we see positive increment
always last= stop-1
if step size is negative value decrement by step value
whenever we see decrement or negative step
always last= stop+1
In [81]: for i in range(2,20,2):
print(i,end=' ')
# start= 2 step=2 last=20-1=19
# 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
# 2 4 6 8 10 12 14 16 18
2 4 6 8 10 12 14 16 18
In [ ]: range(2,20,-3)
range(2,-20,3)
range(-2,20,3)
range(-2,-20,-3)
In [83]: for i in range(2,20,-3):
print(i)
In [85]: for i in range(-2,20,3):
print(i,end=' ')
-2 1 4 7 10 13 16 19
In [ ]: range(3,20,3) # w
range(3,20,-3) # nw
range(3,-20,3) # nw
range(-3,20,3) # w
range(3,-20,-3) # w
range(-3,20,-3) # nw
range(-3,-20,3) # nw
range(-3,-20,-3) # w
range(20,3,3) # nw
range(20,3,-3) # w
range(20,-3,3) # nw
range(-20,3,3) # w
range(-20,3,-3) # nw
In [ ]: # wap ask the user get sum of 10 natural numbers
# natural means starting from 1
# countable
# ans: 1+2+3+4+5+6+7+8+9+10=55
# n*(n+1)/2
# 10*11/2= 5*11=55
0+1=1
1+2=3
3+3=6
6+4=10
10+5=15
15+6=21
21+7=28
28+8=36
36+9=45
45+10=55
summ=0
summ=summ+i
Summation wrapper
In [2]: summ=0
for i in range(1,11):
summ=summ+i
print(summ)
# step-1: summ=0 i=1 summ=0+1=1 print(1)
# step-2: summ=1 i=2 summ=1+2=3 print(3)
# step-3: summ=3 i=3 summ=3+3=6 print(6)
1
3
6
10
15
21
28
36
45
55
In [4]: summ=0
for i in range(1,11):
summ=summ+i
print(summ)
55
In [6]: for i in range(3):
print(i)
0
1
2
In [8]: i
Out[8]: 2
In [ ]: # wap ask the user get sum of squares between 1 to 10
# wap ask the user get sum of even numbers between 1 to 100
# wap ask the user get 5 random numbers print sum of even and sum of add
In [12]: # wap ask the user get sum of squares between 1 to 10
summ=0
for i in range(1,11):
summ=summ+i*i
summ
Out[12]: 385
In [16]: even_summ=0
odd_summ=0
for i in range(1,101):
if i%2==0:
even_summ=even_summ+i
else:
odd_summ+=i # odd_summ=odd_summ+i
print("even_summ:",even_summ)
print("odd_summ:",odd_summ)
even_summ: 2550
odd_summ: 2500
In [20]: import random
even_summ=0
odd_summ=0
for i in range(5):
num=random.randint(1,100)
if num%2==0:
even_summ=even_summ+num
else:
odd_summ+=num # odd_summ=odd_summ+i
print("even_summ:",even_summ)
print("odd_summ:",odd_summ)
even_summ: 76
odd_summ: 113
counter wrapper
counter will give how many runs happend
counter will use for how many succsess full runs
it is similar to summation summ=0, summ=summ+i
counter=0 counter=counter+1
In [23]: import random
even_count=0
odd_count=0
for i in range(5):
num=random.randint(1,100)
if num%2==0:
even_count=even_count+1
else:
odd_count+=1 # odd_summ=odd_summ+i
print("even_summ:",even_count)
print("odd_summ:",odd_count)
even_summ: 2
odd_summ: 3
In [25]: # wap ask the userget sum of digits
# input 12345
# output : 1+2+3+4+5=15
summ=0
for i in range(5):
num=eval(input('Enter a number between 1 and 5: '))
summ=summ+num
print(f'Sum is {summ}')
Sum is 15
In [31]: # idea
# step-1: 12345
# <we need to extract each digit sep>
# step-2: 1+2+3+4+5
i in range(12345)
i 0 ,1,2,3,4,5,6,.......12344
Out[31]: 3
in operator
in will use direct access of string charcters
In [35]: for i in range('python'):
print(i)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[35], line 1
----> 1 for i in range('python'):
2 print(i)
TypeError: 'str' object cannot be interpreted as an integer
In [37]: 'p' in 'python'
'y' in 'python'
i in 'python'
Out[37]: True
In [39]: for i in 'python':
print(i)
p
y
t
h
o
n
In [ ]: sum
max
min
eval
range
In [41]: str1='python'
for i in str1:
print(i,end=' ')
p y t h o n
In [47]: summ=0
num=12345
for i in str(num):
summ=summ+int(i)
# int+str
summ
Out[47]: 15
In [ ]: 'p'>'P' # False
'apple'=='APPLE' # False
ASCII: American standard code Information Interchange
In [58]: # A=65 a=97
ord('p'),ord('a'),ord('A'),ord('P')
# 'p'>'P'
# 112>80 # True
Out[58]: (112, 97, 65, 80)
In [60]: 'p'>'P'
Out[60]: True
In [62]: chr(112),chr(1000)
Out[62]: ('p', 'Ϩ')
In [66]: for i in range(65,97):
print(i,chr(i),end='==>')
65 A==>66 B==>67 C==>68 D==>69 E==>70 F==>71 G==>72 H==>73 I==>74 J==>75 K==>76 L
==>77 M==>78 N==>79 O==>80 P==>81 Q==>82 R==>83 S==>84 T==>85 U==>86 V==>87 W==>8
8 X==>89 Y==>90 Z==>91 [==>92 \==>93 ]==>94 ^==>95 _==>96 `==>
In [68]: ord('A')
Out[68]: 65
In [70]: chr(65)
Out[70]: 'A'
In [72]: for i in range(2308,2361):
print(i,chr(i),end='==>')
2308 ऄ==>2309 अ==>2310 आ==>2311 इ==>2312 ई==>2313 उ==>2314 ऊ==>2315 ऋ==>2316
ऌ==>2317 ऍ==>2318 ऎ==>2319 ए==>2320 ऐ==>2321 ऑ==>2322 ऒ==>2323 ओ==>2324 औ==>
2325 क==>2326 ख==>2327 ग==>2328 घ==>2329 ङ==>2330 च==>2331 छ==>2332 ज==>2333
झ==>2334 ञ==>2335 ट==>2336 ठ==>2337 ड==>2338 ढ==>2339 ण==>2340 त==>2341 थ==>234
2 द==>2343 ध==>2344 न==>2345 ऩ ==>2346 प==>2347 फ==>2348 ब==>2349 भ==>2350 म==>2
351 य==>2352 र==>2353 ऱ==>2354 ल==>2355 ळ==>2356 ऴ ==>2357 व==>2358 श==>2359 ष==
>2360 स==>
In [74]: for i in range(3077,3150):
print(i,chr(i),end='==>')
3077 అ==>3078 ఆ==>3079 ఇ==>3080 ఈ==>3081 ఉ==>3082 ఊ==>3083 ఋ==>3084 ఌ==>3
085 ==>3086 ఎ==>3087 ఏ==>3088 ఐ==>3089 ==>3090 ఒ==>3091 ఓ==>3092 ఔ==>3093 క=
=>3094 ఖ==>3095 గ==>3096 ఘ==>3097 ఙ==>3098 చ==>3099 ఛ==>3100 జ==>3101 ఝ==>3
102 ఞ==>3103 ట==>3104 ఠ==>3105 డ==>3106 ఢ==>3107 ణ==>3108 త==>3109 థ==>3110
ద==>3111 ధ==>3112 న==>3113 ==>3114 ప==>3115 ఫ==>3116 బ==>3117 భ==>3118 మ==>
3119 య==>3120 ర==>3121 ఱ==>3122 ల==>3123 ళ==>3124 ఴ==>3125 వ==>3126 శ==>3127
ష==>3128 స==>3129 హ==>3130 ==>3131 ==>3132 ఼==>3133 ఽ==>3134 ా==>3135 ి==>313
6 ీ==>3137 ు==>3138 ూ==>3139 ృ==>3140 ౄ==>3141 ==>3142 ె==>3143 ే==>3144 ై==>3
145 ==>3146 ొ==>3147 ో==>3148 ౌ==>3149 ్==>
In [ ]: