Python Ans
Python Ans
Python Ans
3. Extensive Libraries: Python has a vast collection of libraries and modules that provide pre-
written code for various functionalities. These libraries cover areas such as data
manipulation, web development, scientific computing, machine learning, and more. The
availability of these libraries saves time and effort in coding complex functions from
scratch.
6. Dynamically Typed: Python is a dynamically typed language, which means that variable
types are checked during runtime rather than at compile-time. This enables more flexibility
as variables can be assigned different types of values at different points in the program.
Dynamic typing also reduces the amount of code needed and simplifies the development
process.
Explain input and print command in python with suitable example.
The input and print commands are both fundamental building blocks of programming in Python.
The input command allows you to prompt the user for input and store their response. It takes an
optional string argument that serves as the prompt for the user. Here’s an example:
In the above example, the input command is used to prompt the user for their name. The user’s
input is then stored in the variable name. The print command is used to display a greeting message,
including the user’s name.
The print command is used to display outputs or values to the console. It can display variables,
strings, numbers, or any other data type. Here’s an example:
age = 25
print(“i am “ + str(age) + “ years old.”)
In the above example, the print command is used to display a message that includes the value of
the age variable. The str function is used to convert the integer age to a string, as the print command
can only display strings directly.
Both the input and print commands are essential for interacting with the user and displaying
information in Python programs. They are commonly used in a wide range of applications,
including user interfaces, command-line tools, and data processing scripts.
What will be the output of following print statements in python?
print('GoodLuck' * 2)
The output of the print statement would be “GoodLuckGoodLuck”. This is because when the string
“GoodLuck” is multiplied by 2, the same string is repeated twice, resulting in the output of
“GoodLuckGoodLuck”.
Print(3-4j + 2+6j)
The output of the print statement would be (5+2j). This is because when adding two complex
numbers, the real numbers are added together, as well as the imaginary ones. In this case, we are
adding 3-4j and 2+6j, resulting in 3+2j + 2+6j = 5+2j.
print(‘3+4j’*int(4/2))
The output of the print statement would be “3+4j3+4j”. This is because when the string “3+4j” is
multiplied by the integer 2 (4/2), the same string is repeated twice, resulting in the output of
“3+4j3+4j”.
The “in” operator is also a comparison operator used to test whether an object is present in an
iterable object, such as a string, list, or dict. For example:
x = [1,2,3,4]
print(2 in x) # Output: True
In this example, the “in” operator returns True because the value 2 is present in the list x.
The “not in” operator is the exact opposite of the “in” operator and is used to test whether an object
is not present in an iterable object. For example:
x = [1,2,3,4]
print(5 not in x) # Output: True
In this example, the “not in” operator returns True because the value 5 is not present in the list x.
num = 1
while num <= 10:
print(num)
num += 1
This will loop until the condition num <= 10 returns False and the loop will stop. The code inside
the loop, print(num) and num += 1, will be executed at each iteration of the loop. The first line
num = 1 sets the initial value for the loop, 1 in this case.
This means the loop will start from the number 1 and will add 1 to the value of num at each iteration
(using num += 1) until it reaches 10 and the loop is terminated. The result will be a sequence of
numbers from 1 to 10 printed on the screen.
The distance between two cities (in km.) is input through the keyboard. Write a python
program to convert and print this distance in meters and centimeters.
distance_km = float(input(“Enter the distance between two cities (in kilometers): “))
Output
Enter the distance between two cities (in kilometers): 150
Distance in meters: 150000.0
Distance in centimeters: 15000000.0
Explain break, continue and pass statement with suitable example of each.
The break, continue, and pass statements are utilized in loops to control the flow of the program.
The break statement is used to terminate a loop before all of the statements have been executed.
For example, if you have a loop that iterates through a list of numbers and you want to stop the
loop when a particular number is encountered, you can use the break statement. For example:
numbers = [1, 2, 3, 4, 5]
nor num in numbers:
if num == 3:
break
print(num)
In this example, the loop will only print the numbers 1 and 2 since when the number 3 is
encountered, the break statement is executed and the loop is terminated.
The continue statement is used to skip over the current iteration and go to the next one. For
example:
Numbers = [1, 2, 3, 4, 5]
For num in numbers:
If num == 3:
Continue
Print(num)
In this example, the number 3 will be skipped over and the loop will print the numbers 1, 2, 4, and
5.
# create a list
my_list = [“cat”, “dog”, “rabbit”]
The copy() method is used to create a copy of a list. This is useful if you want to duplicate a given
list without altering the original one. For example:
# create a list
my_list = [“cat”, “dog”, “rabbit”]
This will result in the list [“cat”, “dog”, “rabbit”] being printed.
The length & breadth of a rectangle are input through the keyboard. Write a python
program to calculate and print the arca & perimeter of the rectangle.
The formula for the area of a rectangle is length * breadth, and the formula for the perimeter is 2
* (length + breadth). We can use this to calculate the area and perimeter of the rectangles like this:
Explain “and, or, not” operators of python with suitable examples of each.
1. “and” Operator:
The “and” operator returns True if both operands are True. Otherwise, it returns False. It can be
used to check if multiple conditions are all True. If any of the conditions is False, the result will be
False.
Example:
x=5
y = 10
z=7
if x > y or y < z:
print(“at least one condition is true.”)
else:
print(“both conditions are false.”)
3. “not” Operator:
The “not” operator is a unary operator that returns the opposite of the operand’s logical value. If
the operand is True, “not” returns False, and if the operand is False, “not” returns True. It can be
used to negate a condition.
Example:
x=5
y = 10
if not x > y:
print(“x is not greater than y.”)
else:
print(“x is greater than y.”)
Write a python program to print following pattern (using while loop).
aaaaaaa
aaaaa
aaa
a
n=7
while n >= 1:
print(‘a’ * n)
n -= 2
def fibonacci(n):
if n<=0:
return 0
elif n==1:
return 1
else:
return fibonacci(n-1)+fibonacci(n-2)
This function will recursively calculate the Fibonacci sequence for any positive integer n. For
instance, the fifth number in the Fibonacci sequence (5) can be calculated by calling Fibonacci(5).
The output of this call would be 5.
What is package? What is Python PIP? With example explain how to get list of all packages
installed on your system?
In Python, a package is a collection of related Python modules, which has its own version number
and can be listed in the Python Package Index (PyPI). PIP is the package manager for Python. It
makes it easier to find, install, and manage Python packages.
To get a list of all packages installed on your system, you can use the “pip list” command. For
example, running the command “pip list” will output the names and versions of all packages
installed on your system. As an example, running this command might output something that looks
like this:
pip (20.2.4)
setuptools (50.3.2)
numpy (1.19.4)
matplotlib (3.3.3)
This example shows that the system has the packages pip, setuptools, numpy, and matplotlib
installed.
What will be the output of following python statements?
print(23 // 5)
The output of the statement “print(23 // 5)” is 4.
print(2 <<2)
The output of the statement “print(2 <<2)” is 8.
print(2 >> 0)
The output of the statement “print(2 >> 0)” is 2.
print(2^2)
The output of the statement “print(2^2) ” is 0.
print(2!=2)
The output of the statement “print(2!=2)” is False.
print(2<0)
The output of the statement “print(2<0) ” is False.