Learn by Doing – Learn Python By Examples Part 1

Learn by Doing – Learn Python By Examples Part 1

Python is an highly expressive, dynamically typed, high-level, general-purpose programming language.

KtmBytes Python By Example 1: Printing Hello World in Python
Code:

print("Hello World!")

Output: Hello World!

KtmBytes Python By Example 1: Adding Two Numbers
Code:

num1=1
num2=5
num = num1+num2
print("Adding ",num1," and", num2 , " gives", num)
Output: Adding 1 and 5 gives 6
Code:
num1=input("input number 1:")
num2=input("input number 2:")
sum=float(num1)+float(num2)
print("the sum is :", sum)

 

KtmBytes Python By Example 1: Area of a triangle
Code:

a=3
b=5
c=6
s=(a+b+c)/2
area=(s*(s-a)*(s-b)*(s-c))**0.5
print("The Area of the triangle is :", area)
The Area of the triangle is : 7.483314773547883

KtmBytes Python By Example 1: Printing Hello World in Python
Code:

#swapping two variables
x=1.5
y=7

temp=x
x=y
y=temp
print("x = ",x,"y = ",y)

KtmBytes Python By Example 1: Finding roots of Quadratic Equation in Python
Code:

#finding Roots of Quadratic Equations
import cmath
a=1
b=6
c=8

d=(b**2)-(4*a*c)
root1=(-b-cmath.sqrt(d))/(2+a)
root2=(-b+cmath.sqrt(d))/(2+a)
print("The roots are :",root1, root2)

KtmBytes Python By Example : Converting Celsius to Fahrenheit in Python
Code:

#converting celsius to fahrenheit

celsius=36.5
fah=(celsius*1.8)-32
print('%0.1f deg Celsius = %0.1f deg Fahrenheit'%(celsius,fah))

KtmBytes Python By Example : Odd or even using python
Code:

#check if the number is odd or even
num=input("enter a number :")
num=int(num)
if num>0:
if num%2==0:
print("The number is Even")
else:
print("The number is odd")
else:
print("The number is negative number")

KtmBytes Python By Example : Finding out a leap year in Python
Code:

#Leap Year
year=int(input("enter a year :"))

if year%4==0:
if year%100==0:
if year%400==0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not leap year".format(year))

KtmBytes Python By Example : Prime number using Python
Code:

#prime number
num=int(input("enter a num :"))

if num>1:
for i in range(2,num):
if (num%i)==0:
print("The number is not a prime no")
break
else:
print("{0} is prime number.".format(num))
else:
print("The number is not a prime no")

KtmBytes Python By Example : Printing all the prime no in range
Code:

#print all prime numbers in an interval

low=int(input("enter a lower range :"))
upp=int(input("enter a upper range :"))
print("prime no. between",low, "and",upp,"are :")

for num in range(low,upp + 1):
if num>1:
for i in range(2, num):
if (num%i)==0:
break
else:
print(num)

KtmBytes Python By Example Part :  Factorial of a number 
Code:

#to find the factorial of a number
num= int(input("enter a number :"))
f=1
if num<0:
print("Sorry factorial does not exist for negative numbers")
elif num==0:
print("Factorial of 0 is 1")
else:
for i in range (1,num+1):
f = f * i
print("Factoriall of ",num, " is : ",f)