Untitled

Introduction to Programming with Python

  • Course Code: UGC 1201 - Computer Literacy II
  • Lecturer: Lenandlar Singh
  • Date: March 18, 2026

Objectives

  • At the end of this lecture, students should be able to:
    • Create and use lists
    • Create and use loops
    • Use loops to process lists

Python Lists

  • Definition of a List: A list is a kind of collection that allows us to put many values in a single "variable".
    • This is useful because it enables us to carry many values around in one convenient package.
    • Examples:
    • friends = ['Joseph', 'Glenn', 'Sally']
    • carryon = ['socks', 'shirt', 'perfume']

Characteristics of Python Lists

  • Versatile Data Type: Lists in Python can store different data types and are defined using square brackets with values separated by commas.
    • Examples:
    • list1 = ['physics', 'chemistry', 1997, 2000]
    • list2 = [1, 2, 3, 4, 5]
    • list3 = ['a', 'b', 'c', 'd']

Built-in Functions and Lists

  • Python provides several built-in functions that work with lists as parameters.
  • Examples of Built-in Functions:
    • To illustrate:
      python >>> nums = [3, 41, 12, 9, 74, 15] >>> print(len(nums)) # Output: 6 >>> print(max(nums)) # Output: 74 >>> print(min(nums)) # Output: 3 >>> print(sum(nums)) # Output: 154 >>> print(sum(nums) / len(nums)) # Output: 25

Visualising a List Data Structure

  • A list can be visualized as follows:
    • Indices: 0 1 2 …
    • Values: 12 21 34
    • The elements of a list can be of any type, e.g.:
    • Index 0: "first"
    • Index 1: 21.5
    • Index 2: True

Accessing List Elements

  • Lists use square brackets to access elements.
    • Example: For a list my_list:
      python my_list = [12, 21, 34]
    • The first element can be accessed with my_list[0], and the second with my_list[1].

Creating and Printing Lists

  • Lists are created using square brackets, with elements separated by commas.
  • Examples:
    • Creating a list: my_list = [12, 21, 34]
    • Creating an empty list can be done by:
    • my_list = []
    • my_list = list()

Printing a List

  • Lists can be printed using the print() function:
    python my_list = [5, 2, 7, 4, 3, 8, 0, 1, 9, 6, -3] print(my_list) # Output: [5, 2, 7, 4, 3, 8, 0, 1, 9, 6, -3]
  • The length of a list can be obtained using len():
    python number_of_elements = len(my_list) print(number_of_elements) # Output: 11

Iterating Through a List

  • To visit each element in a list, a for loop can be used: python my_list = ['No', 'keyboard', 'detected.', 'Press', 'F1', 'to', 'continue'] for element in my_list: print(element)
    • Output:
      • No
      • keyboard
      • detected.
      • Press
      • F1
      • to
      • continue

Processing Lists

  • Example of Counting Elements:
    python num = [1, 2, 3, 5, 3, 6, 7, 8, 4, 8] count = 0 for items in num: if items > 5: count += 1 print(count, "items greater than 5") # Output: (number of items greater than 5)

Adding to a List

  • Lists can be appended to using append().
  • Example:
    python numbers = [22, 300, 400, 440, 650] for i in range(5): print(numbers[i]) numbers.append(i) for i in range(len(numbers)): print(numbers[i])

Basic Search in a List

  • Example of Searching:
    python numbers = [22, 300, 400, 440, 650] searchTerm = int(input('Enter search value: ')) if searchTerm in numbers: print('Search term found')

Loops and Iteration in Python

  • Definition of Iteration/Loop: This describes repeating code execution until a condition or exit point is met.
  • Types of Loops:
    • For Loop: Used for iterating over a sequence (list, tuple, dictionary, set, or string).
    • While Loop

Examples of For Loops

  • Example with Input:

    person = input('Enter your name: ')
    for i in range(5):
        print('Hello', person)
    
  • Example of Iterating Over a List:

    fruits = ['apple', 'banana', 'cherry']
    for x in fruits:
        print(x)
    
  • Example of Iterating with Range:
    python for i in range(5): print('i is', i)

The range() Function

  • The range() function allows looping through a set number of times.
    • It returns a sequence of numbers starting from 0 by default, incrementing by 1 by default, and ending at a specified number.
    • Important Note: range(6) produces values 0 to 5, not 0 to 6.
    • You can specify starting values, e.g. range(2, 6) means values from 2 to 5.

Generating Random Numbers

  • Example of Generating Random Numbers:
    • A program that generates a list of 5 random numbers from 1 to 9, which replaces initial values in the list:
      python numbers = [0, 0, 0, 0, 0] # Code that generates random numbers goes here.
    • A program that generates a list of 100 random numbers from 1 to 9.

Homework Assignment

  • Write a program in Python that:
    • Collects a list of n numbers from the user via the keyboard and calculates how many numbers are greater than the average of all the numbers.

End of Lecture