/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
PYTHON PROGRAMMING -1BLC205B MODULE-1 |
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
The way of the program
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
Variables, Expressions and Statements
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/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
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/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}.")
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/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!
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
Break and continue
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/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)
/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()
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
/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
/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))
/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))
/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
/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.
/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.
/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)
/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)
/skit.org.in
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
Sri Krishna Institute of Technology
THANK YOU