Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

Input and print command

print("Admission form-21k")
print(" ------------------------")
input("Enter my age:")

2. Adding two number:-


num1 = 40
num2 = 30

# Adding two nos


sum = num1 + num2

#printing values
#print("sum of", num1, "and", num2 , "is", sum)
print(sum)

number1 = input("First number: ")


number2 = input("Second number: ")
sum = float(number1) + float(number2)
print(sum)

3. Simple 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(10, 6, 5)

4. Number guessing game

import random

num = random.randint(1, 10)


guess = None

while guess != num:


guess = input("guess a number between 1 and 10: ")
guess = int(guess)

if guess == num:
print("congratulations! you won!")
break
else:
print("nope, sorry. try again!")
5. To create a calculator

def add(num1, num2):


#to return the output
return num1 + num2

# Function to subtract two numbers


def subtract(num1, num2):
return num1 - num2

# Function to multiply two numbers


def multiply(num1, num2):
return num1 * num2

# Function to divide two numbers


def divide(num1, num2):
return num1 / num2

print("Please select operation -\n" \


"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")

# Take input from the user


select = int(input("Select operations from 1, 2, 3, 4 :"))
#int converts the number or string in to a integer
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))

if select == 1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))
#elif is else if
elif select == 2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))

elif select == 3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))

elif select == 4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Invalid input")

6. Number guessing game

import random

user_action = input("Enter a choice (rock, paper, scissors): ")


possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")

if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")

7 Roll a dice game

import random

def roll_dice():
#Rolls a single six-sided die and returns the result."""
return random.randint(1, 6)

def play_dice_game():
#Plays a simple dice rolling game."""
player_roll = roll_dice()
computer_roll = roll_dice()

print(f"You rolled a {player_roll}")


print(f"The computer rolled a {computer_roll}")

if player_roll > computer_roll:


print("You win!")
elif player_roll < computer_roll:
print("The computer wins!")
else:
print("It's a tie!")

if __name__ == "__main__":

play_dice_game()

8 Using For loop

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
# continue
print(x)

# Iterating over a String


print("String Iteration")

s = "John"
for i in s:
print(i)

8 Check if number is prime number or not

def is_prime(number):
# Check if the number is less than 2
if number <= 1:
return False

# Check for factors from 2 to the square root of the number


return all(number % i != 0 for i in range(2, int(number ** 0.5) + 1))

# Test the function


number = int(input("Enter a number: "))
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")

9. To check whether number is prime or not

def is_even_or_odd(number):
if number % 2 == 0:
return "even"
else:
return "odd"

# Test the function


number = int(input("Enter a number: "))
result = is_even_or_odd(number)
print(f"{number} is {result}.")

10. To print multiples of a number

def find_multiples(number, range_start, range_end):


multiples = []
for i in range(range_start, range_end + 1):
if i % number == 0:
multiples.append(i)
return multiples

11. Temperature convertor using Unary operator

def celsius_to_fahrenheit(celsius):
return celsius * 9/5 + 32

def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9

temperature_c = 25
temperature_f= celsius_to_fahrenheit(temperature_c)

print(f"{temperature_c} C is equal to {temperature_f}°F")

# Using negative unary operator


temperature_c = -temperature_c
temperature_f = celsius_to_fahrenheit(temperature_c)

print(f"{temperature_c} C is equal to {temperature_f}°F")

12. Creating a counter using Unary operator

counter = 0
print(f"Initial counter value: {counter}")

# Incrementing using unary plus


counter = +counter + 1
print(f"Counter after incrementing: {counter}")

# Decrementing using unary minus


counter = -counter - 1
print(f"Counter after decrementing: {counter}")

13. Logical operator.

# Check if both conditions are true


a=5
b = 10

if a > 0 and b > 5:


print("Both conditions are True")
else:
print("At least one condition is False")

14. Combination of AND OR operators

# Combine logical operators


a=5
b = 10
c = 15

if a < b and b < c or c < a:


print("The combined condition is True")
else:
print("The combined condition is False")

You might also like