Python programms
Word guessing Game in Python
import random
name = input("What is your name? ")
print("Good Luck ! ", name)
words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
word = random.choice(words)
print("Guess the characters")
guesses = ''
turns = 12
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_")
failed += 1
if failed == 0:
print("You Win")
print("The word is: ", word)
break
print()
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")
How to Add Two Numbers in Python
# Python3 program to add two numbers
number1 = input("First number: ")
number2 = input("\nSecond number: ")
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1,
number2, sum))
Python Program to Find the Factorial of a Number
# Input: An integer number
num = 6
# Initialize the factorial variable to 1
factorial = 1
# Calculate the factorial using a for loop
for i in range(1, num + 1):
factorial *= i
# Output: The factorial of the number
print(f"The factorial of {num} is {factorial}")
Python Program for Simple Interest
# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.
def simple_interest(p,t,r):
print('The principal is', p)
print('The time period is', t)
print('The rate of interest is',r)
si = (p * t * r)/100
print('The Simple Interest is', si)
return si
# Driver code
simple_interest(8, 6, 8)
Python Program to Find Area of a Circle
# Python program to find Area of a circle
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print("Area is %.6f" % findArea(5));
Python Program to Print all Prime numbers in an
Interval
def prime(x, y):
prime_list = []
for i in range(x, y):
if i == 0 or i == 1:
continue
else:
for j in range(2, int(i/2)+1):
if i % j == 0:
break
else:
prime_list.append(i)
return prime_list
# Driver program
starting_range = 2
ending_range = 7
lst = prime(starting_range, ending_range)
if len(lst) == 0:
print("There are no prime numbers in this range")
else:
print("The prime numbers in this range are: ", lst)
Python Program to Find Area of a Circle
# Python program to find Area of a circle
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print("Area is %.6f" % findArea(5));
Python Program to Check Prime Number
Last Updated : 08 Oct, 2024
num = 11
# Negative numbers, 0 and 1 are not primes
if num > 1:
# Iterate from 2 to n // 2
for i in range(2, (num//2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Python Program for n-th Fibonacci number
# To find the n-th Fibonacci Number using formula
from math import sqrt
# import square-root method from math library
def nthFib(n):
res = (((1+sqrt(5))**n)-((1-sqrt(5)))**n)/(2**n*sqrt(5))
# compute the n-th fibonacci number
print(int(res),'is',str(n)+'th fibonacci number')
# format and print the number
# driver code
nthFib(12)
# This code is contributed by Kush Mehta
Python Program for Sum of squares of first n natural
numbers
# Python3 Program to
# find sum of square
# of first n natural
# numbers
# Return the sum of
# square of first n
# natural numbers
def squaresum(n):
# Iterate i from 1
# and n finding
# square of i and
# add to sum.
sm = 0
for i in range(1, n+1):
sm = sm + (i * i)
return sm
# Driven Program
n = 4
print(squaresum(n))
# This code is contributed by Nikita Tiwari.*/
Python Program for cube sum of first n natural
numbers
# Simple Python program to find sum of series
# with cubes of first n natural numbers
# Returns the sum of series
def sumOfSeries(n):
sum = 0
for i in range(1, n + 1):
sum += i * i*i
return sum
# Driver Function
n = 5
print(sumOfSeries(n))
# Code Contributed by Mohit Gupta_OMG <(0_o)>
Python Program to Swap Two Elements in a List
a = [23, 65, 19, 90]
# Swap elements at index 1 and index 3
a[1], a[3] = a[3], a[1]
print(a)
Python statistics