You are on page 1of 12

Python

State and explain any six features of python.


1. Readability: Python emphasizes code readability with its consistent and clear syntax. Its
use of indentation instead of brackets or braces makes it easy to understand the structure
of the code.

2. Simplicity: Python is designed to be simple and straightforward. Its simplicity allows


beginners to learn and understand the language quickly. It also simplifies the development
process and reduces the time taken for programming tasks.

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.

4. Cross-platform compatibility: Python is a cross-platform language, meaning it can run on


different operating systems without the need for significant modifications. This makes it
highly versatile and adaptable, allowing developers to create applications that can run
seamlessly on various platforms.

5. Integration capabilities: Python offers excellent integration capabilities with other


languages such as C, C++, and Java. It allows developers to easily incorporate existing
code or libraries written in other languages into their Python projects. This feature enhances
code reusability and promotes interoperability.

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:

name = input(“please enter your name: “)


print(“hello, “ + name + “! nice to meet you.”)

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”.

Explain following operators of python with suitable examples of each.


1. is
2. in
3. not in
The “is” operator in Python is a comparison operator used to test whether two objects have the
same identity or not. For example:
x=5
y=5
print(x is y) # Output: True
In this example, the “is” operator returns True because the objects x and y have the same identity.

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.

Explain while loop with suitable example?


a while loop in Python is a loop that will keep running until the condition is false. For example,
let’s say we want to print the numbers 1 to 10. We could create a while loop like this:

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): “))

# Convert kilometers to meters and centimeters


distance_m = distance_km * 1000
distance_cm = distance_km * 100000

# Print the converted distances


print(“Distance in meters:”, distance_m)
print(“Distance in centimeters:”, distance_cm)

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.

The pass statement is used to do nothing in a statement block. For example:


for num in range(10):
if num % 2 == 0:
pass
else:
print(num)
In this example, the loop will print the odd numbers from 1 to 9 while the even numbers will be
skipped over using the pass statement.
Explain append() and copy() methods of list with suitable examples of each
Sure, the append() and copy() methods are useful for manipulating and copying lists in Python.
The append() method is used to add a single element to a list. For example:

# create a list
my_list = [“cat”, “dog”, “rabbit”]

# append “hamster” to the list


my_list.append(“hamster”)

# print the list


print(my_list)

This will result in the list [“cat”, “dog”, “rabbit”, “hamster”].

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”]

# copy the list


my_list_copy = my_list.copy()

# print the copy


print(my_list_copy)

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:

length = float(input(“enter the length: “))


breadth = float(input(“enter the breadth: “))

area = length * breadth


perimeter = 2 * (length + breadth)

print(“area = %.2f” % area)


print(“perimeter = %.2f” % perimeter)

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 and y < z:


print(“both conditions are true.”)
else:
print(“at least one condition is false.”)
2. “or” Operator:
The “or” operator returns True if at least one of the operands is True. It returns False only if both
operands are False. It can be used to check if at least one condition is True.
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

What are functions in python? Explain recursion with suitable example.


Functions are an essential part of Python programming. They are named blocks of code that
perform specific tasks. A function can optionally take arguments (input values) and return a result.
A recursive function is a function that calls itself as part of its own definition. This allows a function
to repeat its execution and resolve complex tasks in a few lines of code. A classic example of
recursion is the Fibonacci sequence, which can be written using recursion like this:

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.

You might also like