Monday, October 14, 2019

Learning Python - Day 2


On day 1, we had already assigned value to the variables like a and b. Today we would be inputing values for variables.
a=int(input("Enter 1st number"))
b=
int(input("Enter 2nd number"))
c=a+b
print("Sum=",c)


run the function CTL+shift+fn+F10
Enter 1st number7
Enter 2nd number8
Sum= 15
Swapping of 2 Numbers
To Swap 2 numbers python uses the formula
a,b=b,a
Other programming langauages uses formula for swapping
t=a
a=b
b=t
or a=atb
b-aib
c=a-b
example
a=int(input("Enter 1st number"))
b=
int(input("Enter 2nd number"))

print("B'for swaping a=",a,"b=",b)

a,b=b,a
print("After swaping a=",a,"b=",b)


run the function CTL+shift+fn+F10

Enter 1st number7
Enter 2nd number8
B'for swaping a= 7 b= 8
After swaping a= 8 b= 7

CONDITIONAL STATEMENTS
n=int(input(“Enter a Number”))
if (n % 2 == 0):
   
print(n,"is even")
else:
   
print(n,"is odd")

run the function CTL+shift+fn+F10

Enter a number19
19 is odd

Example
a=int(input("Enter 1st number"))
b=
int(input("Enter 2nd number"))
if(a==b):
       print("equal no.")
else:
   
print("not equal no.")
run the function CTL+shift+fn+F10
Enter 1st number10
Enter 2nd number90
not equal no.
EXAMPLE
a=int(input("Enter 1st number"))
b=int(input("Enter 2nd number"))
if(a>b):
    print("a>b")
elif(b>a):
    print("b>a")
else:
    print("Equal")
run the function CTL+shift+fn+F10
Enter 1st number10
Enter 2nd number98
b>a
EXAMPLE
a=int(input("Enter 1st number"))
b=
int(input("Enter 2nd number"))
c=
int(input("Enter 3rd number"))
if(a>b and a>c):
   
print(a,"is largest")
elif(b>a and b>c):
   
print(b,"is largest")
elif(c>a and c>b):
   
print(c,"is largest")
else:
   
print("cant find the largest one")
run the function CTL+shift+fn+F10
Enter 1st number23
Enter 2nd number37
Enter 3rd number45
45 is largest
EXAMPLE
print("enter 5 subject marks")
a=
int(input())
b=
int(input())
c=
int(input())
d=
int(input())
e=
int(input())
avg=(a+b+c+d+e)/
5
if(avg>60):
   
print("1st Division")
elif(avg>=45):
   
print("2nd Division")
elif(avg>=30):
   
print("3rd division")

else:
   
print("Fail")
run the function CTL+shift+fn+F10
enter 5 subject marks
10
20
30
40
50
3rd division
EXAMPLE
Electricity Bill generation
Unit
0-100 =>4Rs/U
101-200=>5Rs/u
201-400=>6Rs/u
401-above=>7Rs/u
Fixed Charges=150

u=int(input("Enter consumed units"))
amt=
0
fc=150
if(u<=100):
    amt=u*
100
elif(u<=200):
    amt=
100*4+(u-100)*5
elif(u<=400):
    amt=
100*4+100*5+(u-200)*6
else:
    amt=
100*4+100*5+200*6+(u-400)*7
'''t_amt=amt+fc
print("Total bill=",t_amt)'''

if(u<=200):
    t_amt=
0
else:
    t_amt=amt+fc
print("Total Bill=",t_amt)
run the function CTL+shift+fn+F10
Enter consumed units300
Total Bill= 1650

EXAMPLE
Menu Driven program lets say Calculator
a=int(input("enter 1st number"))
b=
int(input("enter 2nd number"))
print("Press 1-add 2-sub, 3-mod")
c=
int(input())
if(c==1):
   
print("sum=",(a+b))
elif(c==2):
   
print("sub=",(a-b))
elif(c==3):
   
print("mod=",(axb))
else:
   
print("wrong choice")

run the function CTL+shift+fn+F10
enter 1st number8
enter 2nd number9
Press 1-add 2-sub, 3-mod
1

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...