SCMA241 Lab : Lists

In [1]:
# create an empty list and initialize its values by random numbers
import random

number = int(input('Enter the length of the list : '))

# (1) create a list
my_list = [0]*number

# (2) assign values to elements of the list
for i in range(number):
    my_list[i] = random.randint(1,100)

# (3) print the list
print(my_list)
Enter the length of the list : 5
[88, 32, 16, 43, 17]
In [2]:
# count the number of odd and even numbers in the list
#
import random

def count_odd(my_list):
    # count the number of odd numbers
    num_odd = 0
    
    for num in my_list:
        if num%2 == 1:
            num_odd += 1
            
    return num_odd

def count_even(my_list):
    # count the number of even numbers
    num_even = 0
    
    for num in my_list:
        if num%2 == 0:
            num_even += 1
            
    return num_even
    
# define the main function
def main():
    
    number = int(input('Enter the length of the list : '))

    my_list = [0]*number

    for i in range(number):
        my_list[i] = random.randint(1,100)
    
    print(my_list)
    
    # call the function
    num_odd = count_odd(my_list)
    num_even = count_even(my_list)
    
    print(num_odd,num_even)
    
# call the main function
main()
Enter the length of the list : 10
[20, 9, 5, 54, 78, 56, 41, 14, 23, 38]
4 6
In [3]:
# indexing and slicing : write a function that receives a list as an argument and 
# computes the square of each element in the list.

len(my_list)
Out[3]:
5
In [4]:
# write a function that returns the length of a list (without using len() function).
#
def my_len(my_list):
    # find the length of the list 
    length = 0
    
    for num in my_list:
        length += 1
    
    return length
    
# define the main function
def main():
    mylist = [1,2,3,4,5,6,7]
    length = my_len(mylist)
    
    print('The length of this list is ', length)
    
# call the main function
main()
The length of this list is  7
In [7]:
# write a program that converts a string into a list and print it in reverse order.
#
def print_reverse(mystr):
    # print a string in reverse
    
    my_list = list(mystr)
    for i in range(len(my_list)):
        print(my_list[len(my_list)-i-1],end='')
    
# define the main function
def main():
    name = "Krikamol"
    print_reverse(name)
    
# call the main function
main()
lomakirK
In [8]:
# write a function that counts the number of unique elements in a list (enumerate function).
#
def count_unique(my_list):
    # count the number of unique elements
    num_unique = 0
    
    for i in range(len(my_list)):
        is_unique = True
        
        for j in range(i):
            if my_list[i] == my_list[j]:
                is_unique = False
                break
        
        if is_unique:
            num_unique += 1
    
    return num_unique
    
# define the main function
def main():
    mylist = [2,3,2,7,4,3]
    num_unique = count_unique(mylist)
    
    print('The number of unique elements is ', num_unique)
    
# call the main function
main()
The number of unique elements is  4
In [ ]:
 
In [1]:
 
In [ ]: