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

Lab Assignment 2

Uploaded by

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

Lab Assignment 2

Uploaded by

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

"""Question 1: Write a program that asks the user to enter a string.

The program
should be in a Python file *.py.
The program should then print the following:
(a)The total number of characters in the string."""

name='Abu bakar Amin'


length=len(name)
print(length)

#(b) The string repeated 10 times


for _ in range(10):
print(name)
#(c) The first character of the string

first_character=name[0]
print(first_character)
#(d) The first three characters of the string

First_three_characters=name[0:3:1]
print(First_three_characters)
#(e) The last three characters of the string.

last_three_characters=name[-1:-4:-1]
print(last_three_characters)
#(f) The string backwards

reversed_name=name[::-1]
print(reversed_name)
#(g) The seventh character of the string

seventh_character=name[6]
print(seventh_character)

#(h) After removing first and last character from name

name_1=name[1:-1]
print(name_1)
#(i) The string in all uppercase

name.upper()
print(name.upper())
#(j) The string with every “a” replaced with an “e”

name_2=name.replace('a','e').replace('A','e')
print(name_2)
#(k) The string with every letter replaced by a “space”
"""Question 2: A simple way to estimate the number of words in a string is to count
the number of spaces in the
string. Write a program that asks the user for a string and returns an estimate of
how many words are in the
string"""

name='Abu bakar Amin'


space_count=name.count(' ')
print(space_count)

word_estimated=space_count+1
print(word_estimated)
"""Question 03: Write a program that asks the user for their name and how many
times to print it. The program
should print out the user’s name the specified number of times."""

#Numbers and Math


"""
Question 04: Create a program to calculate and print the following operations on a
list of ten numbers: the
mean, median, mode and standard deviation of the numbers."""

import statistics
#Mean data
data=[1,4,7,9,11,14,16,19,23,26]
mean_data=sum(data)/len(data)
len(data)
print(mean_data)

#Median
data=[1,6,9,12,14,16,19,23,27,29]
print(statistics.median(data))
#Mode
data=[1,3,5,7,9,11,13,15,17,19]
print(statistics.mode(data))
#Standard deviation
data=[1,3,7,11,15,20,23,29,33,38]
std_dev=statistics.stdev(data)
print(std_dev)

"""Question 05: Write a program that asks the user for a weight in kilograms and
converts it to pounds. There are
2.2 pounds in a kilogram."""

weight=int(input('enter your weight:'))


unit=input('Kilograms (kg):')
if unit=='kg':
weight=weight*2.205
unit='lb'
print('your weight in lb:{weight}{}')
else:
print('unit is not valid')
"""Question 06: Write a program that converts time from one time zone to another.
The user enters the “time” in
the usual American way, such as 3:48pm or 11:26am. The “first time zone” the user
enters is that of the original
time and the “second time zone” is the desired time zone. The possible time zones
are Eastern, Central,
Mountain, or Pacific."""
def convert_time(time, from_zone, to_zone):

period = time[-2:]
time = time[:-2]
hours, minutes = map(int, time.split(":"))

if period.lower() == "pm" and hours != 12:


hours += 12
elif period.lower() == "am" and hours == 12:
hours = 0

time_zone_offsets = {
"eastern": 0,
"central": -1,
"mountain": -2,
"pacific": -3
}

if from_zone.lower() in time_zone_offsets and to_zone.lower() in


time_zone_offsets:
offset_difference = time_zone_offsets[to_zone.lower()] -
time_zone_offsets[from_zone.lower()]
else:
return "Invalid time zone"

new_hours = hours + offset_difference

if new_hours < 0:
new_hours += 24
elif new_hours >= 24:
new_hours -= 24

new_period = "AM"
if new_hours >= 12:
new_period = "PM"
if new_hours > 12:
new_hours -= 12
elif new_hours == 0:
new_hours = 12
new_time = f"{new_hours}:{minutes:02d}{new_period}"
return new_time

time = input("Enter time (e.g., 3:48pm): ")


from_zone = input("Enter the first time zone (eastern, central, mountain, pacific):
")
to_zone = input("Enter the second time zone (eastern, central, mountain, pacific):
")

converted_time = convert_time(time, from_zone, to_zone)


print("Converted time:", converted_time)

#Functions and Loops

""" Question 07: Write a program that defines a function to calculate the factorial
of a number. The program
should:
1. Take a positive integer input from the user.
2. Use a loop to calculate the factorial.
3. Print the result."""

def factoraial(n):
result=1
for i in range(1,n+1):
result *= i
return result

n=int(input('enter positive integer:'))


if n<0:
print('enter positive integer.')
else:
print(f'the factorial of {number} is{factorial(n)}')

"""
Question 08: The Fibonacci numbers are the sequence below, where the first two
numbers are 1, and each
number thereafter is the sum of the two preceding numbers. Write a program that
asks the user how many
Fibonacci numbers to print and then prints that many.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . """

def fib(n):
print('enter number from user:')

a=0
b=1

if n==1:
print(a)

else:
print(a)
print(b)

for i in range(2,n):
c=a+b
a=b
b=c
print(c)

fib(11)

"""Question 09: Use for loops to print a diamond like the one below. The user must
specify as input, how high the
diamond should be."""

height = int(input("Enter the height of the diamond (number of rows for the top
half): "))

for i in range(height):
print(" " * (height - i - 1) + "*" * (2 * i + 1))

for i in range(height - 2, -1, -1):


print(" " * (height - i - 1) + "*" * (2 * i + 1))

You might also like