0% found this document useful (0 votes)
24 views5 pages

Python Assignment

The document contains a series of Python code snippets demonstrating various programming concepts such as basic arithmetic operations, number conversions, age classification, Fibonacci series generation, prime number checking, and more. Each code snippet includes user input and outputs results based on the operations performed. The examples cover a range of topics suitable for beginners learning Python programming.

Uploaded by

Yam Raj Bohara
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)
24 views5 pages

Python Assignment

The document contains a series of Python code snippets demonstrating various programming concepts such as basic arithmetic operations, number conversions, age classification, Fibonacci series generation, prime number checking, and more. Each code snippet includes user input and outputs results based on the operations performed. The examples cover a range of topics suitable for beginners learning Python programming.

Uploaded by

Yam Raj Bohara
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/ 5

6/30/25, 7:48 PM python-assignment

In [1]: #basic Calculator performing

num1 = float(input("Enter first number: "))


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

# Perform operation
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
result = "❌ Error: Cannot divide by zero"
else:
result = "❌ Invalid operator"

print("Result:", result)

Result: 4.0

In [2]: # Decimal to Binary Converter

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


binary = bin(decimal)[2:]
print("Binary equivalent:", binary)

Binary equivalent: 1010

In [3]: #users age

age = int(input("enter your age: "))


if age<18:
print("you are a minor: ")
elif age<60:
print("you are an adult: ")
else:
print("you are a senior: ")

you are an adult:

In [4]: # Swap two variables without using a third variable

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


b = int(input("Enter second number (b): "))
a = a + b
b = a - b
a = a - b

print("Vafter swapping:")

file:///C:/Users/alish/Downloads/Document/python-assignment(1).html 1/5
6/30/25, 7:48 PM python-assignment

print("a =", a)
print("b =", b)

Vafter swapping:
a = 2
b = 1

In [5]: # first 10 numbers of fibonacci series

a, b = 0, 1
print("Fibonacciseries:")
for _ in range(10):
print(a, end=' ')
a, b = b, a + b

Fibonacciseries:
0 1 1 2 3 5 8 13 21 34

In [6]: #check prime or not

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


if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not a prime number")
break
else:
print("Prime number")
else:
print("Not a prime number")

Prime number

In [7]: #3rd nbr is sum of the 1st and 2nnd nbr

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


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

if c == a + b:
print("Third number is the sum of first two.")
else:
print("Third number is not the sum.")

Third number is the sum of first two.

In [8]: #custom model created

def calculate_factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print(calculate_factorial(5))

120

file:///C:/Users/alish/Downloads/Document/python-assignment(1).html 2/5
6/30/25, 7:48 PM python-assignment

In [9]: #divisor is zero

a = float(input("Enter numerator: "))


b = float(input("Enter denominator: "))

if b != 0:
print("Result:", a / b)
else:
print("Error: Cannot divide by zero")

Error: Cannot divide by zero

In [10]: #return the maximum value

def find_max(numbers):
return max(numbers)

nums = [3, 5, 9, 1, 6]
print("Maximum value:", find_max(nums))

Maximum value: 9

In [11]: #prints greet

def greet(name, age=25):


print(f"Hello {name}, you are {age} years old")

greet("Alice")
greet("Bob",30)

Hello Alice, you are 25 years old


Hello Bob, you are 30 years old

In [12]: #count number of vowels in a string

text = input("Enter a string: ")


vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
print("Number of vowels:", count)

Number of vowels: 5

In [13]: #prints a multiplication table

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


for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

file:///C:/Users/alish/Downloads/Document/python-assignment(1).html 3/5
6/30/25, 7:48 PM python-assignment

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

In [17]: #print a right angled triangle of '*'

print("* ")
print("** ")
print("*** ")
print("**** ")
print("*****")

*
**
***
****
*****

In [18]: #print a pyramid of '*'

print(' * ')
print(' *** ')
print(' ***** ')
print(' ******* ')
print('*********')

*
***
*****
*******
*********

In [19]: ###SET--B

#pallindrome number

x = int(input("Enter number: "))


if str(x) == str(x)[::-1]:
print(True)
else:
print(False)

True

In [20]: #single number

nums = [4, 1, 2, 1, 2]
result = 0
for num in nums:

file:///C:/Users/alish/Downloads/Document/python-assignment(1).html 4/5
6/30/25, 7:48 PM python-assignment

result ^= num
print("Single Number:", result)

Single Number: 4

In [21]: #two sum

nums = [2, 7, 11, 15]


target = 9
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
print([i, j])

[0, 1]

In [22]: #happy number

def is_happy(n):
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = sum(int(ch) ** 2 for ch in str(n))
return n == 1

print(is_happy(19))

True

In [24]: #contains duplicate

nums = [1, 2, 3, 1]
if len(nums) == len(set(nums)):
print(True)
else:
print(False)

False

In [ ]:

file:///C:/Users/alish/Downloads/Document/python-assignment(1).html 5/5

You might also like