UNIT I [08 Marks]
Introduction and Syntax of Python Program
1. List the features of python and explain.
Ans:-
Easy to Learn and Use – Python has a simple and readable syntax, making it
beginner-friendly.
Interpreted Language – Python executes code line by line, making debugging easier.
Dynamically Typed – No need to declare variable types; Python determines the type
at runtime.
Platform-Independent – Python code can run on different operating systems
without modification.
Open-Source and Free – Available for free and has a large community supporting
development.
Extensive Libraries and Modules – Comes with a rich set of built-in libraries for
various applications.
Object-Oriented and Procedural Support – Supports both object-oriented and
procedural programming paradigms.
High-Level Language – Allows writing code in a human-readable format, reducing
complexity.
2. Enlist application of python programming.
Ans:-
Web Development (Django, Flask)
Data Science & Analytics (Pandas, NumPy)
Artificial Intelligence & Machine Learning (TensorFlow, PyTorch)
Automation & Scripting (Task automation)
Game Development (Pygame)
Cybersecurity & Ethical Hacking (Scapy, PyCrypto)
Networking & Cloud Computing (AWS, Paramiko)
Desktop Application Development (Tkinter, PyQt)
3. What do you mean by variable? Does python allow explicit declaration of
variables? Justify your answer.
Ans:-
What is a Variable?
A variable is a name that refers to a memory location where data is stored such as numbers,
strings, lists, etc
Does Python Allow Explicit Declaration of Variables?
No, Python does not allow explicit declaration of variables before assigning a value.
Justification (Simple Explanation):
1. No Need to Declare Type –Python understands it automatically when you assign a value.
x = 10 # No need to declare 'x' before using it
y = "Hello" # Python knows 'y' is a string
2. Type Can Change Anytime – You can store a number in a variable first, then change it to
a string without any error.
x = 10 # x is an integer
x = "Text" # Now x becomes a string
4. Explain python building block.
Ans:-
1. Variables and Data Types
Variables store values, and their type is determined automatically.
Data Types include integers (int), floating-point numbers (float), strings (str), lists (list),
tuples (tuple), dictionaries (dict), etc.
Example: name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
2. Operators
Used to perform operations on variables and values.
Types: Arithmetic (+, -, *, /), Comparison (==, !=, >, <), Logical (and, or, not), etc.
Example: a = 10, b = 5
print(a + b
print(a > b)
3. Control Flow Statements
Used for decision-making and loops.
Examples:
o Conditional Statements (if, elif, else)
num = 10
if num > 0:
print("Positive number")
o Loops (for, while)
for i in range(5):
print(i)
4. Functions
Blocks of reusable code that perform a specific task.
Example:
def greet(name):
print("Hello, " + name)
greet("Alice")
5. What is comment? How to apply comments in python?
Ans:-
What is a Comment?
A comment in Python is a line of text that is ignored by the Python interpreter.
Comments are used to explain the code and make it more readable. They do not
affect the program's execution.
How to Apply Comments in Python?
1. Single-line Comment – Starts with # and applies to one line.
Example:- print("Hello, World!") # This prints a message
2. Multi-line Comment (Using #) – Multiple lines with # at the beginning of
each line.
Example:- # Each line starts with #
print("Python is fun!")
3. Multi-line Comment (Using Triple Quotes ''' or """) – Used as a block of
text, often for documentation.
Example:-
""" This is a multi-line comment
It spans multiple lines """
print("Welcome to Python !")
6. Write different data-types in python with suitable example.
Ans:-
. Numeric Types
Used for storing numbers.
Integer (int) – Whole numbers (positive or negative).
x = 10 # Integer
Floating-point (float) – Numbers with decimals.
y = 10.5 # Float
Complex (complex) – Numbers with real and imaginary parts.
z = 3 + 4j # Complex number
2. Sequence Types
Used to store multiple values in order.
String (str) – Collection of characters (text).
name = "Python" # String
List (list) – Ordered, changeable collection of items.
fruits = ["Apple", "Banana", "Cherry"] # List
Tuple (tuple) – Ordered but unchangeable collection.
colors = ("Red", "Green", "Blue") # Tuple
3. Set Types
Used for storing unique values.
Set (set) – Unordered collection with unique elements.
numbers = {1, 2, 3, 3, 4} # Set (duplicates removed)
7. Describe bitwise operators in Python with example.
Ans:-
Bitwise AND (&)
Compares each bit of two numbers and returns 1 if both bits are 1, otherwise
returns 0.
Example:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a & b # Result: 0001 (1 in decimal)
print(result) # Output: 1
2. Bitwise OR (|)
Compares each bit of two numbers and returns 1 if at least one bit is 1,
otherwise returns 0.
Example:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a | b # Result: 0111 (7 in decimal)
print(result) # Output: 7
3. Bitwise XOR (^)
Compares each bit of two numbers and returns 1 if the bits are different,
otherwise returns 0.
Example:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a ^ b # Result: 0110 (6 in decimal)
print(result) # Output: 6
4. Bitwise NOT (~)
Inverts the bits of a number (flips 1 to 0 and 0 to 1).
Python uses two’s complement representation for negative numbers.
Example:
a = 5 # Binary: 0101
result = ~a # Result: -(5+1) = -6
print(result)
8. Explain membership and identity operators in Python.
Ans:-
ython has membership operators to check if a value is in a sequence and identity
operators to check if two variables refer to the same object.
1. Membership Operators (in, not in)
These check if a value exists in a list, tuple, or string.
in → Returns True if the value is present.
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
not in → Returns True if the value is NOT present.
numbers = [1, 2, 3]
print(4 not in numbers) # Output: True
2. Identity Operators (is, is not)
These check if two variables refer to the same object in memory.
is → Returns True if two variables refer to the same object.
a = [1, 2, 3]
b=a
print(a is b) # Output: True
is not → Returns True if two variables refer to different objects.
x = [10, 20, 30]
y = [10, 20, 30] # Different object with the same values
print(x is not y) # Output: True