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

1 Unit Assignment_CS-205_Python_Programming

The document contains an assignment on Python programming, covering short and long answer questions. It discusses concepts such as dynamic typing, identifiers, operators, data types, and memory allocation, along with Python programs for various tasks like calculating simple interest, grades, and factorials. The assignment aims to assess understanding of Python fundamentals and programming constructs.

Uploaded by

Ajeet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views7 pages

1 Unit Assignment_CS-205_Python_Programming

The document contains an assignment on Python programming, covering short and long answer questions. It discusses concepts such as dynamic typing, identifiers, operators, data types, and memory allocation, along with Python programs for various tasks like calculating simple interest, grades, and factorials. The assignment aims to assess understanding of Python fundamentals and programming constructs.

Uploaded by

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

Assignment No 1 (Python Programming)

Section A (Short Answer)

Q1 What is dynamic typing in python?

Answer:
Dynamic typing means Python determines variable types at runtime based on assigned
values, eliminating the need for explicit declarations.

Q2. Describe an identifier and keyword in python?

Answer:
An identifier names variables, functions, or objects. Keywords are reserved words in
Python, like 'if', 'for', and 'while', with predefined meanings.

Q2. Write down the python program to input 2 numeric values and find out its sum, minus,
multiply and division?

Answer:
a = float(input('Enter first number: '))
b = float(input('Enter second number: '))
print('Sum:', a + b)
print('Difference:', a - b)
print('Product:', a * b)
print('Division:', a / b)

Q4. Describe the concept of variable in python?

Answer:
A variable is a container for data, created by assigning a value to a name. Python infers its
type based on the value.

Q5. Write down the python program to find the simple interest of the given input values?

Answer:
P = float(input('Enter principal amount: '))
R = float(input('Enter rate of interest: '))
T = float(input('Enter time in years: '))
SI = (P * R * T) / 100
print('Simple Interest:', SI)
Q6. Describe the membership (in) operator in python with example?

Answer:
The 'in' operator checks if a value exists in a sequence. Example: 'a' in 'apple' returns True.

Q7. Describe bitwise operator (<<,>>) in python?

Answer:
'<<' shifts bits left, multiplying by 2. '>>' shifts bits right, dividing by 2.

Q8. To input the given character value and find out it either it is vowel or consonant using if-
else?

Answer:
char = input('Enter a character: ').lower()
if char in 'aeiou':
print('Vowel')
else:
print('Consonant')

Q9. Difference between break and continue?

Answer:
'break' exits the loop entirely, while 'continue' skips the current iteration and proceeds to
the next.

Q10. Write down the python program to print the given series 0+1+2+...+N. Input N at
runtime?

Answer:
N = int(input('Enter a number: '))
result = sum(range(N + 1))
print('Sum of series:', result)

Section B (Long Answer)

Q1. Describe down the features and benefits of the python?

Answer:
Python is a versatile, high-level programming language celebrated for its simplicity and
readability. It uses an easy-to-learn syntax that emphasizes code clarity, making it beginner-
friendly while efficient for professionals.

Python supports multiple programming paradigms, including procedural, object-oriented,


and functional programming, making it adaptable for diverse applications. It is platform-
independent, meaning Python code can run on various operating systems without
modification.

One of its greatest strengths is its extensive standard library and third-party modules,
which simplify tasks like web development, data analysis, machine learning, and
automation. Python's dynamic typing and automatic memory management enhance
developer productivity.

Moreover, Python has strong community support, with abundant resources, documentation,
and libraries that are regularly updated. Its integration capabilities allow seamless
interaction with other languages and tools, making it a preferred choice for scientific
computing, AI, and large-scale software development.

These features make Python highly efficient, scalable, and suitable for tasks ranging from
small scripts to complex applications, offering excellent performance and flexibility.

Q2. The Python program to find out the grade according to the input percentage value
percentage = float(input("Enter your percentage: "))

if percentage >= 90:


grade = "A"
elif percentage >= 75:
grade = "B"
elif percentage >= 50:
grade = "C"
else:
grade = "F"

print(f"Your Grade: {grade}")

This program uses conditional statements to categorize grades based on percentage. It


