1 Unit Assignment_CS-205_Python_Programming
1 Unit Assignment_CS-205_Python_Programming
Answer:
Dynamic typing means Python determines variable types at runtime based on assigned
values, eliminating the need for explicit declarations.
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)
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.
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')
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)
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.
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: "))
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.
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
1. For Loop: Used for iterating over a sequence (e.g., list, tuple, string).
python
Copy code
for i in range(5):
print(i)
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.
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:
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
Conversion functions in Python allow changing the type of data. Common functions:
Example:
num = "123"
converted = int(num)
print(converted + 10) # Outputs 133