CSCI101 Lab03 Strings Lists
CSCI101 Lab03 Strings Lists
What about:
print(s[len(s)])
print(s[len(s-1)])
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
s= C o m p u t e r s c i e n c e
Page 1
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Strings and Lists
#program 2
L = input('Enter list of values:').split()
print(L)
print(len(L))
print(L[0])
print(L[3])
3. Write a program that reads the user full name and prints first name and last name
separately. Test you program using the following examples:
Example1: Example2:
Enter name: Mohamed Ahmed Enter name: Mohamed Ahmed Kamal
Your first name is: Mohamed Your first name is: Mohamed
Your last name is: Ahmed Your last name is: Kamal
4. The following programs use two different methods to loop through a list L and print
only positive items.
#Method 1
L = input('Enter list of values:').split()
for i in range (len(L)):
if ……………… > 0:
print(……………………)
#Method 2
L = input('Enter list of values:').split()
for i in L:
if ……………… > 0:
print(……………………)
Page 2
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Strings and Lists
5. It is required to write a program that takes an integer value from the user and a list
of integer numbers. The program should count how many items in the list are less
than or equal to the given value and print the final count. If no such items are
found, the program prints “ALL items are greater”
A beginner tried to write this program but had made some mistakes. Check the
program and fix the mistakes
value = int(input('Enter a value:'))
L = int(input('Enter list of values:'))
for i in L:
c = 0
if L[i] <= value:
c = c + 1
print(c)
else:
print('All item are greater')
6. Complete the following program that reads a list of employees’ gross salaries and
reads the tax. It then prepares and prints a new list for the net salaries after taxes.
Assume salaries are integer and tax is a fraction. Check the example below
sal = input('Enter salaries:')._________
tax = float( _________ ('enter tax:'))
salAfterTax=[]
for v in __________:
x = int(v) - ___________
salAfterTax._________ (x)
print(___________________)
Example:
Enter salaries:2500 6450 8425
enter tax:0.22
salaries after tax: [1950.0, 5031.0, 6571.5]
Page 3
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Strings and Lists
7. Write a program that reads a statement from the user and counts the number of
vowel letters in the statement. If no vowels found the program prints “No vowels
found”.
8. Write a program that reads a list of integers A and produces and prints
another list B that contains the square values of A’s items
Example:
A = 10 12 5 4
B = [100, 144, 25, 16]
9. Write a program that reads two strings S1 and S2 and checks whether S1
and S2 are identical. If they are identical, print “Identical Strings”. Otherwise,
print all locations at which mismatches are found. Assume S1 and S2 are of
the same length.
Example1: (user input is in blue)
Enter s1:Ahmed Ali
Enter s2:Ahmed Ali
Identical Strings
Page 4
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Strings and Lists
10. Write a program to read a list of students’ grades from the user and do the
following:
a. Check each element if it is a valid grade (valid range is from 0 to 100).
For each grade, the program displays either Valid or Invalid. Count
number of invalid grades.
d. Find and display the highest and lowest grades and specify their
locations.
e. Locate and display students having grades greater than 85%, and
display their count.
f. Locate and display students having grades greater than average, and
display their count.
Page 5