0% found this document useful (0 votes)
3 views5 pages

Python Assignment Answers

The document provides answers to 16 Python-related questions covering topics such as the features of Python, program structure, advantages over C, standard data types, control statements, loops, functions, and operators. It includes examples for various concepts like control flow, input/output functions, and the use of the math and datetime modules. Additionally, it discusses naming conventions for identifiers and control statements like break and continue.

Uploaded by

ashwinij.1177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Assignment Answers

The document provides answers to 16 Python-related questions covering topics such as the features of Python, program structure, advantages over C, standard data types, control statements, loops, functions, and operators. It includes examples for various concepts like control flow, input/output functions, and the use of the math and datetime modules. Additionally, it discusses naming conventions for identifiers and control statements like break and continue.

Uploaded by

ashwinij.1177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Assignment Answers (Questions 1–16)

Q1.
What is Python? Explain the main features of Python.
Python is a high■level, interpreted programming language known for its simple and readable syntax. I

Main Features:
1. Easy to learn and read.
2. Interpreted language (no need to compile).
3. Object■oriented programming support.
4. Large standard library.
5. Platform independent.
6. Supports multiple programming paradigms.
7. Automatic memory management.
8. Open source and free to use.

Q2.
Explain the structure of a Python program with an example.

Basic Structure:
1. Import statements
2. Variable declarations
3. Logic or statements
4. Output

Example:
import math

num = 16
result = [Link](num)

print("Square root is:", result)

Q3.
Write the advantages of Python over C language.

1. Python has simpler syntax than C.


2. Python programs are shorter and easier to write.
3. Automatic memory management in Python.
4. Python supports dynamic typing.
5. Large libraries available in Python.
6. Faster development compared to C.

Q4.
What are Standard Data Types in Python? Explain any three with example.

Standard Data Types:


1. Numeric
2. String
3. List
4. Tuple
5. Set
6. Dictionary

Examples:
Numeric: x = 10
String: name = "Python"
List: numbers = [1,2,3,4]

Q5.
What are Control Statements in Python? Explain any one with example.

Control statements control the flow of execution of a program.

Types:
1. Decision making (if)
2. Looping (for, while)
3. Jump statements (break, continue)

Example (if):
num = 10
if num > 0:
print("Positive number")

Q6.
Explain the if statement, if■else and if■elif■else with example.

if statement:
x = 10
if x > 5:
print("Greater than 5")

if■else:
x = 3
if x > 5:
print("Greater")
else:
print("Smaller")

if■elif■else:
marks = 75
if marks >= 90:
print("A grade")
elif marks >= 70:
print("B grade")
else:
print("C grade")

Q7.
Explain for loop and while loop with example.

for loop:
Used to iterate over a sequence.

for i in range(5):
print(i)

while loop:
Executes as long as condition is true.

i = 1
while i <= 5:
print(i)
i += 1

Q8.
What are the rules for naming identifiers in Python?

1. Identifier must start with a letter or underscore.


2. Cannot start with a number.
3. Only letters, digits and underscore allowed.
4. Cannot use Python keywords.
5. Identifiers are case sensitive.
Example: total_marks, _value, student1

Q9.
Explain the following functions with example.

range():
Used to generate a sequence of numbers.

Example:
for i in range(1,5):
print(i)

input():
Used to take input from user.

Example:
name = input("Enter your name: ")
print("Hello", name)

Q10.
Explain range().

range() function generates a sequence of numbers.

Syntax:
range(start, stop, step)

Example:
for i in range(1,10,2):
print(i)

Q11.
Explain input().

input() function is used to accept data from the user.

Example:
age = input("Enter your age: ")
print("Your age is:", age)

Q12.
What is a function in Python? What are the advantages?

A function is a block of reusable code used to perform a specific task.

Advantages:
1. Code reusability.
2. Reduces code duplication.
3. Improves readability.
4. Makes debugging easier.
5. Helps divide program into modules.

Q13.
Write a Python program:

i) Factorial using math module


import math
num = 5
print("Factorial:", [Link](num))

ii) Square root using math module


import math
num = 25
print("Square root:", [Link](num))

iii) Extract substring


text = "Hello Python"
print(text[0:5])
iv) Get today's date
from datetime import date
today = [Link]()
print("Today's date:", today)

v) Get yesterday's date


from datetime import date, timedelta
yesterday = [Link]() - timedelta(days=1)
print("Yesterday:", yesterday)

Q14.
List different types of operators in Python.

1. Arithmetic Operators (+, -, *, /, %)


2. Relational Operators (>, <, ==)
3. Logical Operators (and, or, not)
4. Assignment Operators (=, +=, -=)
5. Bitwise Operators (&, |, ^)
6. Membership Operators (in, not in)
7. Identity Operators (is, is not)

Q15.
Explain the following control statements with syntax and example.

1. for loop

Syntax:
for variable in sequence:
statements

Example:
for i in range(3):
print(i)

2. while loop

Syntax:
while condition:
statements

Example:
i = 1
while i <= 3:
print(i)
i += 1

3. if elif else

Syntax:
if condition:
statement
elif condition:
statement
else:
statement

Q16.
Discuss break and continue statements with syntax and example.

break:
Used to terminate the loop.

Example:
for i in range(5):
if i == 3:
break
print(i)
continue:
Used to skip current iteration.

Example:
for i in range(5):
if i == 3:
continue
print(i)

You might also like