Friday, June 28, 2019

mathematical functions in python

Example1:

from math import *
print(sqrt(2))


output:
1.4142135623730951


Example2:

from math import *
diameter = 5
perimeter = 2 * pi * diameter
print(perimeter)

output:
31.41592653589793

Example 3:

from math import *
Ueff = 230
amplitude = Ueff * sqrt(2)
print(amplitude)

output:
325.2691193458119


Example 4:

""" Calculate area of a circle"""
from math import *
d = float(input("Diameter: "))
A = pi * d**2 / 4
print( "Area = ",A)

output:
Diameter: 2.5
Area =  4.908738521234052

list in python

a = [2, " Jack", 45, "23 Wentworth Ave"]
print(a)


output:
[2, ' Jack', 45, '23 Wentworth Ave']

nested for loop in python

for n in range (2, 10):
    for x in range (2, n):
        if n % x == 0:
            print(n, "equals ", x, "*", n/x)
            break
    else:
        # loop fell through without finding a factor
        print(n, "is a prime number ")

output:

2 is a prime number
3 is a prime number
4 equals  2 * 2.0
5 is a prime number
6 equals  2 * 3.0
7 is a prime number
8 equals  2 * 4.0
9 equals  3 * 3.0

for and else loops in python

for n in [10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1]:
    print (n)
else:
    print(" blastoff")

output:
10
9
8
7
6
5
4
3
2
1
 blastoff

while loop in python

n = 0
while n < 10:
    print(n)
    n = n + 3



output:

0
3
6
9



while True:
    print("Hello")

output :
helloworld

prints continuously

python class topic video