0% found this document useful (0 votes)
19 views6 pages

Week 1

The document provides instructions on how to perform various tasks in Python like visiting the official Python website, using the Python interpreter as a calculator, writing programs to calculate compound interest and distance between two points, and reading/printing a person's details. Code snippets and step-by-step explanations are given for each task.

Uploaded by

2303c50245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Week 1

The document provides instructions on how to perform various tasks in Python like visiting the official Python website, using the Python interpreter as a calculator, writing programs to calculate compound interest and distance between two points, and reading/printing a person's details. Code snippets and step-by-step explanations are given for each task.

Uploaded by

2303c50245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WEEK - I

1 i). Use a web browser to go to the Python website http://python.org. This page contains information

about Python and links to Python-related pages, and it gives you the ability to search the Python

documentation.

Open a Web Browser: Use any web browser you prefer (e.g., Chrome, Firefox, Safari).

Go to the Python Website: Enter the URL https://www.python.org into your browser's address bar and

press Enter. This will take you to the homepage of the official Python website.

Explore the Website: The Python website is designed to be user-friendly and contains a wealth of

information about Python, including recent news, upcoming events, and resources for learning

Python.

Access Python Documentation: On the top navigation bar, there's a link labeled "Docs." Hovering over

this link will reveal a drop-down menu that provides direct access to Python's documentation for

various versions. Click on the version of the documentation you wish to view.

Use the Search Feature: If you're looking for specific information within the Python documentation,

you can use the search bar typically located at the top of the documentation page. Enter your query

into the search bar and press Enter to search the Python documentation.

1 ii). Start the Python interpreter and type help() to start the online help utility.

To start the Python interpreter and access the online help utility, you'll need to follow these steps:

Open Your Terminal or Command Prompt:

On Windows, you can search for "cmd" or "Command Prompt" in the Start menu.

On macOS or Linux, you can open the "Terminal" application.


Start the Python Interpreter: Type python or python3 (depending on your installation) and press

Enter. This command starts the Python interpreter in interactive mode.

Access the Help Utility: Once the Python interpreter starts (you'll see the >>> prompt), type help()

and press Enter. This command launches Python's built-in help system.

>>>help()

Using the Help System:

After typing help(), you'll enter the interactive help utility. Here, you can type the name of any Python

module, keyword, or topic to get more information. For example, you can type keywords to see a list

of Python's reserved keywords.

To exit the help utility, type quit and press Enter.

Here's a quick example of starting the Python interpreter and accessing the help utility:

>>>python3 # or python, depending on your systemhelp()

And then within the help utility:

keywords # To see a list of Python's keywords

To quit the help system, simply type:

quit

This interactive help utility is a powerful resource for learning and exploring Python's extensive

features and modules.

2. Start a Python interpreter and use it as a Calculator.

To use the Python interpreter as a calculator, follow these steps:

Open Your Terminal or Command Prompt:

On Windows, search for "cmd" or "Command Prompt" in the Start menu.


On macOS or Linux, open the "Terminal" application.
Start the Python Interpreter: Type python or python3 (depending on your Python installation)
and press Enter. This command initiates the Python interpreter in interactive mode.

python
or
python3

Perform Calculations: Once the Python interpreter starts and you see the >>> prompt, you can
start performing calculations. The interpreter can handle basic arithmetic (addition, subtraction,

multiplication, division) as well as more complex operations (exponentiation, modulo, etc.).

Here are some examples of calculations you can perform:

Addition: >>> 2 + 3 (Outputs 5)

Subtraction: >>> 5 - 2 (Outputs 3)

Multiplication: >>> 2 * 3 (Outputs 6)

Division: >>> 10 / 2 (Outputs 5.0)


Exponentiation: >>> 2 ** 3 (Outputs 8) (This raises 2 to the power of 3)

Modulo: >>> 10 % 3 (Outputs 1) (This calculates the remainder of 10 divided by 3)

To exit the Python interpreter, you can type exit() or press Ctrl+D (on Unix-based systems) or Ctrl+Z

followed by Enter (on Windows).

exit()

3 i). Write a program to calculate compound interest when principal, rate and number of periods are

given.

To calculate compound interest in Python when the principal amount, interest rate, and number of

periods are given, you can use the following program:

def calculate_compound_interest(principal, rate, periods):


# Formula for compound interest:A = P(1 + r/n)^(nt)

# A is the amount of money accumulated after n years, including interest.

# P is the principal amount (the initial amount of money).

# r is the annual interest rate (in decimal).

# n is the number of times that interest is compounded per unit t.

# t is the time the money is invested for, in years.

# Calculate compound interest

amount = principal * (1 + rate / periods) ** (periods * 1)

# Subtract the principal amount to get the interest alone

interest = amount - principal

return interest

# Input principal, rate, and periods from the user

principal = float(input("Enter the principal amount: "))

rate = float(input("Enter the annual interest rate (in decimal): "))

periods = int(input("Enter the number of periods (compounding frequency): "))

# Call the function to calculate compound interest

compound_interest = calculate_compound_interest(principal, rate, periods)

# Print the compound interestprint("Compound interest:", compound_interest)

This program defines a function calculate_compound_interest that takes the principal amount, annual

interest rate, and number of periods as input and calculates the compound interest using the provided
formula. Then, it prompts the user to input the principal, rate, and periods, calculates the compound

interest using the function, and prints the result.

3 ii). Given coordinates (x1, y1), (x2, y2) find the distance between two point.

To calculate the distance between two points given their coordinates ((x1, y1)) and ((x2, y2)), you can

use the distance formula derived from the Pythagorean theorem. The formula to calculate the distance

(d) between two points in a plane is:

[ d = \sqrt{(x2 - x1)^2 + (y2 - y1)^2} ]

Here's a Python program to calculate the distance between two points:

import math

def calculate_distance(x1, y1, x2, y2):

# Calculate the distance using the distance formula

distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

return distance

# Input coordinates of the two points

x1 = float(input("Enter x1: "))

y1 = float(input("Enter y1: "))

x2 = float(input("Enter x2: "))

y2 = float(input("Enter y2: "))

# Call the function and print the distance

distance = calculate_distance(x1, y1, x2, y2)

print("The distance between the two points is:", distance)


This program defines a function calculate_distance that takes coordinates of two points as

input and calculates the distance using the distance formula. Then, it prompts the user to input the

coordinates of the two points and prints the calculated distance.

4. Read name, address, email and phone number of a person through keyboard and print the details.

To read a person's name, address, email, and phone number from the keyboard and print these

details, you can use the following Python program:

# Input details from the user

name = input("Enter your name: ")

address = input("Enter your address: ")

email = input("Enter your email: ")

phone_number = input("Enter your phone number: ")

# Print the details

print("\nPerson Details:")

print("Name:", name)

print("Address:", address)

print("Email:", email)

print("Phone Number:", phone_number)

This program uses the input function to prompt the user for their name, address, email, and

phone number. After the details are entered, it prints them out.

You might also like