1. Trang chủ
  2. » Công Nghệ Thông Tin

Algorithms Programming with Python Module 1 – Python basics

24 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 24
Dung lượng 762,5 KB

Nội dung

PowerPoint Presentation Algorithms Programming with Python Module 1 – Python basics – Lesson 8 Nguyễn Chí ThứcList 2 • A list is a se.PowerPoint Presentation Algorithms Programming with Python Module 1 – Python basics – Lesson 8 Nguyễn Chí ThứcList 2 • A list is a se.

Algorithms & Programming with Python Module – Python basics – Lesson Nguyễn Chí Thức gthuc.nguyen@gmail.com 0986636879 List • A list is a sequence of elements numbered from (just as characters in the string) • The list can be set manually by enumerating of the elements the list in square brackets, like here: Primes = [2, 3, 5, 7, 11, 13] Rainbow = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'] List Primes = [2, 3, 5, 7, 11, 13] Rainbow = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'] • The list Primes has elements, namely: Primes[0] == 2, Primes[1] == 3, Primes[2] == 5, Primes[3] == 7, Primes[4] == 11, Primes[5] == 13 • The list Rainbow has elements, each of which is the string Length of the list Primes = [2, 3, 5, 7, 11, 13] • Length of the list: len(Primes) == • The list elements can also have negative index (start from last element): Primes[-1] == 13, Primes[-6] == Elements of list Unlike strings, the elements of a list are changeable: Rainbow = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'] print(Rainbow[0]) Rainbow[0] = 'red' for i in range(len(Rainbow)): print(Rainbow[i]) Adding element to the end of a list Using append() function: a = [] # start an empty list n = int(input()) # read number of elements for i in range(n): new_element = int(input()) # read next element a.append(new_element) # add it to the list print(a) List concatenation (addition) & repetition (multiplying) a = [1, 2, 3] b = [4, 5] c = a + b # d = b * # print([7, 8] + [9]) # print([0, 1] * 3) # [1, [4, [7, [0, 2, 5, 8, 1, 3, 4, 5] 4, 5, 4, 5] 9] 0, 1, 0, 1] Reading a list with predefined length a = [0] * int(input()) for i in range(len(a)): a[i] = int(input()) Loop through elements of list a = [1, 2, 3, 4, 5] for i in range(len(a)): print(a[i], end=' ') for elem in a: print(elem, end=' ') Quiz s = 'ab12c59p7dq' digits = [] for symbol in s: if '1234567890'.find(symbol) != -1: digits.append(int(symbol)) print(digits) 10 String’s split() and join() a = '11.03.2018'.split('.') print(a) # ['11', '03', '2018'] b = "/".join(a) # 11/03/2018 11 Input a list of value in one line (separated by spaces) # the input is a string # s = input() # s == '1 3' a = s.split() print(a) # ['1', '2', '3'] for i in range(len(a)): a[i] = int(a[i]) print(a) # [1, 2, 3] 12 Input a list of value in one line – using generator a = [int(s) for s in input().split()] print(a) 13 Display list of strings in one line a = ['red', 'green', 'blue'] print(' '.join(a)) # red green blue print(''.join(a)) # redgreenblue print('**'.join(a)) # red**green**blue 14 Display list of numbers in one line a = [1, 2, 3] # print(' '.join(a))  ERROR print(' '.join([str(i) for i in a])) # OK # 123 15 Generators Generators are used to fill a list according to a formula: [expression for variable in sequence] a = [0 for i in range(5)] print(a) # [0, 0, 0, 0, 0] 16 Generators Generators are used to fill a list according to a formula: [expression for variable in sequence] n = a = [i ** for i in range(1, n + 1)] print(a) # what is the output? 17 Generators Generators are used to fill a list according to a formula: [expression for variable in sequence] n = a = [i ** for i in range(1, n + 1)] print(a) # [1, 4, 9, 16, 25] 18 Slices Slicing with lists is similar to that with strings: • A[i:j] slice j-i elements A[i], A[i+1], , A[j-1] • A[i:j:-1] slice i-j elements A[i], A[i-1], • • ., A[j+1] A[i:j:k] cut with the step k: A[i], A[i+k], A[i+2*k] • If k

Ngày đăng: 20/10/2022, 14:39