0% found this document useful (0 votes)
5 views

Python Examples

The document provides examples of using random numbers and the randint() method in Python. It also shows how to generate a list of random numbers using a for loop. Further, it includes examples of calculating the area of a triangle and circle through Python programs. The document ends with an example of a rock-paper-scissors game in Python using functions for getting user and computer choice and determining a winner.

Uploaded by

abrahamjad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Examples

The document provides examples of using random numbers and the randint() method in Python. It also shows how to generate a list of random numbers using a for loop. Further, it includes examples of calculating the area of a triangle and circle through Python programs. The document ends with an example of a rock-paper-scissors game in Python using functions for getting user and computer choice and determining a winner.

Uploaded by

abrahamjad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Example - 1:

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

Using for loop


The randint() method can be used with for loop to generated a list of random
numbers. To do so, we need to create an empty list, and then append the random
numbers generated to the empty list one by one. Let's understand the following
example.

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.

See this example:

1. # Three sides of the triangle is a, b and c:


2. a = float(input('Enter first side: '))
3. b = float(input('Enter second side: '))
4. c = float(input('Enter third side: '))
5.
6. # calculate the semi-perimeter
7. s = (a + b + c) / 2
8.
9. # calculate the area
10. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
11. print('The area of the triangle is %0.2f' %area)
12. Output:
13. Enter first side: 5
14. Enter second side: 6
15. Enter third side: 7
16. The area of the triangle is 14.70

Python3

# Python program to find the


# maximum of two numbers

def maximum(a, b):

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

# 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));

Output
Area is 78.550000
==============
Python Example Program No.2

This is the second demo program in the Python programming examples


series.
print("Enter Your Name: ")
name = input()

print("\nHello", name, "\nWelcome to Carey")

Python Program No.3

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!")

Python Program No.4

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()

print("\nEnter Two Numbers to Add: ")


numOne = int(input())
numTwo = int(input())

print("\nEnter Password to Display the Result: ")


ep = 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 determine_winner(user_choice, computer_choice):


if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!"
else:
return "Computer wins!"

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()

You might also like