0% found this document useful (0 votes)
0 views4 pages

Extended Basic Python Programs

The document contains various Python programming examples, including printing 'Hello, World!', adding two numbers, taking user input, checking if a number is even or odd, and finding the largest of three numbers. It also covers loops, functions for checking prime numbers, list operations, and a simple calculator implementation. Each section provides code snippets demonstrating basic programming concepts and operations.

Uploaded by

amrutaoffical5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Extended Basic Python Programs

The document contains various Python programming examples, including printing 'Hello, World!', adding two numbers, taking user input, checking if a number is even or odd, and finding the largest of three numbers. It also covers loops, functions for checking prime numbers, list operations, and a simple calculator implementation. Each section provides code snippets demonstrating basic programming concepts and operations.

Uploaded by

amrutaoffical5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Hello World Program

print("Hello, World!")

Adding Two Numbers

a = 5
b = 3
sum = a + b
print("Sum:", sum)

Input from User

name = input("Enter your name: ")


print("Hello", name)

Check Even or Odd

num = int(input("Enter a number: "))


if num % 2 == 0:
print("Even")
else:
print("Odd")

Find Largest of Three Numbers

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Largest number is:", a)
elif b >= a and b >= c:
print("Largest number is:", b)
else:
print("Largest number is:", c)

While Loop: Print 1 to 5

i = 1
while i <= 5:
print(i)
i += 1

For Loop: Sum of First N Numbers

n = int(input("Enter a number: "))


sum = 0
for i in range(1, n+1):
sum += i
print("Sum is:", sum)

Function to Check Prime Number

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True

n = int(input("Enter a number: "))


if is_prime(n):
print("Prime")
else:
print("Not Prime")

List Operations

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


fruits.append("mango")
fruits.remove("banana")
print("Fruits List:", fruits)

Simple Calculator

def add(x, y):


return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


return x / y

print("Select operation: +, -, *, /")


choice = input("Enter choice: ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

if choice == '+':
print("Result:", add(a, b))
elif choice == '-':
print("Result:", subtract(a, b))
elif choice == '*':
print("Result:", multiply(a, b))
elif choice == '/':
if b != 0:
print("Result:", divide(a, b))
else:
print("Cannot divide by zero")
else:
print("Invalid input")

You might also like