1. discuss the concept of variables and data types in python.
how are variables
declared and assigned values? illustrate example?
Answer : Variables and Data Types in Python
1. Variables in Python
A variable is a name that refers to a value stored in memory. Variables are used to store data that
your program can manipulate.
In Python, variables are dynamically typed, which means you don’t need to declare the
type of a variable explicitly.
Python automatically determines the type of the variable based on the value assigned to
it.
Variable Declaration and Assignment
Variables are declared by assigning a value using the = operator.
x = 10 Integer
name = Alice String
i = 3.14 float
x= 2.8 double
with example
addition of two number
a=10
b=20
c=a+b
print(c)
2. explain for and while loop with example
Answer: Loops in Python: for and while
Loops allow you to execute a block of code multiple times. Python provides two main types of
loops: for and while.
🔁 1. for Loop
The for loop is used to iterate over a sequence like a list, string, or range.
Example 1: Loop through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Example 2: Using range()
for i in range(5): # 0 to 4
print(i)
2. while Loop
The while loop repeats as long as a condition is True
Example 1: Count to 5
count = 1
while count <= 5:
print(count)
count += 1
3. how do you call a function in phyton and pass argument to it?
explain with example
Answer: Calling a Function and Passing Arguments in Python
In Python, functions are used to group reusable blocks of code.
Calling a Function:
You call a function by writing its name followed by parentheses and passing values (called
arguments) inside the parentheses.
Example 1: Function with Parameters
Def add ():
a=eval;(input(“enter a number”))
b=eval;(input(“enter a number”))
c=a+b
print(c)
Example 2: Function with Multiple Parameters
def add_numbers(a, b):
c = a + b
print("Sum:", c)
add_numbers(5, 3)
add_numbers(10, 20)
Example 3: Function with Return Value
Deff (a,b)
C=a+b
Return
X=eval(input(“enter a number”))
Y=eval(input(“enter a number”))
Z=add(x,y)
Pprint(z)
4. how do you call a function in python and pass arguments to ithow
do you call a function in python and pass arguments to it? explain
with example
Answer What is a function?
A function is a block of code that performs a task. You define it once, and then you can call it
whenever you want.
Step-by-step:
Step 1: Define the function
You define it using the def keyword.
def say_hello(name):
print("Hello", name)
Here:
say_hello is the function name.
name is aarameter (a placeholder for the value you pass in)
Step 2: Call the function and pass an argument
"Alice" is the argument you're passing to the function.
Inside the function, name will now be
Full Example:
python
CopyEdit
def say_hello(name):
print("Hello", name)
Another Example: Function with Two Arguments
def add_numbers(a, b):
result = a + b
print("The sum is:", result
add_numbers(3, 5)
Another Example: Function with Two Arguments
def add_numbers(a, b):
result = a + b
print("The sum is:", result)
5. explain the process of creating a dictionary in python . how are
key-value pairs used to store data in a dictionary
Answer: A dictionary is a collection of key-value pairs. It’s used to
store data where each value is accessed using a key, not an index
(like in a list).
How to Create a Dictionary
🔹 Example 1: Basic Dictionary
python
CopyEdit
student = {
"name": "Alice",
"age": 20,
"grade": "A"
}
"name", "age", and "grade" are keys
"Alice", 20, and "A" are the values
Accessing Values Using Keys
python
print(student["name"])
print(student["age"])
You use the key inside square brackets [] to get the value.
Adding or Updating Key-Value Pairs
python
student["age"] = 21
student["subject"] = "Math"
print(student)
Output:
python
{'name': 'Alice', 'age': 21, 'grade': 'A', 'subject': 'Math'}
You cannot have two identical keys in a dictionary. The last one will overwrite the previous
one.
python
data = {
"id": 1,
"id": 2
}
print(data["id"])
6.demonstrate the concept of tuple in python . how are tuples similar
to and different from lists?
Answer What is a Tuple in Python?
A tuple is a collection of ordered, immutable items. This means:
The order of items matters.
You cannot change (modify, add, or remove) items once the tuple is created.
Example 1:
my_tuple = (1, 2, 3)
print(my_tuple)
Example 2: Tuple with Different Data Types
person = ("Alice", 25, "Engineer")
print(person)