Python Paper 2
a) Define a class. How is class members accessed in Python?
class MyClass:
class_variable = "I am a class variable"
print(MyClass.class_variable) # Access using class name
obj = MyClass()
print(obj.class_variable) # Access using an instance
-----------------------------------------------------------------------------------------------------------------------------
b ). Explain the utility of assert statement
def divide(a, b):
assert b != 0, "Denominator must not be zero"
return a / b
result = divide(10, 2) # Works fine
result = divide(10, 0) # Raises AssertionError: Denominator must not be zero
---------------------------------------------------------------------------------------------------------------------------
2) Explain the steps of installing Python. Discuss the features and limitations of Python
programming language.
Steps to Install Python
To install Python on your computer, follow these steps:
1. Download Python Installer
- Go to the official Python website: https://www.python.org/.
- Navigate to the Downloads section.
- Select the appropriate installer for your operating system:
- Windows:
.exe
file - macOS:
.pkg
file - Linux: Most Linux distributions come with Python pre-installed, but you can download updated versions if needed.
- Windows:
2. Install Python on Your System
-
For Windows:
- Run the downloaded
.exe
file. - In the installation wizard, check the box "Add Python to PATH" (important for accessing Python via the command line).
- Click "Install Now" or choose "Customize Installation" for advanced settings.
- Once the installation is complete, verify it by running
python --version
orpython
in Command Prompt.
- Run the downloaded
-
For macOS:
- Open the downloaded
.pkg
file and follow the installation prompts. - Verify the installation by running
python3 --version
in the terminal.
- Open the downloaded
-
For Linux:
- Install Python using the package manager:
sudo apt update sudo apt install python3
- Verify the installation:
python3 --version
- Install Python using the package manager:
3. Verify Installation
- Open a terminal or command prompt.
- Type
python
orpython3
to start the Python interpreter. - Alternatively, run
python --version
orpython3 --version
to check the installed version.
4. Install a Code Editor or IDE (Optional)
- While Python can be written in any text editor, using an IDE or code editor improves productivity. Popular options include:
- PyCharm
- Visual Studio Code
- Jupyter Notebook
- IDLE (comes pre-installed with Python)
Comments
Post a Comment