2 marks python:
significance of jupyter notebook in python
Jupyter Notebook is very useful in Python because it makes learning, writing, and testing code easier.
Here’s the simple significance:
1. Interactive Coding – You can write Python code and run it step by step in small blocks (called
cells).
2. Instant Results – It immediately shows output below the code, so you can test quickly.
3. Easy to Learn – Beginners can easily try examples and see results without writing full
programs.
4. Supports Text + Code – You can add notes, explanations, and even math formulas along with
code.
5. Data Science Friendly – Widely used for data analysis, machine learning, and visualization
because it shows graphs and tables directly.
four important features of Python :
1. Easy to Learn and Readable – Python has simple English-like syntax, so beginners can
understand and write code easily.
2. Interpreted Language – You don’t need to compile; Python runs line by line, making testing
and debugging easier.
3. Portable – The same Python code can run on different operating systems (Windows, Mac,
Linux) without changes.
4. Rich Libraries – Python has a huge collection of built-in libraries (like math, random,
datetime) and external ones (like NumPy, Pandas, TensorFlow) for almost every task.
output of print(type(3.5))
In Python:
print(type(3.5))
Output:
<class 'float'>
Because 3.5 is a decimal number, and in Python decimal numbers belong to the
float data type.
what is the syntax of the input() function in python
The syntax of the input() function in Python is:
input(prompt)
• prompt (optional): A message (string) shown to the user before taking input.
• It always returns the input as a string.
Example:
name = input("Enter your name: ")
print("Hello,", name)
If you just write input() without a prompt, it will wait for the user to type something, but won’t
display any message.
List the different types of comments in Python
In Python, there are mainly two types of comments:
1. Single-line Comment
o Starts with a # symbol.
o Anything after # on that line is ignored by Python.
2. # This is a single-line comment
3. print("Hello, Python!") # Comment at the end of a line
4. Multi-line (Block) Comment
o Written using triple quotes ''' ... ''' or """ ... """.
o Often used for longer explanations or documentation.
5. """
6. This is a multi-line comment
7. It can span multiple lines
8. Python ignores it while running the code
9. """
10. print("Multi-line comment example")
What is the difference between = and == operators?
1. = (Assignment Operator)
• Used to assign a value to a variable.
• Example:
• x = 10 # assigns value 10 to variable x
• name = "Python"
2. == (Equality Operator)
• Used to compare two values.
• Returns True if values are equal, otherwise False.
• Example:
• x = 10
• print(x == 10) # True
• print(x == 5) # False
Write a one-line Python statement to find the square of a number.
num = 5
square = num ** 2
print(square) # Output: 25
You can also use:
square = num * num
Mention any two logical operators in Python
In Python, two commonly used logical operators are:
1. and – Returns True if both conditions are true.
x=5
print(x > 2 and x < 10) # True
2. or – Returns True if at least one condition is true.
x=5
print(x > 2 or x > 10) # True
What is the use of the break statement?
The break statement in Python is used to exit (terminate) a loop immediately when a certain
condition is met, even if the loop’s normal condition is still true.
Example with for loop:
for i in range(1, 10):
if i == 5:
break # exit the loop when i = 5
print(i)
Output:
3
4
As soon as i == 5, the break statement stops the loop.
Use: To stop a loop early when a condition is satisfied.
Expand IDLE and write its use in Python programming
IDLE stands for Integrated Development and Learning Environment.
Uses of IDLE in Python programming:
1. Code Writing & Editing – Provides a simple editor to write Python programs.
2. Interactive Shell – Lets you run Python commands one by one and see results immediately.
3. Debugging – Has tools to find and fix errors in your code.
4. Beginner-Friendly – Comes pre-installed with Python, so it’s easy for beginners to practice.
IDLE is the default Python IDE that helps you write, run, and debug Python programs easily.
UNIT 2
What is the difference between return and print statements?
print Statement
• Used to display output on the screen.
• Mainly for the user to see results.
• It does not give back a value to the program.
Example:
def add(a, b):
print(a + b)
result = add(5, 3) # prints 8
print("Result is:", result) # prints None (because nothing was returned)
return Statement
• Used to send a value back from a function to the caller.
• The returned value can be stored in a variable and reused.
Example:
def add(a, b):
return a + b
result = add(5, 3) # returns 8
print("Result is:", result) # prints: Result is: 8
Write the syntax to define a function in Python.
The syntax to define a function in Python is:
def function_name(parameters):
# function body (statements)
return value # optional
Example:
def greet(name):
"""This function greets the user by name."""
print("Hello,", name)
• def → keyword to define a function.
• function_name → name of the function.
• parameters → values passed into the function (optional).
• return → used to send a value back (optional).
Name two built-in string functions and their uses.
Here are two built-in string functions in Python
• upper()
Converts all characters of a string to uppercase.
text = "python"
print([Link]()) # Output: PYTHON
• lower()
Converts all characters of a string to lowercase.
text = "HELLO"
print([Link]()) # Output: hello
What is the output of 'Python'[::-1]?
In Python:
print('Python'[::-1])
Output:
nohtyP
• [::-1] means slice the string with a step of -1, i.e., take characters in reverse order.
• So 'Python' becomes 'nohtyP'.
What are positional arguments in Python functions?
Positional Arguments
In Python functions, positional arguments are the arguments that are passed to a function in the
correct order (position) as defined in the function.
• The position matters — the first value goes to the first parameter, the second value to the
second parameter, and so on.
Example:
def student_info(name, age):
print("Name:", name)
print("Age:", age)
# Calling with positional arguments
student_info("Ravi", 21)
Output:
Name: Ravi
Age: 21
Here "Ravi" is assigned to name and 21 is assigned to age because of their positions.
Mention any two methods used on lists
Here are two commonly used list methods in Python:
1. append()
o Adds an element to the end of the list.
fruits = ["apple", "banana"]
[Link]("mango")
print(fruits) # Output: ['apple', 'banana', 'mango']
2. remove()
o Removes the first occurrence of a specified element from the list.
fruits = ["apple", "banana", "mango", "banana"]
[Link]("banana")
print(fruits) # Output: ['apple', 'mango', 'banana']
What is the purpose of the len() function?
The len() function in Python is used to find the number of items in a sequence or collection.
• Strings → returns number of characters
• Lists/Tuples/Sets → returns number of elements
• Dictionaries → returns number of key-value pairs
Examples:
# String
print(len("Python")) # Output: 6
# List
print(len([10, 20, 30])) # Output: 3
# Dictionary
print(len({"a": 1, "b": 2})) # Output: 2
Differentiate between list and tuple in a single line.
A list is mutable (can be changed), whereas a tuple is immutable (cannot be changed).
Example:
my_list = [1, 2, 3]
my_list[0] = 10 # allowed
my_tuple = (1, 2, 3)
my_tuple[0] = 10 # error (tuples can’t be modified)
Feature List ([]) Tuple (())
Mutability Mutable (can be changed) Immutable (cannot be changed)
Syntax Defined with square brackets [ ] Defined with parentheses ( )
Performance Slower (because mutable) Faster (because immutable)
Many methods like append(),
Functions Fewer methods (only count(), index())
remove(), sort()
When data can change (e.g., When data should not change (e.g., coordinates,
Use case
shopping cart) fixed values)
What is a docstring in Python?
Docstring in Python stands for documentation string.
Definition:
A docstring is a special string written inside triple quotes ("""...""" or '''...''') that is used to
document a function, class, or module.
• It explains what the code does.
• Python stores it in the __doc__ attribute.
• Docstrings are multi-line comments for documentation.
• Unlike normal comments, they can be accessed programmatically using __doc__.
Syntax:
def function_name():
"""This is a docstring describing the function."""
# function body
pass
Example:
def greet(name):
"""This function greets the user with their name."""
print("Hello,", name)
print(greet.__doc__)
Output:
This function greets the user with their name.
Write the output of: x = [1, 2, 3]; [Link]([4, 5]); print(x)
x = [1, 2, 3]
[Link]([4, 5])
print(x)
1. x starts as [1, 2, 3].
2. [Link]([4, 5]) adds the entire list [4, 5] as a single element at the end.
Output:
[1, 2, 3, [4, 5]]