Lab Assignment 2
Lab Assignment 2
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."""
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)
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"""
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."""
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."""
period = time[-2:]
time = time[:-2]
hours, minutes = map(int, time.split(":"))
time_zone_offsets = {
"eastern": 0,
"central": -1,
"mountain": -2,
"pacific": -3
}
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
""" 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
"""
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))