Chapter 1 An Introduction to Python

  What are the advantages of Python?

Readability and Simplicity: Python code is easy to read and write, making it accessible for beginners and reducing the cost of program maintenance.

Versatility: Python is a versatile language that can be used for web development, data science, artificial intelligence, machine learning, automation, and more.

Large Standard Library: Python comes with a vast standard library that includes modules and packages for a wide range of tasks, eliminating the need for additional downloads.

Object-Oriented Language: Python supports both procedural and object-oriented programming, promoting code reuse and modular design.

 Python is a scripting language. Comment.

Python's concise syntax, ease of use, and the ability to interact with other languages and systems make it well-suited for scripting tasks.


- What is dry run in Python?
A dry run in Python refers to the process of mentally or manually simulating the execution of code without actually running it on a computer. It helps developers understand the flow of the program, identify potential errors, and verify the logic before executing the code.


- Write a short note on datatypes in Python.

Python supports various data types, including:

Numeric Types: Integers, Floats, Complex Numbers

Sequence Types: Strings, Lists, Tuples

Mapping Type: Dictionary

Boolean Type: bool


What is the output of following code :

 def f(X) :

 def  f1(a, b) :

 print ("hello")

 if (b==0) :

   print ("NO")

   return

 return f(a, b)

 return f1

 @ f

 def f(a, b) :

 return a%b

 f(4, 0)

 X = 5

 def  f1( ):

 global X

 X = 4

 f2(a, b) :

 global X

 return a+b+X

f1()

 total = f2(1, 2)

 print (total)


- Define identifiers.

  • Write a Python program to check if a given number is Armstrong.
def is_armstrong_number(num):
    # Convert the number to a string to find its length
    num_str = str(num)
    num_digits = len(num_str)

    # Calculate the sum of the nth powers of its digits
    armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)

    # Check if the sum is equal to the original number
    return armstrong_sum == num

# Example usage:
number = int(input("Enter a number to check for Armstrong property: "))
if is_armstrong_number(number):
    print(f"{number} is an Armstrong number.")
else:
    print(f"{number} is not an Armstrong number.")


  • Write a Python program to check for Zero Division Error Exception.

def divide_numbers(dividend, divisor):

    try:

        result = dividend / divisor

        print(f"Result of {dividend} / {divisor} is: {result}")

    except ZeroDivisionError:

        print("Error: Division by zero is not allowed.")


# Example usage:

try:

    dividend = float(input("Enter the dividend: "))

    divisor = float(input("Enter the divisor: "))

    divide_numbers(dividend, divisor)

except ValueError:

    print("Error: Please enter valid numeric values.")