2 ListsTuples
2 ListsTuples
• For non-negative indices, the length of a slice is the difference of the indices,
if both are within bounds
• >>> squares[:3]
[1, 4, 9]
• >>> squares[:]
[1, 4, 9, 16, 25]
• Removing slice
>>> letters[2:5]=[]
>>> letters
['a', 'b', 'f', 'g']
1 for i in range(5):
2 print (i)
3
4 Output
5 0
6 1
7 2
8 3
9 4
CSCI2040 Introduction to Python 17
for statement
• range() function combine with len() can iterate over elements in a list
• We retrieve the item through their index here
• range(5, 10)
5, 6, 7, 8, 9
• range(0, 10, 3)
0, 3, 6, 9
cse_teachers = ['John C.S. Lui', 'Patrick P.C. Lee', 'James Cheng', 'Wei Meng']
print ("first element in the list is =", cse_teachers[6])
#print ("first element in the list is =", cse_teachers[-7])
• char pw[100];
int cnt=0;
Need to think of character, loop ,
character function
scanf("%s", pw); Many low level thinking
for (i=0; i<strlen(pw); i++)
if (islower(pw[i]) cnt++;
if (cnt<=0) printf("No lower case ");