Python Examples
Python Examples
1. import random
2. n = random.randint(0,50)
3. print(n)
Output:
40
Example - 2:
1. import random
2. n = random.randint(100, 200)
3. print(n)
Output:
143
Example -
1. import random
2. rand_list = []
3. for i in range(0,10):
4. n = random.randint(1,50)
5. rand_list.append(n)
6. print(rand_list)
Output:
[10, 49, 16, 31, 45, 21, 19, 32, 30, 16]
Area of a triangle = (s*(s-a)*(s-b)*(s-c))-1/2
Here is the semi-perimeter and a, b and c are three sides of the triangle. Let's understand the
following example.
Python3
if a >= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(maximum(a, b))
Output
4
Python Program to Find Area of a Circle
Python3
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print("Area is %.6f" % findArea(5));
Output
Area is 78.550000
==============
Python Example Program No.2
This program is created using the if-else statement of Python. Let's have a
look:
print("Guess a Number: ")
num = input()
num = int(num)
if num>10 and num<20:
print("\nCorrect Guess!")
else:
print("\nIncorrect Guess!")
This is program number four, and it checks whether the user is a robot or
not in the most basic way possible.
print("Are you a Robot ? ")
chk = input()
if chk == "yes":
print("\nSorry!\nRobots aren't Allowed here!")
else:
print("\nWelcome to Carey")
Python Program No.5
This is the last demo program of this Python exercise (examples) series.
print("Create a Password: ")
cp = input()
if cp == ep:
sum = numOne + numTwo
print("\nResult = ", sum)
else:
print("\nWrong Password!")
import random
def get_user_choice():
user_choice = input("Enter your choice (rock, paper, or scissors): ").lower()
while user_choice not in ["rock", "paper", "scissors"]:
print("Invalid choice. Please try again.")
user_choice = input("Enter your choice (rock, paper, or scissors): ").lower()
return user_choice
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
return random.choice(choices)
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"You chose {user_choice}.")
print(f"Computer chose {computer_choice}.")
result = determine_winner(user_choice, computer_choice)
print(result)
if __name__ == "__main__":
play_game()