1.Lab Activity
1.Lab Activity
01
Introduction of Python & Installation of Anaconda/ Spyder Software
Objectives:
Introduction to Python
Introduction of Anaconda / Spyder Software
Installation of Anaconda / Spyder Software.
Equipment Required:
Anaconda/ Spyder Software
Computer
Theory:
1. What is Anaconda?
Anaconda is an open-source distribution of the Python and R programming languages used
primarily for scientific computing, data science, machine learning, and data analysis. It simplifies
package management and deployment by bundling together many useful libraries and tools,
making it easy to set up a development environment.
Key features of Anaconda:
Package Manager: Uses Conda, a package manager that helps manage and install
libraries and dependencies.
Pre-installed Libraries: Includes libraries like NumPy, pandas, Matplotlib, SciPy, and
many more, making it convenient for data science and machine learning.
Environment Management: Allows users to create isolated environments for different
projects to avoid dependency conflicts.
Jupyter Notebooks: Comes with Jupyter Notebooks for interactive Python
programming.
2. What is Spyder?
Spyder (Scientific Python Development Environment) is an open-source Integrated
Development Environment (IDE) included with Anaconda. It is designed specifically for data
science and scientific computing, offering features tailored for those needs.
Key features of Spyder:
Editor: A powerful editor for writing Python code with syntax highlighting,
autocompletion, and linting.
IPython Console: Provides an interactive console to run Python code and view outputs
immediately, which is useful for debugging and experimentation.
Variable Explorer: Displays variables and their values, making it easy to monitor data
during execution, similar to MATLAB.
Spyder is part of the Anaconda distribution, but it can also be installed separately. It’s
particularly useful for data analysis and scientific computing tasks due to its user-friendly
interface.
The Spyder console is an essential feature of the Spyder IDE that allows you to interact with
Python code in real-time. There are two main types of consoles available in Spyder: the
IPython Console and the Python Console. The IPython Console is more commonly used due
to its enhanced features and interactivity.
Here’s a detailed explanation of key aspects of the Spyder Console:
1. IPython Console:
Interactive Environment: The IPython console in Spyder provides an interactive
environment where you can execute Python code, inspect variables, and see results
immediately. It’s similar to a command line but integrated into the Spyder IDE with
additional features.
Code Execution: You can run individual lines, blocks, or entire scripts directly in the
console. Code can be executed by typing it directly in the console or by sending it from
the editor.
o To run code from the editor, you can select a line or block and press Shift+Enter
(to run the selection) or F5 (to run the entire script).
Autocompletion and Help: The IPython console provides autocompletion suggestions as
you type, making it easier to explore libraries and functions. You can also access
documentation by typing a function name followed by a question mark (?).
2. Variable Explorer Integration:
The IPython console is integrated with the Variable Explorer in Spyder. As you execute
code in the console, variables created in your session are automatically displayed in the
Variable Explorer. This lets you easily inspect the type, size, and values of the variables
without needing to print them manually in the console.
3. History Log:
The console keeps a history log of all commands you’ve executed, which can be
accessed by pressing the up/down arrow keys or viewing the complete history through
the Ctrl+R shortcut. This feature is useful for quickly re-executing previous commands
without rewriting them.
Example Workflow:
Write a piece of code in the Spyder editor.
Select the code and press Shift+Enter to send it to the console.
The console executes the code, and results are displayed immediately.
Inspect variables in the Variable Explorer.
If needed, adjust the code and rerun it without restarting the console.
Task.01
Write the following program in Spyder and execute the output to understand the operations of
Spyder.
# Ask the user for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Add the two numbers sum = num1 + num2
# Display the result print(f"The sum of {num1} and {num2} is: {sum}")
Procedure:
How to Run in Spyder:
1. Open Spyder.
2. Create a New Script (File → New File).
3. Copy the Code into the editor.
4. Run the Script by pressing F5.
5. Interact with the Console: Enter two numbers when prompted, and see the result
displayed.
This program introduces basic concepts such as:
Taking user input.
Performing addition.
Displaying results using print().
Task.02
Write the following program in Spyder and execute the output to understand the operations of
Spyder.
Task.03
Write the following program in Spyder and execute the output to understand the operations of
Spyder.
# Calculator functions
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
# User input
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Perform calculation
if choice == '1':
print(f"The result is: {add(num1, num2)}")
elif choice == '2':
print(f"The result is: {subtract(num1, num2)}")
elif choice == '3':
print(f"The result is: {multiply(num1, num2)}")
elif choice == '4':
if num2 != 0:
print(f"The result is: {divide(num1, num2)}")
else: print("Error! Division by zero.")
else: print("Invalid input")
Task.04
Write the following program in Spyder and execute the output to understand the operations of
Spyder.
import random
# Generate a random number between 1 and 10
random_number = random.randint(1, 10)
else:
print(f"Sorry, the correct number was {random_number}. Better luck next time!")
Activity Name
Group No.