0% found this document useful (0 votes)
4 views5 pages

Python Notes

The document demonstrates basic data types in Python, including integers, floats, strings, booleans, lists, tuples, sets, and dictionaries. It also includes several Python programs for calculating distances between points, adding two numbers from command line arguments, checking if a number is even or odd, and printing decimal equivalents of fractions from 1/2 to 1/10. Each section provides code examples and explanations of how to use Python for these tasks.

Uploaded by

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

Python Notes

The document demonstrates basic data types in Python, including integers, floats, strings, booleans, lists, tuples, sets, and dictionaries. It also includes several Python programs for calculating distances between points, adding two numbers from command line arguments, checking if a number is even or odd, and printing decimal equivalents of fractions from 1/2 to 1/10. Each section provides code examples and explanations of how to use Python for these tasks.

Uploaded by

drneetugecjpr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

P-1 # Demonstrating Basic Data Types in Python

# Integer

num_int = 10

print("Integer value:", num_int)

print("Type:", type(num_int))

print()

# Float

num_float = 10.5

print("Float value:", num_float)

print("Type:", type(num_float))

print()

# String

text = "Hello, Python!"

print("String value:", text)

print("Type:", type(text))

print()

# Boolean

is_python_fun = True

print("Boolean value:", is_python_fun)

print("Type:", type(is_python_fun))

print()

# List

my_list = [1, 2, 3, "Python", 4.5]

print("List value:", my_list)


print("Type:", type(my_list))

print()

# Tuple

my_tuple = (1, 2, 3, "Python", 4.5)

print("Tuple value:", my_tuple)

print("Type:", type(my_tuple))

print()

# Set

my_set = {1, 2, 3, 3, 4}

print("Set value:", my_set)

print("Type:", type(my_set))

print()

# Dictionary

my_dict = {"name": "Alice", "age": 25}

print("Dictionary value:", my_dict)

print("Type:", type(my_dict))

print()

P-2 Write a program in python to compute distance between two points taking input from the user.

# Program to calculate distance between two points

import math

# Taking input from the user


x1 = float(input("Enter x-coordinate of first point: "))

y1 = float(input("Enter y-coordinate of first point: "))

x2 = float(input("Enter x-coordinate of second point: "))

y2 = float(input("Enter y-coordinate of second point: "))

# Calculating distance using distance formula

distance = [Link]((x2 - x1)**2 + (y2 - y1)**2)

# Displaying the result

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

P-3 Write a program [Link] that takes 2 numbers as command line arguments and prints its sum.

# [Link]

import sys

# Check if exactly two arguments are provided

if len([Link]) != 3:

print("Usage: python [Link] <num1> <num2>")

[Link](1)

# Convert command line arguments to float

num1 = float([Link][1])

num2 = float([Link][2])

# Calculate sum

result = num1 + num2


# Display result

print("Sum:", result)

P-4 Write a Program for checking whether the given number is an even number or not.

# Program to check whether a number is even or odd

# Taking input from the user

num = int(input("Enter a number: "))

# Checking if the number is even

if num % 2 == 0:

print(num, "is an Even number.")

else:

print(num, "is an Odd number.")

P-5 Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10

# Program to print decimal equivalents of 1/2 to 1/10

for i in range(2, 11):

value = 1 / i

print("1/{0} = {1}".format(i, value))

If you want the output rounded to 2 decimal places, you can use:

print("1/{0} = {1:.2f}".format(i, value))

format() is used to insert values into a string in a clean and organized way.

 "1/{0} = {1}" → This is a string with placeholders.

 {0} → Refers to the first value inside format()


 {1} → Refers to the second value inside format()

 .format(i, value) → Replaces {0} with i and {1} with value

You might also like