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

Simple_Python_Codes (1)[1]

The document contains Python code examples that demonstrate various programming concepts, including class creation, method overloading, searching in arrays, calculating factorials, generating Fibonacci series, sorting, palindrome checking, and converting decimal to binary. It also includes error handling using try-except blocks for user input and simple mathematical operations. Each example is designed to show how Python can achieve similar functionality to Java programs.

Uploaded by

shreyruparel1290
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)
4 views

Simple_Python_Codes (1)[1]

The document contains Python code examples that demonstrate various programming concepts, including class creation, method overloading, searching in arrays, calculating factorials, generating Fibonacci series, sorting, palindrome checking, and converting decimal to binary. It also includes error handling using try-except blocks for user input and simple mathematical operations. Each example is designed to show how Python can achieve similar functionality to Java programs.

Uploaded by

shreyruparel1290
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/ 7

Simple Python Equivalents of Java Programs

1. Student Class with Constructor

class Student:

def __init__(self, name, roll, age):

self.name = name

self.roll = roll

self.age = age

def show(self):

print("Name:", self.name)

print("Roll Number:", self.roll)

print("Age:", self.age)

name = input("Enter name: ")

roll = input("Enter roll number: ")

age = input("Enter age: ")

s = Student(name, roll, age)

s.show()

2. Method Overloading (simulated using default arguments and


type checks)

def add(a, b):


return a + b

int1 = int(input("Enter first integer: "))


int2 = int(input("Enter second integer: "))
print("Sum:", add(int1, int2))

float1 = float(input("Enter first float: "))


float2 = float(input("Enter second float: "))
print("Sum:", add(float1, float2))

str1 = input("Enter first string: ")


str2 = input("Enter second string: ")
print("Concatenation:", add(str1, str2))

3. Class and Object

class Student:

def __init__(self, name, roll, age):

self.name = name

self.roll = roll

self.age = age

def show(self):

print("Name:", self.name)

print("Roll Number:", self.roll)

print("Age:", self.age)

name = input("Enter name: ")

roll = input("Enter roll number: ")

age = input("Enter age: ")

s = Student(name, roll, age)

s.show()
4. Search for an Element in an Array

arr = [10, 20, 30, 40, 50]

num = int(input("Enter number to search: "))

if num in arr:

print(num, "is found in the array.")

else:

print(num, "is not found in the array.")

5. Factorial of a Number

num = int(input("Enter a non-negative integer: "))


fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)

6. Fibonacci Series

n = int(input("How many numbers? "))

a=0

b=1

count = 0

while count < n:

print(a, end=" ")

c=a+b

a=b
b=c

count += 1

print()

7. Sort Elements in Ascending Order

n = int(input("How many elements? "))

arr = []

for i in range(n):

num = int(input("Enter element: "))

arr.append(num)

arr.sort()

print("Sorted array:", arr)

8. Check Palindrome String

s = input("Enter a string: ")


if s == s[::-1]:
print("Palindrome")
else:
print("Not a Palindrome")

9. Decimal to Binary

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

binary = ""

if num == 0:

binary = "0"
else:

while num > 0:

rem = num % 2

binary = str(rem) + binary

num = num // 2

print("Binary:", binary)

10. Single Digit to Word

num = int(input("Enter a single-digit number: "))

if num == 0:

print("Zero")

elif num == 1:

print("One")

elif num == 2:

print("Two")

elif num == 3:

print("Three")

elif num == 4:

print("Four")

elif num == 5:

print("Five")

elif num == 6:
print("Six")

elif num == 7:

print("Seven")

elif num == 8:

print("Eight")

elif num == 9:

print("Nine")

else:

print("Not a single-digit number")

11. Area of a Circle

radius = float(input("Enter radius: "))


area = 3.14159 * radius ** 2
print("Area:", area)

12. Check Odd or Even Using Exception

try:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
except ValueError:
print("Invalid input")
13. Add Two Numbers Using Try-Catch

try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
except ValueError:
print("Invalid input")

14. Cube of a Number Using Try-Catch

try:
num = int(input("Enter a number: "))
print("Cube:", num ** 3)
except ValueError:
print("Invalid input")

15. Right-Angle Star Pattern

rows = int(input("Enter number of rows: "))


for i in range(1, rows + 1):
print("* " * i)

You might also like