Aaemp
Wed 12 November 2025
# created : 20250316
#
Score: 0
Category: python-basics
# created : 20250316
#
Score: 0
Category: python-basics
def access_list_element(lst, index):
try:
print("Element:", lst[index])
except IndexError:
print("Error: Index out of range.")
except TypeError:
print("Error: Invalid index type.")
# Example usage
access_list_element([1, 2, 3], 1)
Element: 2
access_list_element([1, 2, 3], 5)
Error: Index out of range.
access_list_element([1, 2, 3], "a")
Error: Invalid …Category: python-basics
Read More# created : 20250125
# https://www.scientecheasy.com/2022/10/for-loop-in-python.html/
num = int(input('Enter a number: '))
even = 0
odd = 0
for x in range(num):
if x % 2 == 0:
even = even + x
else:
odd = odd + x
print('Sum of even numbers = ',even)
print('Sum of odd numbers = ',odd)
Enter a …Category: python-basics
Read More# created : 20250125
# https://www.scientecheasy.com/2022/10/while-loop-in-python.html/
num = int(input('Enter a number: '))
sum = 0
remainder = 0
while num != 0:
remainder = num % 10
sum = sum + remainder
num = int(num / 10)
print('Sum of all digits in number = ', sum)
Enter a number: 5
Sum of all digits in …Category: python-basics
Read More# created : 20250113
# https://www.scientecheasy.com/2022/10/operators-in-python.html/
# python operators
# arithmetic operators
x = 20
y = 30
sum = x + y
print("Sum: ", sum)
Sum: 50
# Type Conversion in Addition Calculation
x = 20 # an integer value.
y = 30.60 # a float value.
sum = x + y # Adding two numbers with + operator …Category: python-basics
Read More
def is_armstrong(num):
temp = num
sum_of_cubes = 0
while temp > 0:
digit = temp % 10
sum_of_cubes += digit ** 3
temp //= 10
if sum_of_cubes == num:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
is_armstrong(153)
153 is an Armstrong number.
is_armstrong(123)
123 is not an Armstrong number …Category: python-basics
Read More# created : 20250113
# https://www.scientecheasy.com/2022/10/assignment-operators-in-python.html/
# Assignment Operators in Python
# Simple Assignment
x = 20
y = 50
z = x + y + 2
print("Result = ", z)
Result = 72
# x += y It is equivalent to x = x + y.
x = 20
y = 5
z = 10
x += y
print("Result of (x …Category: python-basics
Read More# created : 20250113
# https://www.scientecheasy.com/2022/10/bitwise-operators-in-python.html/
# https://chatgpt.com/share/678524a9-cd24-8012-a475-2b7098cd8750
# Bitwise Operators in Python
# & Bitwise AND Binary
# | Bitwise OR Binary
# ^ Bitwise XOR (Exclusive OR) Binary
# ~ Bitwise NOT Unary
# << Bitwise Left Shift Binary
# >> Bitwise Right Shift Binary
# Bitwise AND operator (&)
A = 2
B = 6
result = A …Category: python-basics
Read More# created : 20250316
# https://www.scientecheasy.com/2022/11/break-statement-in-python.html/
# Example Program based on Break
# Use of break statement inside the for loop.
for x in range(1, 11):
if x == 5:
break # Breaking a loop.
print("X = ",x)
X = 1
X = 2
X = 3
X = 4
# Use of break …Category: python-basics
Read More# created : 20250321
# https://www.scientecheasy.com/2022/12/calling-a-function-in-python.html/
# Execution Style of Calling a Function in Python
# Function definition.
def funct_name(): # function header.
print('This function does not contain any parameter.')
# Main program execution started from here.
funct_name() # function calling.
This function does not contain any parameter.
# Function definition …Category: python-basics
Read More