You are on page 1of 2

1.

Python Hello World

print('Hello, world!')

2. Sum of two numbers

num1 = 1.5

num2 = 6.3

sum = num1 + num2

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

3. Square root

num = 8

num_sqrt = num ** 0.5

print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

4. Area of a triangle

a=5

b=6

c=7

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)

5. Quadratic equation

import cmath

a=1

b=5

c=6

d = (b**2) - (4*a*c)

sol1 = (-b-cmath.sqrt(d))/(2*a)

sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))


6. Swap two variables

x=5

y = 10

temp = x

x=y

y = temp

print('The value of x after swapping: {}'.format(x))

print('The value of y after swapping: {}'.format(y))

You might also like