Dhruv Python Lab File
Dhruv Python Lab File
Name : ______________
Roll Number : ______________
Subject : ______________
Course and Section : ______________
Semester and Year : ______________
Submitted to : ______________
Index
Programming constructs in
2
python -hands-on practice
Programming constructs in
3
python -hands-on practice
Introducing Python:
Python is a high-level, interpreted programming language known for its simplicity and
readability. It was created by Guido van Rossum and first released in 1991. Python
supports multiple programming paradigms, including procedural, object-oriented, and
functional programming styles, making it versatile and suitable for various applications.
The Python shell, also known as the Python interactive interpreter, provides an
interactive environment for executing Python code line by line. It allows users to quickly
test code snippets, explore language features, and debug programs interactively.
To access the Python shell, you typically open your command-line interface (such as
Terminal on macOS or Command Prompt on Windows) and type python or python3,
depending on your system configuration and installed Python versions. This launches the
Python interpreter, and you'll see a prompt (>>>) where you can start typing Python
code.
Setting up Anaconda, a popular Python distribution that includes a wide range of data
science and machine learning tools, is straightforward. Here's a step-by-step guide to
installing Anaconda:
Download Anaconda:
It's a good idea to update Anaconda after installation to ensure you have the latest
packages and bug fixes. You can do this by running the following command in your
terminal:
sql
Copy code
conda update anaconda
Install Anaconda Navigator (Optional but recommended):
You can create and manage Python environments using Conda. To create a new
environment, use the command conda create --name myenv where myenv is the name
of your environment.
You can activate your environment using conda activate myenv and deactivate it using
conda deactivate.
That's it! You've successfully installed Anaconda on your system. You can now start using
Anaconda's powerful tools and libraries for data science and machine learning.
3. Introduction to jupyter notebook.
Jupyter Notebook is an open-source web application that allows you to create and share
documents that contain live code, equations, visualizations, and narrative text. It's
widely used in data science, machine learning, scientific research, and education
because of its interactive and collaborative nature.
2. Code Execution: You can execute code cells individually, which allows for rapid
prototyping, testing, and debugging. The results of code execution (output, plots, etc.)
are displayed directly below the code cell.
5. Rich Outputs: Apart from textual output, Jupyter Notebook can display rich media
outputs, including HTML, images, videos, and interactive widgets. This feature enables
you to create dynamic and engaging presentations or reports.
6. Integration with External Tools: Jupyter Notebook supports integration with various
external tools and libraries, including version control systems like Git, data processing
libraries like Pandas, and machine learning frameworks like TensorFlow and PyTorch.
7. Shareability and Collaboration: You can share your Jupyter Notebooks with others by
exporting them to various formats like HTML, PDF, or slides. Additionally, platforms like
GitHub, JupyterHub, and Google Colab allow for collaborative editing and sharing of
notebooks.
8. Extensions and Customization: Jupyter Notebook offers a range of extensions and
customization options to enhance your workflow. You can install extensions for
additional functionalities, themes for visual customization, and keyboard shortcuts for
faster navigation.
Overall, Jupyter Notebook is a powerful tool for data analysis, exploration, visualization,
and communication.
Working with Jupyter Notebook can be a rewarding experience, especially for data
scientists, researchers, and educators. Here's a brief overview of how to get started:
Installation: If you haven't installed Jupyter Notebook yet, you can do so via pip
(Python's package manager). Simply run pip install notebook in your command line.
Launching Jupyter Notebook: After installation, you can start Jupyter Notebook by typing
jupyter notebook in your command line. This will launch a local server and open a new
tab or window in your default web browser.
Creating a New Notebook: Once Jupyter Notebook is open, you can create a new
notebook by clicking on the "New" button in the top right corner and selecting "Python
3" (or any other available kernels you may have installed).
Writing and Running Code: In a Jupyter Notebook, you'll see cells where you can write
and execute code. To run a cell, you can either click the "Run" button in the toolbar,
press Shift + Enter, or use other keyboard shortcuts like Ctrl + Enter (to run the current
cell without advancing) or Alt + Enter (to run the current cell and insert a new one
below).
Markdown Cells: Jupyter Notebook supports Markdown cells, which allow you to write
formatted text, add images, create lists, tables, etc. To create a Markdown cell, change
the cell type from "Code" to "Markdown" using the dropdown menu in the toolbar.
Saving and Exporting: Jupyter Notebooks are automatically saved periodically, but you
can also save manually by clicking on the floppy disk icon in the toolbar or by pressing
Ctrl + S. You can export your notebook to various formats such as HTML, PDF, or slides by
navigating to "File" -> "Download as" and selecting the desired format.
Other Features: Jupyter Notebook has many other features, such as the ability to install
and use different kernels (for languages other than Python), support for interactive
widgets, and integration with version control systems like Git.
Shutdown: Once you're done working with a notebook, it's a good practice to shut down
the notebook's kernel to free up system resources. You can do this by selecting "File" ->
"Close and Halt" for the specific notebook you're working on or by shutting down the
entire Jupyter Notebook server.
Jupyter Notebook's flexibility and interactivity make it a powerful tool for data analysis,
visualization, and prototyping code.
2. Even or odd:
3. Leap Year
4. Vowel or Consonants
7. Star pattern.
8. Fibonacci Series
9. Prime or not
Lab: 3
1) Swapping two numbers (Using third variable):
3) Sum of cubes:
4) If two variables are equal:
5) Smallest of three
7. Write a Python program to get a string made of the first 2 and the last 2 chars
from a given a string. If the string length is less than 2, return instead of the
empty string.
Lab: 5