☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Lists

الأسبوع 7 · الدرس ٣ «السلاسل النصية والقوائم في لغة Python» (٢): القوائم والربط بين السلاسل والقوائم والبرامج التطبيقية · 3 دروس
3

Strings versus lists, and a small checker program

ICT Prep 3 Term 1, Unit 1, Lesson 3: pp. 34–42
GoalCompare strings and lists, and read two small programs that loop over them.
New words
list(text) · makes a list with one item for each characterbreak · leaves the loop at onceisdigit() · True when the character is a digitscore · a number that counts points earned
Explanation

Strings and lists are both ordered and both use indexes, but they differ. A string is written in quotation marks, holds only characters and cannot be changed. A list is written in square brackets, holds any values and can be changed. The function list() turns a string into a list of single characters: list("cat") gives ['c', 'a', 't']. You do not need it to loop over a string, because a loop can visit a string directly. Two small programs use both ideas. The first prints the names that start with S, using name[0]. The second gives a strength score to a sample string, one point each for the length, a digit, a capital letter and a symbol. After finding the first digit the loop uses break, so each rule gives at most one point. The sample is an invented string. In real life never type a real password into any practice program or game.

StringList
Written withquotation markssquare brackets
Can holdcharacters onlyany values
Can be changednoyes
Worked examples
Python
1names = ["Mona", "Omar", "Salma", "Sami"]
2for name in names:
3    if name[0] == "S":
4        print(name)
Output
Salma
Sami
Program 1 loops over a list of names and prints those whose first character is S. Mona and Omar fail the test; Salma and Sami pass it.
Python
1sample = "Blue7sky!"
2symbols = "@#$%&!"
3score = 0
4if len(sample) >= 8:
5    score += 1
6for ch in sample:
7    if ch.isdigit():
8        score += 1
9        break
10for ch in sample:
11    if ch.isupper():
12        score += 1
13        break
14for ch in sample:
15    if ch in symbols:
16        score += 1
17        break
18print(score)
Output
4
Program 2 scores the invented sample “Blue7sky!”: 9 characters give 1 point, the digit 7 gives 1, the capital B gives 1 and the symbol ! gives 1. The total is 4. Each loop stops at its first match.
This flow shows the order of the four checks in program 2. A table of invented samples can be filled by following it: a short word such as cat earns 0 points.
Class activity

Use the four checks to give a score to these invented strings, never your own: “Sunny2024”, “tree!”, “ABCDEFGH”. Fill a table with 1 or 0 for each check and the total. Which string scores highest?

Practice
1
What does list("cat") give?
2
Which two names does program 1 print?
Python
1names = ["Mona", "Omar", "Salma", "Sami"]
2for name in names:
3    if name[0] == "S":
4        print(name)
3
Why does program 2 use break after finding the first digit?
4
Why should you never type a real password into a practice program?