0% found this document useful (0 votes)
12 views

CSCI101 Lab03 Strings Lists

The document discusses strings and lists in Python. It provides examples of string indexing and list operations, and poses programming problems involving string and list manipulation, such as checking for vowels in a string, finding squares of list elements, comparing strings for mismatches, validating grades in a list, and performing calculations on list elements. The document is from an introductory computer science course covering basic Python concepts like strings and lists.

Uploaded by

Eyad Alaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CSCI101 Lab03 Strings Lists

The document discusses strings and lists in Python. It provides examples of string indexing and list operations, and poses programming problems involving string and list manipulation, such as checking for vowels in a string, finding squares of list elements, comparing strings for mismatches, validating grades in a list, and performing calculations on list elements. The document is from an introductory computer science course covering basic Python concepts like strings and lists.

Uploaded by

Eyad Alaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Science and Technology

Communications & Information Engineering Program


CSCI 101: Introduction to Computer Science
Strings and Lists

1. What is the output of the following program?


First solve the problem on a paper then check your answer using Python IDE.
s = ‘Computer science’
print(s); print(len(s));
print(s[1]) print(s[30])
print(s[-1]); print(s[len(s)-1]);
print(s[3:6]); print(s[6:3])
print(s[1:9:2]); print(s[9:1:-2])
print(s[9:]); print(s[:9])
print(s[-1:-5]); print(s[-5:-1])

What about:
print(s[len(s)])
print(s[len(s-1)])

The following table may help:

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

2. What is the output of the following programs if the input is:10 2 35 7


#program 1
L = input('Enter list of values:')
print(L)
print(len(L))
print(L[0])
print(L[3])

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(……………………)

Think: When to use each of the above methods?

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

Example2: (user input is in blue)


Enter s1:man on Mars
Enter s2:fan in mars
mismatch at index 0
mismatch at index 4
mismatch at index 7

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.

b. Check each element if it is a valid grade (valid range is from 0 to 100).


Produce a corresponding list (same size as the grades list) that has 1
or 0 in the same grade position; 1 if the grade is valid and 0 if it is
invalid.
A grade list [90 -10 50 130 -2] will produce an output list [1 0 1 0 0]

c. Calculate and display the average grade.

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

You might also like