In: Computer Science
Python:
IDLE exercise
1. Start by running, in the shell, the following assignment statement:
s1 = 'abcdefghijklmnopqrstuvwxyz'
Now write expressions—on separate lines of code—using sting s1 and the indexing operator that evaluate to 'm', 'p', 'z', 'e', 'a'.
2. Write Python statements that correspond to the following:
a. assign to variable flowers a list containing strings 'rose', 'bougainvillea', 'yucca', 'marigold', 'daylily', and 'lily of the valley'.
b. Write Boolean expressions that evaluate to True if the string ‘carrot’ is in the list of flowers, and evaluate the expression.
c. Assign to list thorny the sublist consisting of the three objects in the list flowers.
d. Assign to list poisonous the sublist consisting of just the last object of list flowers.
e. Assign to list dangerous the concatenation of lists thorny and poisonous.
Programming Problems
For the following two problems, create a module called a1.py and write the code for the following programs in it. Do NOT forget the required comments at the top.
For example, if the user inputs Eve, Lulis as the first and last name respectively, and December and 11 as the month and day of their birth, the program will print the following:
Usage (after pressing the F5 button):
>>>Please enter your first name: Eve
>>>Please enter your last name: Lulis
>>>Please enter the month of your birth spelled out: December
>>>Please enter the day of your birth as a number: 11
Hello, Eve Lulis!
You were born on the 11 day of December.
Usage (after pressing the F5 button): |
>>> Enter a restaurant bill (> 0): 45.37 |
>>> Enter the tip percentage (e.g. 15 = 15%): 20 |
The tip for a bill of $45.37 at 20% is 9.074. |
1.
>>> s1 = 'abcdefghijklmnopqrstuvwxyz'
>>> s1[s1.index('m')]
'm'
>>> s1[s1.index('p')]
'p'
>>> s1[s1.index('z')]
'z'
>>> s1[s1.index('e')]
'e'
>>> s1[s1.index('a')]
'a'
2.
>>> flowers
['rose', 'bougainvillea', 'yucca', 'marigold', 'daylily', 'lily of
the valley']
>>> 'carrot' in flowers
False
>>> thorny = flowers[:3]
>>> thorny
['rose', 'bougainvillea', 'yucca']
>>> poisonous = flowers[-1:]
>>> poisonous
['lily of the valley']
dangerous = thorny + poisonous
>>> dangerous
['rose', 'bougainvillea', 'yucca', 'lily of the valley']
a1.py
fname = input("Please enter your first name: ")
lname = input("PLease enter your last name: ")
month = input("Please enter the month of your birth spelled out:
")
day = input("Please enter the day of your birth as a number: ")
print("Hello,", fname, lname, "!");
print("You were born on the", day, "day if", month)
Please enter your first name: Eve
PLease enter your last name: Lulis
Please enter the month of your birth spelled out: December
Please enter the day of your birth as a number: 11
Hello, Eve Lulis !
You were born on the 11 day if December
billing.py
Enter a restaurant bill (> 0): 45.37
Enter the tip percentage (e.g. 15 = 15%): 20
The tip for a bill of $ 45.37 at 9.074