Introduction
This tutorial helps to write and understand sample programs written in the Python programming language. Python is a fast-growing language and is used in many fields nowadays.
We suggest the developer copy the code and try to run it in your favorite IDE. Hope you will enjoy this tutorial 🙂
Factorial of a Number – Python Program
This program helps to find out the factorial of a given number. Please find the program written in Python below –
def factorial(number):
'''This function finds the factorial of the number passed as argument'''
if number < 0:
print('Invalid entry! Cannot find factorial of a negative number')
if number == 0 or number == 1:
return 1;
else:
return number * factorial(number - 1)
if __name__ == '__main__':
userInput = int(input('Enter the Number to find the factorial of: '))
print(' The Factorial of the number is :: ', factorial(userInput))
Output:
Enter the Number to find the factorial of: 4
The Factorial of the number is :: 24
Basic Calculator – Python Program
This program helps to execute basic arithmetic calculations like
Addition of a two numbers
Subtraction of a two numbers
Multiplication of a two numbers
Division of a two numbers
Please copy the program and execute it on your favorite IDE.
"""
Program to do basic arithmetic like addition, subtraction,
multiplication and division
"""
num1 = float(input("Enter First Number: "))
op = input("Enter The Operator: ")
num2 = float(input("Enter Second Number: "))
if op == "+":
print(num1+num2)
elif op == "-":
print(num1 - num2)
elif op == "*":
print(num1 * num2)
elif op == "/":
print(num1/num2)
else:
print("Invalid Operator passed")
Output:
Enter First Number: 56
Enter The Operator: +
Enter Second Number: 12
68.0
While Loop Code Example
This section will contain python code example related to while loop. Please find the code in below section.
i = 1
# While Loop Example
while i <= 10:
print(i)
i += 1
print("Loop Completed")
# While Loop
i = 1
while i <= 5:
print("Dharmendra", i)
i += 1
j = 5
while j > 0:
print("Jitendra", j)
j -= 1
# For Loop
x = ['Dharmenra', 12,23.3]
for i in x:
print(i)
for i in range(20, 30):
print(i)
# break continue and pass
print("**** Break Keyword Example ****")
for i in range(1, 100):
if i % 2 == 0 or i % 5 == 0:
break
else:
print(i)
print("**** Continue Keyword Example ****")
for i in range(1, 100):
if i % 2 == 0 or i % 5 == 0:
continue
else:
print(i)
print("**** Pass Keyword Example ****")
for i in range(1, 100):
if i % 2 == 0 or i % 5 == 0:
pass
else:
print(i)
words = ["Ram", "Shyam", "Gopal", 1234]
for word in words:
print(word)
# Guess Game
secret_word = "Dharmendra"
guess = ""
guess_count = 0
out_of_guesses = False
guess_limit = 10
while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter the Guess: ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("Out Of Guesses, You Lose!!!")
else:
print("You Win!")
Odd or Even Number – Python Code
This program helps to identify a number is Odd or Even.
x = 2
r = x % 2
if r == 0:
print("Even Number")
else:
print("Odd Number")
if x == 1:
print("One")
elif x == 2:
print("Two")
elif x == 3:
print("Three")
elif x == 4:
print("Four")
else:
print("Wrong Input")
print("Bye Byeeee")
Related