fbpx

A.)# Program to swap two numbers without using a temporary variable
a = 5
b = 10

# Swapping the values
a = a + b # a becomes 15
b = a – b # b becomes 5 (original value of a)
a = a – b # a becomes 10 (original value of b)

print(“After swapping:”)
print(“a =”, a)
print(“b =”, b)

B)# Program to find the length of a string without using any library functions
string = “Hello, World!”
length = 0

for char in string:
length += 1

print(“Length of the string:”, length)

C)# Program to add a new key-value pair to an existing dictionary
my_dict = {‘name’: ‘Alice’, ‘age’: 25}

# Adding a new key-value pair
my_dict[‘city’] = ‘New York’

print(“Updated dictionary:”, my_dict)

E)x = 5
x += 3 # x = x + 3
print(“After +=:”, x) # Output: 8

x -= 2 # x = x – 2
print(“After -=:”, x) # Output: 6

F)

a = True
b = False

print(“a and b:”, a and b) # Output: False
print(“a or b:”, a or b) # Output: True
print(“not a:”, not a) # Output: False

G)

num = 10
if num % 2 == 0:
print(“Even number”)
else:
print(“Odd number”)

H)

score = 85

if score >= 90:
print(“Grade: A”)
elif score >= 80:
print(“Grade: B”)
elif score >= 70:
print(“Grade: C”)
else:
print(“Grade: D”)

 

I)

for i in range(1, 10):
if i == 5:
break # Exits the loop when i is 5
print(i)

1. History of Python

Python was created by Guido van Rossum and first released in 1991. It was designed to be a simple and easy-to-read programming language. Python’s development was influenced by languages like ABC, and it emphasized code readability and simplicity. Today, Python is one of the most popular programming languages, widely used in web development, data science, automation, and more.

2. How Many Ways Can We Execute Python?

Python can be executed in several ways:

  • Interactive Mode: Using the Python interpreter in a terminal or command prompt.
  • Script Mode: Running a .py file by calling python filename.py in the terminal.
  • Integrated Development Environments (IDEs): Like PyCharm, VSCode, or Jupyter Notebook, which provide an environment to write, test, and debug Python code.
  • Web-based Notebooks: Such as Jupyter Notebook, Google Colab, which allow for writing and executing Python code in a web browser.
  • Embedded in Other Software: Python can be embedded within applications or automation systems.

3. Define Dictionary

A dictionary in Python is a data structure that stores key-value pairs. It is mutable, unordered (until Python 3.7), and indexed by unique keys, which means each key maps to a specific value. Here’s an example:

python
# Dictionary example student = { "name": "John", "age":
Scroll to Top