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

Full Python Basic Programs

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)
10 views4 pages

Full Python Basic Programs

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

Python Basic Programs

Basic Programs
Hello World:
print("Hello, World!")

Simple Calculator:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

if operator == '+':
print("Result:", num1 + num2)
elif operator == '-':
print("Result:", num1 - num2)
elif operator == '*':
print("Result:", num1 * num2)
elif operator == '/':
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Cannot divide by zero.")
else:
print("Invalid operator.")

Area of a Circle:
import math
radius = float(input("Enter radius: "))
area = math.pi * radius ** 2
print("Area of the circle:", area)

Simple Interest:
P = float(input("Enter principal: "))
R = float(input("Enter rate: "))
T = float(input("Enter time: "))
SI = (P * R * T) / 100
print("Simple Interest:", SI)

Swap Two Numbers:


# Using third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("After swap using third variable: a =", a, "b =", b)

# Without using third variable


a, b = b, a
print("After swap without third variable: a =", a, "b =", b)

Conditional and Looping Programs


Largest of Three Numbers:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if a >= b and a >= c:


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

Even or Odd:
num = int(input("Enter a number: "))
print("Even" if num % 2 == 0 else "Odd")

Leap Year:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")

Factorial of a Number:
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)

Fibonacci Series:
n = int(input("Enter number of terms: "))
a, b = 0, 1
print("Fibonacci Series:")
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

Prime Number:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Sum of Natural Numbers:


n = int(input("Enter a number: "))
print("Sum:", n * (n + 1) // 2)

Pattern Program - Right Angled Triangle:


rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
print('*' * i)

String and Data Structure Programs


Palindrome String:
text = input("Enter a string: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not a palindrome")

Count Vowels in a String:


text = input("Enter a string: ").lower()
vowels = 'aeiou'
count = sum(1 for char in text if char in vowels)
print("Vowel count:", count)

Reverse a Number:
num = int(input("Enter a number: "))
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed number:", rev)

Sum of Digits:
num = int(input("Enter a number: "))
total = 0
while num > 0:
total += num % 10
num //= 10
print("Sum of digits:", total)

List, Tuple, and Dictionary Operations


List Operations:
numbers = list(map(int, input("Enter list elements separated by space: ").split()))
print("Sum:", sum(numbers))
print("Average:", sum(numbers)/len(numbers))
print("Maximum:", max(numbers))
print("Minimum:", min(numbers))

Tuple Operations:
names = tuple(input("Enter names separated by space: ").split())
search_name = input("Enter name to search: ")
if search_name in names:
print(f"{search_name} found in the tuple.")
else:
print(f"{search_name} not found.")

Dictionary Operations:
students = {}
n = int(input("Enter number of students: "))

for _ in range(n):
roll = input("Enter roll number: ")
name = input("Enter name: ")
marks = float(input("Enter marks: "))
students[roll] = {"name": name, "marks": marks}

cutoff = float(input("Enter cutoff percentage: "))


print(f"Students scoring above {cutoff}%:")
for roll, info in students.items():
if info['marks'] > cutoff:
print(f"Roll No: {roll}, Name: {info['name']}, Marks: {info['marks']}")

You might also like