Strings & Lists in Python | ICT | Grade 9

ICT – LANGUAGE SCHOOLS

Strings & Lists in Python

Grade 9 – 2027

Unit 1: Artificial Intelligence and Programming Languages

Lesson 3

Introduction

Python provides several useful ways to store and work with data. Two important data types are Strings and Lists.

Strings are used to store text, while lists are used to store multiple values in one variable.

Remember: Strings are mainly used for text, while Lists are used to store collections of items.

Watch the Lesson

Watch the lesson first, then use the explanation and examples below to review the main Python concepts.

Learning Objectives

1. Understand Strings

Explain what a string is and how it is created in Python.

2. Work with Strings

Access individual characters using indexing.

3. Understand Lists

Explain what a list is and how it stores multiple values.

4. Compare Strings and Lists

Recognize the differences between strings and lists.

1. What is a String?

A String is a sequence of characters used to represent text in Python.

A string can contain letters, numbers, spaces, and special characters.

name = "Medhat" city = "Cairo" message = "Welcome to Python"
Strings are normally written inside quotation marks such as " " or ' '.

2. Creating Strings

Example 1

name = "Ahmed" print(name)

The variable name stores the string "Ahmed".

Example 2

subject = "ICT" print(subject)

The variable subject stores the text "ICT".

3. String Indexing

Python gives each character in a string an index. Indexing starts from 0.

Consider this string:

word = "PYTHON"
Character P Y T H O N
Index 0 1 2 3 4 5

Therefore:

word = "PYTHON" print(word[0]) print(word[2]) print(word[5])

The output is:

P T N

4. Negative Indexing

Python also allows negative indexes. The last character has index -1.

word = "PYTHON" print(word[-1]) print(word[-2])

Output:

N O

5. What is a List?

A List is a collection that can store multiple values in one variable.

Lists are written using square brackets [ ].

subjects = ["ICT", "Math", "Science", "English"]

The list above contains four items.

6. Creating Lists

List of Numbers

numbers = [10, 20, 30, 40]

List of Names

students = ["Ali", "Mona", "Omar", "Sara"]

Mixed Data

data = ["ICT", 15, True]

7. List Indexing

Like strings, list items have indexes starting from 0.

subjects = ["ICT", "Math", "Science"] print(subjects[0]) print(subjects[1]) print(subjects[2])

Output:

ICT Math Science

8. Changing a List Item

One important difference between a list and a string is that list elements can be changed.

subjects = ["ICT", "Math", "Science"] subjects[1] = "English" print(subjects)

Output:

['ICT', 'English', 'Science']

9. Adding Items with append()

The append() method adds a new item to the end of a list.

subjects = ["ICT", "Math"] subjects.append("Science") print(subjects)

Output:

['ICT', 'Math', 'Science']

10. Adding Items with insert()

The insert() method adds an item at a specific position.

subjects = ["ICT", "Science"] subjects.insert(1, "Math") print(subjects)

Output:

['ICT', 'Math', 'Science']

11. Strings vs Lists

String List
Stores text. Stores multiple items.
Written using quotation marks. Written using square brackets.
Example: "Python" Example: ["Python", "Java"]
Characters can be accessed by index. Items can be accessed by index.
Strings are immutable. Lists are mutable.

12. Practical Python Example

We can combine strings and lists in a simple program.

student = "Ahmed" subjects = ["ICT", "Math", "Science"] print("Student:", student) print("First subject:", subjects[0])

This program stores a student's name in a string and several subjects in a list.

13. Important Points to Remember

  • A string is a sequence of characters.
  • A list is a collection of items.
  • String indexing starts from 0.
  • List indexing also starts from 0.
  • The last item can be accessed using index -1.
  • append() adds an item to the end of a list.
  • insert() adds an item at a specific position.
  • Lists are mutable, while strings are immutable.

Quick Review

  • String: A sequence of characters used to store text.
  • List: A collection of multiple values.
  • Index: The position of an item or character.
  • Python indexing starts at 0.
  • append() adds an item to the end of a list.
  • insert() adds an item at a specific position.

Interactive Quiz

Choose the best answer for each question. You must answer all questions before submitting.

1. What is a String in Python?

2. Which symbol is commonly used to create a Python list?

3. What is the first index in a Python string?

4. What is the result of "PYTHON"[0]?

5. Which index represents the last item?

6. What does append() do?

7. Which method adds an item at a specific position?

8. Which statement creates a list?

9. Which statement about lists is correct?

10. Which statement is correct?

Quiz Result

No comments:

Post a Comment

التصنيفات

```html ```html ```