covers typical thresholds for academic grading systems.

Q3. Explain data types in Python in details

Python supports various data types:

1. Numeric Types:
o int: Integer values (e.g., 5, -20).
o float: Decimal numbers (e.g., 5.5, -3.14).
o complex: Numbers with real and imaginary parts (e.g., 3 + 4j).
2. Sequence Types:
o list: Ordered, mutable collections (e.g., [1, 2, 3]).
o tuple: Ordered, immutable collections (e.g., (1, 2, 3)).
o range: Sequence of numbers (e.g., range(5)).
3. Text Type:
o str: Strings of characters (e.g., "Hello").
4. Boolean Type:
o bool: Logical values, True or False.
5. Mapping Type:
o dict: Key-value pairs (e.g., {"key": "value"}).
6. Set Types:
o set: Unordered collections of unique items (e.g., {1, 2, 3}).
o frozenset: Immutable sets.
7. Binary Types:
o bytes, bytearray, memoryview: Work with binary data.

These data types ensure flexibility for different kinds of programming tasks.

Q4. Define various operators in Python

Python operators are categorized as:

1. Arithmetic Operators: Perform mathematical operations.


o + (addition), - (subtraction), * (multiplication), / (division), % (modulus),
** (exponent), // (floor division).
2. Relational (Comparison) Operators: Compare values.
o ==, !=, >, <, >=, <=.
3. Logical Operators: Combine conditional statements.
o and, or, not.
4. Bitwise Operators: Operate on binary representations.
o &, |, ^, ~, <<, >>.
5. Assignment Operators: Assign or update values.
o =, +=, -=, *=, /=, %=.
6. Membership Operators: Check for membership in a sequence.
o in, not in.
7. Identity Operators: Compare memory locations.
o is, is not.

Q5 Write down the Python program to calculate the factorial of the given input value using
both for and while loop
# Using for loop
num = int(input("Enter a number: "))
factorial = 1

for i in range(1, num + 1):


factorial *= i

print(f"Factorial (using for loop): {factorial}")

# Using while loop


factorial = 1
i = 1

while i <= num:


factorial *= i
i += 1

print(f"Factorial (using while loop): {factorial}")

Q6. Explain the loops for and while in Python

1. For Loop: Used for iterating over a sequence (e.g., list, tuple, string).

python
Copy code
for i in range(5):
print(i)

2. While Loop: Repeats a block of code while a condition is True.

python
Copy code
count = 0
while count < 5:
print(count)
count += 1

Use break to exit a loop prematurely and continue to skip the current iteration.

Q7. Describe the structure of the Python program

A typical Python program has the following structure:

1. Documentation: Comments explaining the purpose of the program.


2. Imports: Import necessary modules or libraries.
3. Global Variables: Define constants or global variables.
4. Functions: Encapsulate code into reusable blocks.
5. Main Logic: Contains the main part of the program, often under if __name__
== "__main__":.

Example:

python
Copy code
# Documentation
"""
This program demonstrates the structure of Python code.
"""
# Importing libraries
import math

# Defining functions
def greet():
print("Hello, World!")

# Main logic
if __name__ == "__main__":
greet()

Q8. The Python program to print the given format using for loop
for i in range(1, 6):
print(" ".join(map(str, range(1, i + 1))))
print(" ".join(map(str, range(5, i - 1, -1))))

Q9. Explain the memory allocation for a variable in Python with an example

In Python, variables are references to objects stored in memory. The actual data resides in
memory locations managed by Python’s internal mechanisms. Memory management
includes:

1. Reference Counting: Tracks the number of references to an object.


2. Garbage Collection: Reclaims memory of unused objects.

Example:

python
Copy code
x = 10
y = x # Both x and y reference the same object
print(id(x), id(y)) # Prints the same memory address

Q10. Explain the conversion function in Python

Conversion functions in Python allow changing the type of data. Common functions:

1. int(): Converts to integer.


2. float(): Converts to float.
3. str(): Converts to string.
4. list(), tuple(), set(): Convert between sequences.
5. bool(): Converts to boolean.

Example:

num = "123"
converted = int(num)
print(converted + 10) # Outputs 133

You might also like