Danh sách Python: Bài tập - 16 với giải pháp
Viết một chương trình Python để tạo và in danh sách 5 yếu tố đầu tiên và cuối cùng trong đó các giá trị là hình vuông số từ 1 đến 30 (cả hai bao gồm).
Ví dụ 1 :
Ví dụ - 2:
Ví dụ - 3:
Giải pháp mẫu:-:-
Mã Python:
def printValues(): l = list() for i in range(1,21): l.append(i**2) print(l[:5]) print(l[-5:]) printValues()Đầu ra mẫu:
[1, 4, 9, 16, 25] [256, 289, 324, 361, 400Sơ đồ:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.
Trước đây: Viết chương trình Python để xáo trộn và in một danh sách được chỉ định. Write a Python program to shuffle and print a specified list.
Next: Write a Python program to generate and print a list except for the first 5 elements, where the values are square of numbers between 1 and 30 (both included).
Python: Tạo và in một danh sách các số từ 1 đến 10.
Giải pháp mẫu:.
Mã Python: nums = phạm vi (1,10) in (danh sách (nums)) in (danh sách (bản đồ (str, nums))) ....
Python: Tạo và in một danh sách các số từ 1 đến 10.
Giải pháp mẫu:.
Mã Python: nums = phạm vi (1,10) in (danh sách (nums)) in (danh sách (bản đồ (str, nums))) ....
Trình chỉnh sửa mã Python: ....list in python can be done is following ways:
- Có một cách khác để giải quyết giải pháp này? ....for loop : Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it.
Python
a =
Trước đây: Viết chương trình Python để in các chữ cái từ bảng chữ cái tiếng Anh từ A-Z và A-Z ..
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 561 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 57 1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
- Không sử dụng các vòng lặp: * Biểu tượng được sử dụng để in các phần tử danh sách trong một dòng duy nhất có không gian. Để in tất cả các phần tử trong các dòng mới hoặc được phân tách bằng dấu phẩy sử dụng sep =, \ n, hoặc sep =, tương ứng. & Nbsp; * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.
Python
a =
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 531 2 3 4 5 in new line 1 2 3 4 541 2 3 4 5 in new line 1 2 3 4 55
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 531 2 3 4 5 in new line 1 2 3 4 581 2 3 4 5 in new line 1 2 3 4 59
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 531 2 3 4 5 in new line 1 2 3 4 541 2 3 4 5 In new line 1 2 3 4 53= 1 2 3 4 5 In new line 1 2 3 4 551 2 3 4 5 in new line 1 2 3 4 59
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 531 2 3 4 5 In new line 1 2 3 4 591 2 3 4 5 in new line 1 2 3 4 59
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 531 2 3 4 5 in new line 1 2 3 4 541 2 3 4 5 In new line 1 2 3 4 53= a 61 2 3 4 5 in new line 1 2 3 4 59
Đầu ra
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 5- Chuyển đổi danh sách thành một chuỗi để hiển thị: Nếu đó là danh sách các chuỗi, chúng ta chỉ cần tham gia vào hàm tham gia () chuỗi và in chuỗi. & nbsp;If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.
Python
a =[=1[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 0=3[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 0=1[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 8
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 53=9[0
a =
Is
Đầu ra
Geeks for Geeks 1, 2, 3, 4, 5- Sử dụng bản đồ: Sử dụng map () để chuyển đổi từng mục trong danh sách thành một chuỗi nếu danh sách không phải là một chuỗi, sau đó tham gia chúng: & nbsp; Use map() to convert each item in the list to a string if list is not a string, and then join them:
Python
a =
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 53=9[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 18[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 191 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 5315[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 22
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 57[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 24
Is
Đầu ra
1 2 3 4 5 in new line 1 2 3 4 5- Sử dụng bản đồ: Sử dụng map () để chuyển đổi từng mục trong danh sách thành một chuỗi nếu danh sách không phải là một chuỗi, sau đó tham gia chúng: & nbsp;Use list comprehension to go one by one to each element in list and print.
Python3
a =
Is
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 571 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 53[1, 4, 9, 16, 25] [256, 289, 324, 361, 400 581 2 3 4 5 in new line 1 2 3 4 59
Đầu ra
Đầu ra
1 2 3 4 5 In new line 1 2 3 4 5