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

Python Assignment 1 Answers

This document provides an introduction to Python programming concepts, including methods for importing modules, handling arithmetic errors, and flow control statements. It covers practical examples such as adding numbers, checking even or odd, and generating Fibonacci sequences. The document also discusses built-in functions, string operations, and variable scope, with a submission deadline of April 28, 2025.
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)
13 views4 pages

Python Assignment 1 Answers

This document provides an introduction to Python programming concepts, including methods for importing modules, handling arithmetic errors, and flow control statements. It covers practical examples such as adding numbers, checking even or odd, and generating Fibonacci sequences. The document also discusses built-in functions, string operations, and variable scope, with a submission deadline of April 28, 2025.
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

Introduction to Python Programming Assignment - 1 Answers

1. Methods of Importing Modules

- import module_name: imports the entire module.

- from module_name import function_name: imports specific functions.

- import module_name as alias: imports with an alias name.

Best Method: Use from ... import for specific functions or as for readability.

2. Arithmetic Expression Error

'hello world' + 100 + 'how are you'

Error: Cannot add string and integer. Use type conversion.

Fix:

print('hello world' + str(100) + 'how are you')

3. Flow Control Statements

Flow control statements: if, elif, else

Flowchart: Start -> Condition -> True/False -> Execute block accordingly

4. Add Two Numbers

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

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

print("Sum =", a + b)

5. List Without Odd Numbers

nums = list(map(int, input("Enter numbers: ").split()))

even_nums = [x for x in nums if x % 2 == 0]

print("Even numbers:", even_nums)


6. Check Even or Odd

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

if n % 2 == 0:

print("Even")

else:

print("Odd")

7. Built-in Functions

print("Hello")

print(len("Hello"))

name = input("Enter your name: ")

8. Print Even Numbers Using Range

for i in range(2, 21, 2):

print(i)

9. Break and Continue

# Break example

for i in range(10):

if i == 5:

break

print(i)

# Continue example

for i in range(10):

if i == 5:
continue

print(i)

10. Factorial and Binomial Coefficient

def factorial(n):

result = 1

for i in range(1, n+1):

result *= i

return result

def binomial_coeff(n, r):

return factorial(n) // (factorial(r) * factorial(n - r))

11. Looping Control Statements

for i in range(5):

if i == 3:

break

print(i)

12. Fibonacci Sequence

n = int(input("Enter N: "))

a, b = 0, 1

for _ in range(n):

print(a, end=' ')

a, b = b, a + b

13. String Concatenation and Replication


s1 = "Hello"

s2 = "World"

print(s1 + " " + s2) # Concatenation

print(s1 * 3) # Replication

14. Return Values and Statements

def add(a, b):

return a + b

result = add(3, 4)

print(result)

15. Local and Global Scope

x = 10 # Global variable

def func():

x = 5 # Local variable

print("Local x:", x)

func()

print("Global x:", x)

Note: Submission Deadline - 28-04-2025

You might also like