Mca306 A1 PDF
Mca306 A1 PDF
NAME – Md Shadab
ROLL NO. -2723760002
1. Install Python
1. Download Python:
- Go to the official [Python
website](https://www.python.org/downloads/).
- Choose the appropriate version for your operating system
(Windows, macOS, or Linux).
- For most users, the latest stable version (e.g., Python 3.x) is
recommended.
2. Install Python:
Windows:
- Run the installer and check the box "Add Python to PATH" before
clicking Install Now.
- This ensures Python is accessible from the command line.
-macOS/Linux:
- Python is usually pre-installed, but you can install the latest version
using a package manager (like `brew` for macOS or `apt` for Linux).
- On macOS: `brew install python3`
- On Linux (Debian/Ubuntu): `sudo apt install python3`
3. Verify Installation:
- Open the terminal (or Command Prompt on Windows) and run:
```bash
python --version
```
You should see the installed version of Python.
1. Download PyCharm:
- Go to the [PyCharm download
page](https://www.jetbrains.com/pycharm/download/).
- You can choose between the Professional version (paid) or the
Community version (free). For most basic Python development, the
Community edition is sufficient.
2. Install PyCharm:
- Follow the installer instructions for your operating system:
- Windows: Run the `.exe` installer.
- macOS: Open the `.dmg` file and drag PyCharm into your
Applications folder.
- Linux: Extract the tarball and follow the setup instructions.
3. Launch PyCharm:
- After installation, launch PyCharm.
- When first opening PyCharm, it may ask you to configure some
settings (e.g., theme, plugins). You can use the default settings.
---
You now have Python and PyCharm set up and have tested a basic
program in both interactive and script modes.
Q2: Write a Program to perform basic functions of a calculator
using functions.
ANS: Python program to implement a basic calculator with functions
for addition, subtraction, multiplication, and division. The program will
prompt the user to input two numbers and then select an operation
(addition, subtraction, multiplication, or division).
```python
Function to add two numbers
def add(x, y):
return x + y
Main program
def calculator():
while True:
Display the menu
display_menu()
1. Functions:
- `add(x, y)`: Returns the sum of `x` and `y`.
- `subtract(x, y)`: Returns the difference between `x` and `y`.
- `multiply(x, y)`: Returns the product of `x` and `y`.
- `divide(x, y)`: Returns the quotient of `x` divided by `y`, with an error
message if division by zero is attempted.
3. Menu Display:
- `display_menu()` function shows the list of operations available to
the user.
4. Input Validation:
- If the user enters invalid input (like a string when a number is
expected), the program will prompt them again for valid input.
Sample Output:
```
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 1
Enter first number: 10
Enter second number: 5
10.0 + 5.0 = 15.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 4
Enter first number: 10
Enter second number: 2
10.0 / 2.0 = 5.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 5
Exiting the calculator.
```
Features:
- Error handling for division by zero.
- Input validation to ensure the user enters valid numerical values.
- Ability to repeat calculations until the user chooses to exit.
Q3: Write….
a program to demonstrate the following string functions:
Python Program:
```python
Function to print odd length words in a string
def print_odd_length_words(string):
words = string.split() Split the string into words
print("Odd length words are:")
for word in words:
if len(word) % 2 != 0:
print(word)
Main program
if __name__ == "__main__":
input_string = input("Enter a string: ")
Remove duplicates
no_duplicates = remove_duplicates(input_string)
print("\nString after removing duplicates:", no_duplicates)
Convert case
converted_case = convert_case(input_string)
print("\nString after converting case:", converted_case)
Calculate the length of the string
string_length = calculate_length(input_string)
print("\nLength of the string:", string_length)
```
1. `print_odd_length_words(string)`:
- Splits the input string into words using `split()`.
- Iterates through each word and checks if its length is odd (using
`len(word) % 2 != 0`).
- Prints all words with an odd length.
2. `remove_duplicates(string)`:
- Converts the string to a `set`, which automatically removes any
duplicate characters (since sets do not allow duplicates).
- Then, it joins the characters back into a string using `''.join()` and
returns it.
3. `convert_case(string)`:
- Uses the `swapcase()` method to convert each lowercase letter to
uppercase and vice versa in the string.
4. `calculate_length(string)`:
- Uses Python's built-in `len()` function to return the length of the
string.
Sample Output:
Input:
```
Enter a string: Hello world from Python
```
Output:
```
Odd length words are:
world
from
Python
2. Removing duplicates:
- The program removes any duplicate characters in the string. For
example, "Hello" becomes "Helo".
3. Converting case:
- It swaps the case of each character in the string. For example,
"Hello" becomes "hELLO".
Python Function:
```python
def longest_word(words):
Check if the list is empty
if not words:
return None, 0 Return None and length 0 if the list is empty
Example usage
words_list = ["apple", "banana", "cherry", "pineapple", "kiwi"]
longest, length = longest_word(words_list)
print(f"The longest word is: {longest}")
print(f"The length of the longest word is: {length}")
```
Explanation:
1. Input:
- The function `longest_word` takes a list of words as input.
Example Usage:
For the list `["apple", "banana", "cherry", "pineapple", "kiwi"]`, the
output would be:
```
The longest word is: pineapple
The length of the longest word is: 9
```
- If the input list is empty (`[]`), the function will return `None` and `0`,
indicating there is no word in the list.
Q5: Here's a Python program that creates a list and counts the
occurrence of a specific element in that list:
ANS:
Python Program:
```python
Function to count the occurrence of an element in the list
def count_occurrence(lst, element):
return lst.count(element)
Example usage
if __name__ == "__main__":
Create a list of elements
my_list = [1, 2, 3, 4, 2, 5, 6, 2, 7]
Explanation:
2. Example Usage:
- The program first creates a list of numbers, `my_list = [1, 2, 3, 4, 2,
5, 6, 2, 7]`.
- It then prompts the user to input an element for which to count the
occurrences.
- The function `count_occurrence` is called with the list and the input
element, and the count is printed.
Sample Output:
Input:
```
Enter the element to count its occurrence: 2
```
Output:
```
The element 2 occurs 3 times in the list.
Edge Case Considerations:
- If the element is not found in the list, the `.count()` method will return
`0`, indicating that the element doesn't appear in the list.