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

Assignment - 1

The document is a Python assignment submitted by Suman Mistri, covering various topics including the definition of Python, its features, differences between local and global variables, and types of functions. It also explains operators, expressions, control flow statements like break and continue, and includes example programs for checking prime numbers and palindromes. Additionally, it outlines the syntax for loops and the use of statements such as pass and break.

Uploaded by

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

Assignment - 1

The document is a Python assignment submitted by Suman Mistri, covering various topics including the definition of Python, its features, differences between local and global variables, and types of functions. It also explains operators, expressions, control flow statements like break and continue, and includes example programs for checking prime numbers and palindromes. Additionally, it outlines the syntax for loops and the use of statements such as pass and break.

Uploaded by

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

BPC

PYTHON
ASSIGNMENT - I

Submited by
Suman Mistri
DCST
ROLL - 47
Reg. no - D232406827
ASSIGNMENT - I
(PYTHON)

1. Define Phython .
Ans- Python is a high-level, interpreted programming
language created by Guido van Rossum in 1991. Known for
its simplicity and readability, Python supports multiple
programming paradigms, including object-oriented,
procedural, and functional programming. It features
dynamic typing and automatic memory management.
Python's extensive standard library and vibrant
community make it suitable for diverse applications such
as web development, data science, machine learning,
automation, and more. Its cross-platform nature allows it
to run on different operating systems, making Python
highly versatile and one of the most popular programming
languages worldwide.

2. Explain the features Python Progaming .


Ans-
Python is a versatile, high-level programming language
known for its simplicity and readability. It is easy to
learn, with a clear syntax resembling English. Python is
interpreted, meaning code is executed line by line,
which aids in quick testing and debugging. It supports
multiple programming paradigms, including object-
oriented, functional, and procedural programming.
(PYTHON)

One of Python’s key strengths is its dynamic typing and


automatic memory management, which allow for
flexibility and easier coding. Its extensive standard
library and vast ecosystem of third-party libraries, such
as NumPy for data analysis or Django for web
development, make it suitable for diverse applications.

Python is cross-platform and can run on different


operating systems. Its community support is large and
active, providing ample resources for learners and
professionals alike. These features, combined with
Python's scalability and automation capabilities, have
made it one of the most popular programming
languages in fields like AI, data science, and web
development.

3. Difference between the local veriable and global


veriable .
Ans-

Topic Local veriable Global veriable

global variables are


Local variables are accessible throughout the
Scope
confined to functions entire program.

while global variables


Local variables exist during
Lifetime persist as long as the
the function call
program runs.
(PYTHON)

Topic Local veriable Global veriable

Local variables are


global variables are
Declaration declared inside
declared outside.
functions

4. Difference between Intermeadiate mode and Script


mode .
Ans-
Topic Interactive Mode Script Mode

Interactive mode
executes code line by Script mode runs the
Execution
line, giving instant entire script at once.
feedback.

Interactive mode is
Script mode is better for
Use suited for testing and
writing full programs.
small tasks.

Interactive mode keeps Script mode terminates


Persistence the session live for once the script finishes
further inputs. running.

Errors in interactive Errors in script mode


mode are caught are only caught when
Error Handling
immediately after each the script is run as a
line. whole.
(PYTHON)

5. What is function. Mention the type of functions and


uses.

Ans- A function in programming is a block of reusable


code that performs a specific task. It allows you to group
together a sequence of instructions, making your code
more organized, readable, and maintainable. Functions can
take inputs, called parameters or arguments, and can
return a value or result after processing.
Types of function :

1. Built-in functions are used for common tasks.


2. User-defined functions let you create custom logic.
3. Lambda functions are useful for short, simple
operations.
4. Recursive functions solve problems by breaking them
into smaller, similar problems.
5. Higher-order functions abstract operations by
accepting other functions.
6. Generator functions manage memory efficiently by
generating values one at a time.
7. Pure functions ensure predictable outputs and ease
testing.
8. Impure functions are necessary for real-world tasks
involving side effects.
(PYTHON)

6. Explaiin the differnt types of operator with example


of each .
Ans-
1. Arithmetic Operators
Definition: These operators perform mathematical
operations on numerical values.
Operators: +, -, *, /, %, **, //
2. Comparison (Relational) Operators
Definition: These operators compare two values and
return a boolean result (True or False).
Operators: ==, !=, >, <, >=, <=
3. Assignment Operators
Definition: These operators are used to assign values to
variables.
Operators: =, +=, -=, *=, /=, %=, **=, //=
4. Logical Operators
Definition: These operators are used to perform logical
operations and return a boolean value.
Operators: and, or, not

5. Bitwise Operators
Definition: These operators perform operations on the
binary (bit) representation of integers.
Operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left
shift), >> (right shift)
(PYTHON)

