0% found this document useful (0 votes)
3 views7 pages

Python Programs

The document contains various Python programming exercises, including printing 'Hello, world!', using different data types like numbers, strings, tuples, lists, and dictionaries, and performing arithmetic operations with type conversions. It also includes programs for currency conversion, data size conversions, calculating area and perimeter, determining odd/even numbers, finding the greatest of three numbers, and generating multiplication tables and Fibonacci sequences. Overall, it serves as a comprehensive guide for beginners to practice basic Python programming concepts.

Uploaded by

tappafiza
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)
3 views7 pages

Python Programs

The document contains various Python programming exercises, including printing 'Hello, world!', using different data types like numbers, strings, tuples, lists, and dictionaries, and performing arithmetic operations with type conversions. It also includes programs for currency conversion, data size conversions, calculating area and perimeter, determining odd/even numbers, finding the greatest of three numbers, and generating multiplication tables and Fibonacci sequences. Overall, it serves as a comprehensive guide for beginners to practice basic Python programming concepts.

Uploaded by

tappafiza
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
You are on page 1/ 7

1.

Write and execute simple python program


code
# This program prints Hello, world!
print('Hello, world!')
2.Write /execute simple python program:devlop minimum 2 programs using different
datatypes(numbers,string,tuple,list and dictionary)
Code
#numbers
Print (23455)
Print(23.12)

#strings
print("Hello")
print('Hello')

#ex2

x=”python”
print(x)

#Ex 3
x = ” Strings in python are surrounded by either single quotation marks, or double quotation
marks.”
Print(x)

#tuples
numbers = (20, -12, 8)
print(numbers)

# tuple of string types


names = ('python', 'program', 'language')
print (names)

# tuple of float types


float_values = (1.2, 3.4, 2.1)
print(float_values)

# tuple including string and integer


mixed_tuple = (2, 'Hello', 'Python')
print(mixed_tuple)

# Output: (2, 'Hello', 'Python')

#Example of index
languages=('Python', 'programming', 5)
# access the first item
print(languages[0])
# access the third item
print(languages[2])

list
# a list of three elements
ages = [19, 26, 29]
print(ages)

# Output: [19, 26, 29]

Ex2
lan = [‘python’, ‘c’, ‘java’]
print(lan)
Dictionaries
# creating a dictionary
teams = {
"virat": "rcb",
"rohith": "mumbai",
"gill": "gt"
}
# printing the dictionary
print(teams)

3.write/execute simple python program:devlop minimum 2 programs using different arithmetic


operators, exhibiting data type conversion
Code
#addition (+)
x=10
y=6
Addition=x+y
print(x+y)
#subtraction (-)
x=10
y=6
sub=x-y
print(x-y)
#multiplication (*)
x=10
y=6
Multiplication=x*y
Print(x*y)

#division (/)
x=10
y=6
Division=x/y
Print(x/y)

#floor division
x=10
y=6
Fllordivision=x//y
Print(x//y)
#modulus
x=10
y=6
Modulus=x%y
Print(x%y)

#type conversions

#integer to float
Num=4
num_float=float (num)
print (num_float)
#float to integer
num=9.0
num_int=int (num)
print (num_int)

#string to integer
str="9"
str_int=int (str)
print (str_int)

#str to float
str="9.0"
str_float=float (str)
print (str_float)

4. (i)write/execute simple python program to convert u.s dollors to Indian rupees.


Code:

usd=float(input("enter currency in usd:"))


inr=usd*83.36
print("the currency inr is”, round(inr,2))
(ii) write/execute simple python program to converts bits to megabytes ,gigabytes and terabytes.
Code
bits = int(input("Enter the number of bits: "))
# Convert to megabytes
megabytes = bits / (1024 * 1024 * 8)
print("Megabytes:", megabytes)
# Convert to gigabytes
gigabytes = bits / (1024 * 1024 * 1024 * 8)
print("Gigabytes:", gigabytes)
# Convert to terabytes
terabytes = bits / (1024 * 1024 * 1024 * 1024 * 8)
print("Terabytes:", terabytes)

Byte:- A byte is a unit of data memory equal to eight bits, depending on whether it requires error
correction. One byte is equivalent to eight bits. A byte is a unit most computers use to describe a
character such as a number, letter, or typographic symbol. 1 Byte = 8 bits.
KiloByte (KB):- A KiloByte is a unit of computer information or memory equivalent to 1024 bytes. 1
KB = 1024 bytes.
Megabyte (MB):- A Megabyte is a unit of computer information or memory equivalent to 1,048,576
bytes. 1 MB = 1024 KB = 1024 * 1024 bytes = 1,048,576 bytes.
Gigabyte (GB):- A Gigabyte is a unit of computer information or memory that is approximately
equivalent to 1 billion bytes. 1 GB = 1024 MB = 1024 * 1024 * 1024 bytes = 1,07,37,41,824 bytes.
Terabyte (TB):- A Terabyte is a unit of computer information or memory that is approximately
equivalent to about 1 trillion bytes. 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024 bytes =
1.099511628×10¹² bytes.
Now let us see how we can convert Bytes to KB, MB, GB, and TB in Python programming language.
Below we have developed separate programs for these conversions and also for Python code to convert
bytes to GB.
//To find Area and Perimeter of a Square
side = int (input ("Enter the side of a square: " ))
area = side*side #Formula for Area of square
perimeter = 4*side #Formula for Perimeter of square
print("Area of a square : ",area)
print("Perimeter of a square : ",perimeter)

import math
def cone_vol(r, h):
#calculating the volume
vol = (1/3)*(math.pi)*r*r*h
print("Volume: ", str(vol))
l=6
h=5
r=2
cone_vol(r, h)

6. i)Write a program to determine: whether a given number is odd or even


ii)find the greatest of the three numbers using conditional operators.
#To check whether the given number is Even or Odd
a = int(input("Enter a number: "))
if a%2 == 0:
print("Number is Even")
else:
print("Number is Odd")

#To find out the Biggest of given three Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= b:
print("Biggest number is: ",a)
elif b >= a and b >= c:
print("Biggest number is: ",b)
else:
print("Biggest number is: ", c)

#Multiplication table in Python using functions


def multiply(num,count):
return num * count
n = int(input("Enter any Number :"));
i=1
for i in range(1,11):
print(n," * ",i," = ",multiply(n,i))
i=i+1
Python Program to Find Factorial Of Number Using Recursion
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(4))
Python Program to Display Fibonacci Sequence Using Recursion
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))

You might also like