ASSIGNMENT-1
Q1. Discuss features of python.
Python is a popular high-level programming language
known for its simplicity and versatility. Here are
the key features of Python:
✅ 1. Easy to Learn and Use
• Simple and readable syntax (similar to English).
• Minimal boilerplate code required.
• Great for beginners and experienced developers alike.
print("Hello, World!") # Example of simple syntax
✅ 2. Interpreted Language
• Python code is executed line-by-line (not compiled first).
• Easier to debug and test.
• Reduces development time.
✅ 3. Dynamically Typed
• No need to declare variable types.
• Variables can change type during execution.
x = 10 # x is an integer
x = "hello" # now x is a string
✅ 4. High-Level Language
• Abstracts low-level operations (like memory management).
• Focuses on logic and problem-solving rather than machine-level details.
✅ 5. Portable
• Write once, run anywhere (WORA).
• Python runs on Windows, macOS, Linux, etc., without modification.
✅ 6. Extensive Standard Library
• Comes with a rich set of built-in modules and functions.
• Examples: math, datetime, os, json, re, etc.
import math
print(math.sqrt(25)) # Outputs: 5.0
✅ 7. Object-Oriented and Functional
• Supports object-oriented programming (OOP): classes, inheritance,
encapsulation.
• Also supports functional features like map(), filter(), lambda, etc.
✅ 8. Large Community and Ecosystem
• Active global community.
• Tons of third-party packages (via PyPI).
• Libraries for web dev (Django, Flask), data science (Pandas, NumPy), ML
(TensorFlow, Scikit-learn), and more.
✅ 9. Embeddable and Extensible
• Can integrate with other languages like C/C++.
• Useful for performance-critical applications.
✅ 10. Free and Open Source
• Python is freely available.
• Open-source under the Python Software Foundation License.
✅ 11. Supports Multiple Programming Paradigms
• Procedural, object-oriented, and functional paradigms.
• Makes it flexible for different types of projects.
✅ 12. Automatic Memory Management
• Built-in garbage collection handles memory allocation and deallocation.
✅ 13. Interactive Mode
• Python has an interactive shell (REPL).
• Useful for quick tests and debugging.
Q2 What are literals explain with the help
of examples.
In programming, literals are the fixed values we
directly use in the code to represent data.
They are constant values (not variables) that are
assigned to variables or used directly in
expressions.
For example:
x = 10
y = 3.14
name = "Varsha"
flag = True
Here, 10, 3.14, "Varsha", and True are literals.
Types of Literals (with examples in Python):
1. Numeric Literals
• Integers: 10, -25, 0
• Floating point: 3.14, -0.5, 2.0
• Complex: 3+4j, -2j
a = 10 # integer literal
b = 3.14 # float literal
c = 2 + 3j # complex literal
2. String Literals
• Text inside quotes "Hello" or 'World'
• Multi-line strings with """ ... """
name = "Varsha"
msg = 'Hello, world!'
para = """This is
a multi-line string."""
3. Boolean Literals
• Only two: True and False
is_student = True
passed = False
4. Special Literal
• None represents nothing or null value.
x = None
5. Collection Literals (Python specific)
• List: [1, 2, 3]
• Tuple: (10, 20, 30)
• Dictionary: {"name": "Varsha", "age": 20}
• Set: {1, 2, 3}
numbers = [1, 2, 3] # list literal
coords = (10, 20) # tuple literal
data = {"name": "Varsha"} # dict literal
unique = {1, 2, 3} # set literal
Q3. List various operators supported in
python.
Operators are special symbols that perform operations
on variables and values.
1. Arithmetic Operators
An arithmetic operator is a symbol in programming (and mathematics) that is
used to perform basic mathematical operations on data values (operands).
Used for mathematical operations.
a = 10
b = 3
print(a + b) # 13 (Addition)
print(a - b) # 7 (Subtraction)
print(a * b) # 30 (Multiplication)
print(a / b) # 3.333... (Division - float)
print(a // b) # 3 (Floor Division - quotient only)
print(a % b) # 1 (Modulus - remainder)
print(a ** b) # 1000 (Exponentiation, 10³)
2. Relational (Comparison) Operators
A relational operator is a symbol in programming that is used to compare two
values.
The result of a relational operation is always Boolean → either True or False.
Used to compare values (results are True or False).
a = 10
b = 20
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= b) # False
print(a <= b) # True
3. Logical Operators
A logical operator is used in programming to combine or compare two or more
conditions (Boolean values: True/False) and return a result that is also True or
False.
Used to combine conditional statements.
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
4. Assignment Operators
An assignment operator is used in programming to assign (store) a value to a
variable.
It can also be combined with arithmetic operations to update the value of a
variable.
Used to assign values to variables.
a = 5 # simple assignment
a += 2 # a = a + 2 → 7
a -= 3 # a = a - 3 → 4
a *= 2 # a = a * 2 → 8
a /= 4 # a = a / 4 → 2.0
a //= 2 # a = a // 2 → 1.0
a %= 2 # a = a % 2 → 1.0
a **= 3 # a = a ** 3 → 1.0
5. Bitwise Operators
A bitwise operator is used in programming to perform operations directly on the
binary (bit-level) representation of numbers.
These operators compare or manipulate numbers bit by bit (0s and 1s).
Work on bits (0s and 1s).
a = 6 # (110 in binary)
b = 3 # (011 in binary)
print(a & b) # 2 (AND → 110 & 011 = 010)
print(a | b) # 7 (OR → 110 | 011 = 111)
print(a ^ b) # 5 (XOR → 110 ^ 011 = 101)
print(~a) # -7 (NOT → inverts bits)
print(a << 1) # 12 (Left shift → 1100)
print(a >> 1) # 3 (Right shift → 011)
6. Membership Operators
A membership operator in Python is used to test whether a value or variable is a
member (present) in a sequence such as a string, list, tuple, dictionary, or set.
Used to test if a value exists in a sequence.
nums = [1, 2, 3, 4]
print(2 in nums) # True
print(5 not in nums) # True
7. Identity Operators
An identity operator in Python is used to compare the memory location
(identity) of two objects, not their values.
Used to compare memory locations (objects).
x = [1, 2]
y = [1, 2]
z = x
print(x is z) # True (same object in memory)
print(x is y) # False (different objects, even
if same values)
print(x is not y) # True
Q4. What is slicing operator hopw can you
extract a substring from a given string.
The slicing operator in Python is written as:
[start : stop : step]
• start → index where slice begins (default = 0)
• stop → index where slice ends (not included)
• step → jump/interval between elements (default = 1)
It can be used on strings, lists, tuples, etc.
Extracting a Substring from a String
Let’s take a string:
text = "PythonProgramming"
Examples:
1. Basic slicing
print(text[0:6]) # 'Python' (from index 0 to 5)
2. Omitting start or end
print(text[:6]) # 'Python' (default start = 0)
print(text[6:]) # 'Programming' (till end)
3. Using negative indexes
print(text[-11:-1]) # 'Programmin' (counting from
end)
4. Using step
print(text[0:12:2]) # 'Pto rgam' (every 2nd
character)
print(text[::-1]) # 'gnimmargorPnohtyP' (reverse
string)
Q5. Write a program to swap two numbers
using a temporary variables.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Before swapping: a =", a, " b =", b)
temp = a
a = b
b = temp
print("After swapping: a =", a, " b =", b)
output:
Enter first number: 5
Enter second number: 10
Before swapping: a = 5 b = 10
After swapping: a = 10 b = 5
Q6.Write a program thet prompts user to
enter two integers x and y and then.
x = int(input("Enter first integer (x): "))
y = int(input("Enter second integer (y): "))
print("\nYou entered: x =", x, "and y =", y)
print("Sum =", x + y)
print("Difference =", x - y)
print("Product =", x * y)
if y != 0:
print("Quotient =", x / y)
print("Floor Division =", x // y)
print("Remainder =", x % y)
else:
print("Division not possible (y = 0)")
Output:
Enter first integer (x): 15
Enter second integer (y): 4
You entered: x = 15 and y = 4
Sum = 19
Difference = 11
Product = 60
Quotient = 3.75
Floor Division = 3
Remainder = 3
Q7 Write a program to calculate the salary
of an employee given his basic pay define
HRA and TA as constants and calculate the
salary of an employee.
HRA = 2000
TA = 1500
basic_pay = float(input("Enter the basic pay of
employee: "))
salary = basic_pay + HRA + TA
print("Basic Pay:", basic_pay)
print("HRA:", HRA)
print("TA:", TA)
print("Total Salary of Employee:", salary)
output:
Enter the basic pay of employee: 10000
Basic Pay: 10000.0
HRA: 2000
TA: 1500
Total Salary of Employee: 13500.0
Q8. Write a program to print the ascii value
of a character.
ch = input("Enter a character: ")
ascii_value = ord(ch)
print("The ASCII value of", ch, "is", ascii_value)
output:
Enter a character: A
The ASCII value of A is 65
ruby
Copy code
Enter a character: $
The ASCII value of $ is 36
Q9. Write a program to calculate the no. Of
seconds in a day.
hours = 24
minutes = 60
seconds = 60
seconds_in_day = hours * minutes * seconds
print("Number of seconds in a day =", seconds_in_day)
Output:
Number of seconds in a day = 86400
Q10. Momentum is calculated E= mc^2 where m
is the mass c is the velocity write a
program that accepts objects mass and
velocity and display momentum.
mass = float(input("Enter mass of the object (kg):
"))
velocity = float(input("Enter velocity of the object
(m/s): "))
momentum = mass * velocity
print("Momentum of the object =", momentum, "kg·m/s")
output:
Enter mass of the object (kg): 10
Enter velocity of the object (m/s): 5
Momentum of the object = 50.0 kg·m/s