0% found this document useful (0 votes)
23 views

Python Theory 3

This document contains an assignment question from the Department of Computer Application at Kalyani Govt. Engineering College. The assignment asks about break and continue statements in loops, nested loops with examples, user defined functions and how to pass parameters to functions. It also asks about the range() function in Python with examples and how to print a table of numbers taking input from the user. Finally, it asks about variables, why we use them, and what implicit declaration of variables means.

Uploaded by

Saikar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Theory 3

This document contains an assignment question from the Department of Computer Application at Kalyani Govt. Engineering College. The assignment asks about break and continue statements in loops, nested loops with examples, user defined functions and how to pass parameters to functions. It also asks about the range() function in Python with examples and how to print a table of numbers taking input from the user. Finally, it asks about variables, why we use them, and what implicit declaration of variables means.

Uploaded by

Saikar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Kalyani Govt.

Engineering College
Department of Computer Application

Programming through python (Theory)

Paper code - MCAN 101 Year- 2023-25

Assignment-3

Semester-1

Saikat Mazumder

Roll-25

1.a) Explain break and continue statements. When do we use


those?

b) What is a nested loop? Explain with examples.

c) What is a user defined function? How can we pass parameters


in user defined functions?

Ans -
a) Break and Continue :
Break and continue are control statements that are used in
loop like for, while to alter the flow of execution.
Break Statement:
Usage : The break statement is used to terminate the loop it
is in.When a break statement is encountered inside a loop,
the loop iteration stops there, and control returns from the
loop immediately to the first statement after the loop.
Basically, break statements are used in situations when we
are not sure about the actual number of iterations for the
loop, or we want to terminate the loop based on some
condition.
When to use:
It is used when a certain condition is met, and you want to
exit the loop immediately, regardless of whether the loop's
iterations are complete.
Example:
Here you can see the loop is terminated after printing 0 to 5
because when i will be 6 the loop will encounter a break
statement and thereafter the loop is terminated.

Continue Statement: Continue Statement is used to skip


the current iteration. We can use continue statement inside
any types of loops such as for, while, and do-while loop.
When to use: Basically continue statements are used in the
situations when we want to continue the loop but do not
want the remaining statement after the continue statement.

Example:

Here we can use from 0 to 10 which is divided by 2 is


skipped; only the numbers which are not divided by 2 are
printed.

b) Nested Loop: A nested loop is a loop inside another loop.


This means one loop is placed within another loop's block.
Each time the outer loop runs, the inner loop will complete
its full iteration. This behavior continues until the outer
loop finishes all its iterations.

Example of Nested for loop:

The outer loop runs 3 times because of range(3).For each


iteration of the outer loop, the inner loop runs completely,
iterating through range(2), printing the combined indices of
both loops.

Example of Nested While loop:
C) User Defined Function: A user-defined function is a
function created by the user to perform a specific task or set
of tasks within a program. These functions are written by
the programmer to modularize the code, improve
readability, and promote reusability.

Passing Parameters in user defined function:


1) Positional Parameters: These are parameters that are
passed based on their positions or order. The function
definition specifies the parameters it expects, and
when calling the function, arguments are passed in the
same order.
Example:
def greet(name):
print(f"Hello, {name}!")

greet("Saikat") # Passing "Saikat" as an argument to


the greet function.

2) Keyword Parameters: Parameters are passed with


their corresponding parameter names. This method
allows passing arguments to functions irrespective of
their positions.
Example:
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")

greet(age=30, name="Saikat") # Passing arguments


with parameter names

3) Default Parameters: Parameters in a function can


have default values. If an argument is not provided
when calling the function, the default value is used.
Example:
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # No argument provided, uses default value
"Guest"
greet("Alice") # Passing an argument "Alice"

4) Variable Number of Parameters: Python allows


defining functions that accept a variable number of
arguments using *args (for non-keyworded variable
length arguments) and **kwargs (for keyworded
variable length arguments).
Example:
def print_values(*args):
for arg in args:
print(arg)

print_values(1, 2, 3, 4) # Passing multiple arguments

def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

print_info(name="Alice", age=30, country="USA") #


Passing keyword arguments
2.a) Illustrate the use of range() in python along with an
example.

b) Write a python program to print a table of numbers, taking


numbers from the user.

c) What is variable? Why do we use it? What do you mean by


implicit declaration of variables?

Ans-
a) In Python, the range() function generates a sequence of
numbers. It's commonly used in loops to iterate a specific
number of times.

Basic Usage:
The range() function can be used in three different ways:

1) range(stop): Generates numbers from 0 up to (but not


including) the stop value.
Example:
for i in range(5):
print(i)
Output: 0 1 2 3 4

2) range(start, stop): Generates numbers from start up to


(but not including) the stop value.
Example:
for i in range(2, 8):
print(i)
Output:
2
3
4
5
6
7

3) range(start, stop, step): Generates numbers from start to


stop (not including stop) in steps of step.
Example:
#Using range() with start, end, and step
for i in range(1, 10, 2):
print(i)
# Generates numbers from 1 to 9 with a step of 2
Output:
1
3
5
7
9

b) print a table of numbers:


c) Variable: A variable in programming is a symbolic name
or identifier that represents or holds a value or data. It's like
a container that stores information that can change during
the execution of a program. Variables are fundamental in
programming as they allow us to store, manipulate, and
retrieve data as needed.

● We use variables for various reasons:

1) Storage: They store data that can be used later in the


program.
2) Manipulation: Variables allow us to perform operations on
data, such as arithmetic, string manipulation, and more.
3) Flexibility: They provide flexibility by allowing the same
variable name to hold different values at different times.
4) Readability: Using variables with descriptive names can
make code more understandable and easier to maintain.

Implicit Variable: Implicit declaration of variables refers to the


process where a variable is created without explicitly specifying
its data type. In some programming languages like JavaScript
and Python, you can create variables without declaring their type
beforehand. For example, in Python:
x = 5 # Here, 'x' is a variable implicitly declared as an
integer

You might also like