6. Identity Operators
Definition: These operators check whether two
variables refer to the same object in memory.
Operators: is, is not
7. Membership Operators
Definition: These operators test if a value is a member
(element) of a sequence, such as strings, lists, or tuples.
Operators: in, not in

8. Ternary Operator (Conditional Expressions)


Definition: This operator evaluates a condition and
returns one of two values based on whether the
condition is True or False.
Operator: value_if_true if condition else value_if_false

7. What is Expression. Explain Expression.


Ans- In programming, an expression is a combination of
variables, constants, operators, and functions that
evaluates to a value. Expressions are fundamental
components of code, as they are used to compute values,
make decisions, and control the flow of a program.

An expression in programming is a combination of


variables, constants, operators, and functions that
produces a value. Expressions are essential for performing
calculations, making comparisons, and implementing logic
in code.
(PYTHON)

Components:
Operands: The values or variables involved, like 5 and 3
in 5 + 3.
Operators: Symbols that define operations, such as +
for addition or == for comparison.
Functions: Functions used within expressions, like
max(10, 20), which evaluates to the maximum of the
two numbers.
Types:
1. Arithmetic Expressions: Perform mathematical
operations, e.g., 7 * (2 + 3) evaluates to 35.
2. Relational Expressions: Compare values, e.g., 10 > 5
results in True.
3. Logical Expressions: Combine boolean values, e.g.,
(True and False) evaluates to False.
4. Conditional Expressions: Short for if-else logic, e.g., x if
x > 0 else -x.

8. Explain break and continue statement with the help


of for loop example.
Ans-
break Statement :
The break statement is used to exit the loop
prematurely when a specific condition is met. It terminates
the loop entirely, and the control flow moves to the
statement immediately after the loop.
(PYTHON)

Example:
numbers = [5, 8, 12, 15, 20]

for num in numbers:


if num > 10:
print(f"First number greater than 10 is: {num}")
break # Exit the loop once the condition is met

continue Statement :
The continue statement skips the rest of the code
inside the loop for the current iteration and moves to the
next iteration of the loop.

Example:
numbers = [5, 8, 12, 15, 20]

for num in numbers:


if num == 15:
continue # Skip the rest of the loop for this
iteration
print(num)
(PYTHON)

9. Write a progam prime or not prime.

def is_prime(number):
"""Check if a number is a prime number."""
if number <= 1:
return False # Numbers less than or equal to 1 are not prime
if number <= 3:
return True # 2 and 3 are prime numbers
if number % 2 == 0 or number % 3 == 0:
return False # Numbers divisible by 2 or 3 are not prime

# Check for factors from 5 to the square root of the number


i=5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False
i += 6

return True

# Input from the user


try:
num = int(input("Enter a number to check if it is prime: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
except ValueError:
print("Please enter a valid integer.")
(PYTHON)

10. What is Boolean value.


Ans- Boolean value is a data type that can hold one of two
possible values: True or False. Named after the
mathematician George Boole, Boolean values are
fundamental in logic and computer science, particularly in
decision-making and control flow.

11. Explain conditional if.


Ans- In Python, the if statement is used for conditional
execution of code. It allows a program to make decisions
based on whether a condition evaluates to True or False.

The simplest form of an if statement evaluates a


condition and executes a block of code only if the
condition is True.

Syntax:
if condition:
# Code to execute if the condition is True

Example:
age = 18

if age >= 18:


print("You are an adult.")
(PYTHON)

12. Write a program to check enter string is palindrom or


not.

def is_palindrome(s):
"""Check if the given string is a palindrome."""

# Remove spaces and convert to lowercase for uniformity


cleaned_string = ''.join(s.split()).lower()

# Check if the cleaned string is equal to its reverse


return cleaned_string == cleaned_string[::-1]

# Input from the user


user_input = input("Enter a string to check if it is a palindrome: ")

if is_palindrome(user_input):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

13. Write the syntex use of while loop or for loop.


Ans-
while Loop :
count = 0
while count < 5:
print("Count is:", count)
count += 1 # Increment the count to eventually terminate the
loop
(PYTHON)

for Loop :
for i in range(5):
print("i is:", i)

14. What is python coontinue statement, pass


statement, break state ment.

Ans-
Continue statement :
The continue statement skips the rest of the code
inside the loop for the current iteration and proceeds
with the next iteration of the loop.

Use Case: Useful when you want to skip certain


iterations based on a condition but continue processing
the remaining iterations.

Pass statement :
The pass statement is a null operation. It is used as a
placeholder for future code. It does nothing when
executed and is often used when a statement is
syntactically required but you have no code to execute.
Use Case: Useful when you are planning to implement
functionality later but need to maintain the syntax
structure of your code.
(PYTHON)

break statement :
The break statement terminates the nearest
enclosing loop (either for or while) and transfers control
to the statement immediately following the loop.

Use Case: Useful when you need to exit a loop


prematurely based on a condition.

You might also like