1 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

PYTHON PROGRAMMING -1BLC205B

MODULE-1

2 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

The way of the program

3 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

4 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

5 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

6 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

7 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

8 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

9 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

10 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

11 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

12 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

13 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

14 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

15 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

16 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

17 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

18 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

Variables, Expressions and Statements

19 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

20 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

21 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

22 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

23 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

24 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

25 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

26 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

27 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

28 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

29 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

x = 5 + 3 * 2 ** 2 / (4 - 2)

x = 8 / 4 + 5 * (3 + 1) - 7 % 4

30 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

31 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

32 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

33 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

34 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

def check_even_odd(number):

if number % 2 == 0:

return "Even"

else:

return "Odd"

num = 5

result = check_even_odd(num)

print(f"The number {num} is {result}.")

35 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

36 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

37 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

38 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

39 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

# Initialize a list of numbers

numbers = [10, 20, 30, 40]

# Initialize a variable to store the total

total = 0

# Iterate through each number in the list

for n in numbers:

total += num

# Equivalent to total = total + n

print(f"The total sum is: {total}")

# Output: The total sum is: 100

# Initialize a counter

n = 5

# Loop while n is greater than 0

while n > 0:

print(n) n -= 1 # Equivalent to n = n – 1

print("Blastoff!")

# Output:

# 5

# 4

# 3

# 2

# 1

# Blastoff!

40 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

41 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

42 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

43 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

44 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

45 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

46 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

47 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

48 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

49 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

50 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

51 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

52 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

53 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

54 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

55 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

56 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

Break and continue

57 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

58 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

59 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

60 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

61 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

62 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

63 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

64 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

x = [1, 2]

y = [4, 5]

for i in x:

for j in y:

print(i, j)

65 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

# Running outer loop from 2 to 3

for i in range(2, 4):

# Printing inside the outer loop

# Running inner loop from 1 to 10

for j in range(1, 11):

# Printing inside the inner loop

print(i, "*", j, "=", i*j)

# Printing inside the outer loop

print()

66 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

67 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

# Define a function named 'add' that takes two parameters: 'a' and 'b‘

def add(a, b): # 'a' and 'b' are parameters

result = a + b

return result # Returns the value stored in 'result'

Functions with arguments and return values in python

def calc(n):

return [n * n, n * n * n]

print(calc(3))

Write your own function SI=PTR/100

68 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

Model Qp Answers

1.a.Explain the concept of type conversion in Python. Differentiate between implicit and explicit conversion with examples.

Answer: Type conversion in Python is the process of changing a value's data type from one to another to ensure compatibility during operations. There are two main types: implicit (automatic) and explicit (manual).

Implicit type conversion example:

x = 10 # Integer

y = 10.6 # Float

z = x + y

print("x:", type(x))

print("y:", type(y))

print("z =", z)

print("z :", type(z))

69 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

Explicit type conversion example:

s = "100" # String

a = int(s)

print(a)

print(type(a))

70 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

1.b. Develop a Python program with a while loop to display the Fibonacci sequence up to n terms entered by the user

n=int(“enter length of Fibonacci series”)

n1=0

n2=1

count=2 # if(n<0): print(“enter only positive values”) elif(n==1): print(n1)

else:

print(n1) print(n2)

While(count<n):

n3=n1+n2

print(n3)

count=count+1

n1=n2

n2=n3

71 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

1.c. Differentiate between a syntax error and a runtime error with examples.

Syntax errors are errors that occur due to incorrect format of a Python statement. They occur while the statement is being translated into machine language and before being executed. They are the most common type of errors which are easily traceable. These errors can be corrected by the user as the reason for the error and an appropriate message about what is wrong in the program is displayed.

For example, print "Hello World" has syntax error as it violates the language protocol by not giving parentheses with the print() function.

So, the corrected statement should be: print("Hello World").

Runtime errors are errors that occur while a program is executing, causing it to crash or behave unexpectedly. These errors are due to invalid operations or conditions that the program encounters during execution, which were not detected during compilation or before the program ran.

For example, 10 * (1/0) will generate runtime error as division of any number by 0 is undefined.

72 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

2.a. Describe the Collatz 3n + 1 sequence and explain how iteration and conditional statements are used in its implementation.

73 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

2.b. Develop a program that prints all numbers from 1 to 100 that are divisible by 3 or 5 but not both. Use continue or break statements wherever suitable.

for num in range(1, 101): # Check if the number is divisible by both 3 and 5 (i.e., by 15).

if num % 3 == 0 and num % 5 == 0:

continue # Skip this number and continue to the next iteration of the loop

# Check if the number is divisible by 3 or 5.

if num % 3 == 0 or num % 5 == 0:

print(num)

74 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

2.c. What is meant by function composition? Illustrate with an example

Function composition is the act of combining two or more functions to create a new function, where the output of one function becomes the input of the next function in the sequence

Example:

def add_five(x):

return x + 5

def multiply_by_two(y):

return y * 2

# Direct composition: multiply_by_two is applied to the output of add_five

result = multiply_by_two(add_five(10))

print(result)

75 of 75

/skit.org.in

(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)

Sri Krishna Institute of Technology

THANK YOU