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

ICT - Python Q and A

The document outlines key features of Python, including its readability, interpreted nature, dynamic typing, and extensive standard library. It also discusses common functions, control statements, and provides examples of Python programs for various tasks. Additionally, it highlights the differences between .py and .pyc files, the importance of indentation, and the characteristics of iteration and decision-making statements.

Uploaded by

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

ICT - Python Q and A

The document outlines key features of Python, including its readability, interpreted nature, dynamic typing, and extensive standard library. It also discusses common functions, control statements, and provides examples of Python programs for various tasks. Additionally, it highlights the differences between .py and .pyc files, the importance of indentation, and the characteristics of iteration and decision-making statements.

Uploaded by

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

Python

Q1. List key features of Python?

Ans. Python's Core Language Features:

Easy to Learn and Read:

Python's syntax is designed to be clear and concise, resembling English, which makes it
beginner-friendly.

Interpreted Language:

Python code is executed line by line, without the need for explicit compilation. This speeds up
development and debugging.

Dynamically Typed:

You don't need to declare variable types explicitly. Python infers them at runtime, providing
flexibility.

High-Level Language:

Python abstracts away low-level details, allowing developers to focus on problem-solving rather
than memory management.

Object-Oriented:

Python supports object-oriented programming (OOP), enabling the creation of modular and
reusable code.

Open Source and Free:

Python is freely available and open-source, fostering a large and active community.

Extensive Standard Library:

Python comes with a rich set of built-in modules and functions, providing ready-made solutions
for various tasks.

Cross-Platform Compatibility:
Python runs seamlessly on various operating systems, including Windows, macOS, and Linux.

Q2. Write some common functions of Python?

Ans. Some common functions are

1. Basic Input/Output:

●​ print(): Displays output to the console.


○​ print("Hello, world!")​

●​ input(): Reads input from the user.


○​ name = input("Enter your name: ")​

2. Data Type Conversion:

●​ int(): Converts a value to an integer.


num = int("10")​

●​ float(): Converts a value to a floating-point number.


pi = float("3.14")​

●​ str(): Converts a value to a string.


text = str(123)​

3. Mathematical Functions:

●​ abs(): Returns the absolute value of a number.


absolute_value = abs(-5)​

●​ round(): Rounds a number to a specified number of decimal places.


rounded_number = round(3.14159, 2)​

4. Sequence Operations:

●​ len(): Returns the length of a sequence (string, list, tuple, etc.).


length = len("Python")​

●​ max(): Returns the largest item in a sequence.


maximum = max([1, 5, 2, 8])​

●​ min(): Returns the smallest item in a sequence.


minimum = min([1, 5, 2, 8])​

●​ sum(): Returns the sum of the items in a sequence.


total = sum([1, 2, 3, 4])​
●​ type(): Returns the type of an object.
data_type = type(10)​

Q3. What are control statements and discussions two types of control functions
in Python?

Ans. Control statements are used to control the flow of execution of the program based Upon
some conditions values and logic.

Two types of control statements in Python


1.​ Decision making statements (if … else): These statements are used for branching flow of
control based upon some condition.

If statement, if….else statement, Nested if statement, life statement

2.​ Iteration statements (loops) : These statements are used for executing statements
repeatedly for a finite number of times based upon a condition.

For loop and While loop

Q4. Write down syntax for While loop?

Ans. The syntax for While loop is

Initialization

While <condition>:

loop body

Updation

e.g

i=1
sum = 0

While (i<=10)

sum = sum + i
print (i)

i=i+1

print (“sum of 1,2….10 is, sum

Q5. Write a Python program to check if a given number is positive, negative or


zero?

Ans.

number = int(input("Enter a number: "))

if number > 0:
print("That's a positive number!")
elif number < 0:
print("That's a negative number!")
else:
print("That's zero!")

Q6. Write a Python program to calculate sum of numbers from 1 to a given


number using While loop?

Ans.

# Ask the user for a number


given_number = int(input("Enter a number: "))

# Start with 1 and the sum at 0


current_number = 1
total_sum = 0

# Keep adding numbers until we reach the given number


while current_number <= given_number:
total_sum = total_sum + current_number
current_number = current_number + 1

# Show the result


print("The sum of numbers from 1 to", given_number, "is:", total_sum)
Q7. Write a Python program to generate multiplication table of given number
using for loop

Ans.

# Ask the user for a number


number = int(input("Enter a number: "))

# Print the multiplication table


print("Multiplication Table of", number)

# Loop from 1 to 10
for i in range(1, 11):
# Calculate and print each line of the table
result = number * i
print(number, "x", i, "=", result)

Q8. Debugging the program

numbers = [ 10, 20, 30, 40, 50]


sum = 0
for num in numbers:
sum = sum + num
average = sum/len(numbers)
print(“The average of the number is “, average)

Q9. How is .pyc file different from .py File?

Ans. Key Differences Summarized:

Content:
* .py files contain source code.
* .pyc files contain bytecode.

Readability:
* .py files are human-readable.
* .pyc files are not easily human-readable.

Purpose:
* .py files are for writing and editing Python code.
* .pyc files are for optimizing the execution of Python code.
Generation:
* .py files are created by the programmer.
* .pyc files are automatically generated by the Python interpreter.

Speed:
* .pyc files allow for faster execution, especially when importing modules.

In essence, .pyc files are a performance optimization that allows Python to run code more
efficiently.

Q10. Describe the different types of decision making statements available in


Python.

Ans. Decision making statements in Python are

If statement
If…else statement
Nested if statement
elif statement

Q11. How Python is an interpreted language?

Ans. Python's code is executed directly by an interpreter, line by line. It skips a separate
compilation step into machine code. The interpreter translates and runs the code as it reads it,
making development quick and flexible.

Q12. What is the importance of indentation in Python?

Ans. Python uses indentation to define scope of code.

Q13. How does Nested if and elif different in Python?

Ans. Nested if is used when we have to check whether a condition is true and elif is used when
we check the condition is false.

Q14. Is Python case sensitive?

Ans. Yes
Q15. What are iteration statements in Python? Define each one of them?.

Ans. Iteration statements, also known as loops, allow you to repeatedly execute a block of code.
Python provides two primary iteration statements: for loops and while loops.

1. for Loop:
The for loop iterates over a sequence (like a list)
The for loop executes a block of code repeatedly until the condition is valid

2. while Loop:
With While loop we can executed a set of statements as long as condition is true.

You might also like